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