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