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