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