src/HOL/Library/Multiset.thy
author eberlm
Wed Jun 01 13:48:34 2016 +0200 (2016-06-01)
changeset 63195 f3f08c0d4aaf
parent 63099 af0e964aad7b
child 63290 9ac558ab0906
permissions -rw-r--r--
Tuned code equations for mappings and PMFs
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@60606
    28
lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
nipkow@39302
    29
  by (simp only: count_inject [symmetric] fun_eq_iff)
haftmann@34943
    30
wenzelm@60606
    31
lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
nipkow@39302
    32
  using multiset_eq_iff by auto
haftmann@34943
    33
wenzelm@60606
    34
text \<open>Preservation of the representing set @{term multiset}.\<close>
wenzelm@60606
    35
wenzelm@60606
    36
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset"
haftmann@34943
    37
  by (simp add: multiset_def)
haftmann@34943
    38
wenzelm@60606
    39
lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset"
haftmann@34943
    40
  by (simp add: multiset_def)
haftmann@34943
    41
wenzelm@60606
    42
lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
haftmann@34943
    43
  by (simp add: multiset_def)
haftmann@34943
    44
haftmann@34943
    45
lemma diff_preserves_multiset:
haftmann@34943
    46
  assumes "M \<in> multiset"
haftmann@34943
    47
  shows "(\<lambda>a. M a - N a) \<in> multiset"
haftmann@34943
    48
proof -
haftmann@34943
    49
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    50
    by auto
haftmann@34943
    51
  with assms show ?thesis
haftmann@34943
    52
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    53
qed
haftmann@34943
    54
haftmann@41069
    55
lemma filter_preserves_multiset:
haftmann@34943
    56
  assumes "M \<in> multiset"
haftmann@34943
    57
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
haftmann@34943
    58
proof -
haftmann@34943
    59
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    60
    by auto
haftmann@34943
    61
  with assms show ?thesis
haftmann@34943
    62
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    63
qed
haftmann@34943
    64
haftmann@34943
    65
lemmas in_multiset = const0_in_multiset only1_in_multiset
haftmann@41069
    66
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
haftmann@34943
    67
haftmann@34943
    68
wenzelm@60500
    69
subsection \<open>Representing multisets\<close>
wenzelm@60500
    70
wenzelm@60500
    71
text \<open>Multiset enumeration\<close>
haftmann@34943
    72
huffman@48008
    73
instantiation multiset :: (type) cancel_comm_monoid_add
haftmann@25571
    74
begin
haftmann@25571
    75
bulwahn@47429
    76
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
bulwahn@47429
    77
by (rule const0_in_multiset)
haftmann@25571
    78
haftmann@34943
    79
abbreviation Mempty :: "'a multiset" ("{#}") where
haftmann@34943
    80
  "Mempty \<equiv> 0"
haftmann@25571
    81
wenzelm@60606
    82
lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
bulwahn@47429
    83
by (rule union_preserves_multiset)
haftmann@25571
    84
wenzelm@60606
    85
lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
haftmann@59815
    86
by (rule diff_preserves_multiset)
haftmann@59815
    87
huffman@48008
    88
instance
wenzelm@60678
    89
  by (standard; transfer; simp add: fun_eq_iff)
haftmann@25571
    90
haftmann@25571
    91
end
wenzelm@10249
    92
eberlm@63195
    93
context
eberlm@63195
    94
begin
eberlm@63195
    95
eberlm@63195
    96
qualified definition is_empty :: "'a multiset \<Rightarrow> bool" where
eberlm@63195
    97
  [code_abbrev]: "is_empty A \<longleftrightarrow> A = {#}"
eberlm@63195
    98
eberlm@63195
    99
end
eberlm@63195
   100
eberlm@63195
   101
wenzelm@60606
   102
lift_definition single :: "'a \<Rightarrow> 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
bulwahn@47429
   103
by (rule only1_in_multiset)
kleing@15869
   104
wenzelm@26145
   105
syntax
wenzelm@60606
   106
  "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
nipkow@25507
   107
translations
nipkow@25507
   108
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
   109
  "{#x#}" == "CONST single x"
nipkow@25507
   110
haftmann@34943
   111
lemma count_empty [simp]: "count {#} a = 0"
bulwahn@47429
   112
  by (simp add: zero_multiset.rep_eq)
wenzelm@10249
   113
haftmann@34943
   114
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
bulwahn@47429
   115
  by (simp add: single.rep_eq)
nipkow@29901
   116
wenzelm@10249
   117
wenzelm@60500
   118
subsection \<open>Basic operations\<close>
wenzelm@60500
   119
haftmann@62430
   120
subsubsection \<open>Conversion to set and membership\<close>
haftmann@62430
   121
haftmann@62430
   122
definition set_mset :: "'a multiset \<Rightarrow> 'a set"
haftmann@62430
   123
  where "set_mset M = {x. count M x > 0}"
haftmann@62430
   124
haftmann@62537
   125
abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
haftmann@62537
   126
  where "Melem a M \<equiv> a \<in> set_mset M"
haftmann@62537
   127
haftmann@62537
   128
notation
haftmann@62537
   129
  Melem  ("op \<in>#") and
haftmann@62537
   130
  Melem  ("(_/ \<in># _)" [51, 51] 50)
haftmann@62537
   131
haftmann@62537
   132
notation  (ASCII)
haftmann@62537
   133
  Melem  ("op :#") and
haftmann@62537
   134
  Melem  ("(_/ :# _)" [51, 51] 50)
haftmann@62537
   135
haftmann@62537
   136
abbreviation not_Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
haftmann@62537
   137
  where "not_Melem a M \<equiv> a \<notin> set_mset M"
haftmann@62537
   138
haftmann@62537
   139
notation
haftmann@62537
   140
  not_Melem  ("op \<notin>#") and
haftmann@62537
   141
  not_Melem  ("(_/ \<notin># _)" [51, 51] 50)
haftmann@62537
   142
haftmann@62537
   143
notation  (ASCII)
haftmann@62537
   144
  not_Melem  ("op ~:#") and
haftmann@62537
   145
  not_Melem  ("(_/ ~:# _)" [51, 51] 50)
haftmann@62430
   146
haftmann@62430
   147
context
haftmann@62430
   148
begin
haftmann@62430
   149
haftmann@62430
   150
qualified abbreviation Ball :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@62430
   151
  where "Ball M \<equiv> Set.Ball (set_mset M)"
haftmann@62430
   152
haftmann@62430
   153
qualified abbreviation Bex :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@62430
   154
  where "Bex M \<equiv> Set.Bex (set_mset M)"
haftmann@62430
   155
haftmann@62430
   156
end
haftmann@62430
   157
haftmann@62430
   158
syntax
haftmann@62430
   159
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>#_./ _)" [0, 0, 10] 10)
haftmann@62430
   160
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>#_./ _)" [0, 0, 10] 10)
haftmann@62430
   161
haftmann@62537
   162
syntax  (ASCII)
haftmann@62537
   163
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_:#_./ _)" [0, 0, 10] 10)
haftmann@62537
   164
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_:#_./ _)" [0, 0, 10] 10)
haftmann@62537
   165
haftmann@62430
   166
translations
haftmann@62430
   167
  "\<forall>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Ball A (\<lambda>x. P)"
haftmann@62430
   168
  "\<exists>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Bex A (\<lambda>x. P)"
haftmann@62430
   169
haftmann@62430
   170
lemma count_eq_zero_iff:
haftmann@62430
   171
  "count M x = 0 \<longleftrightarrow> x \<notin># M"
haftmann@62430
   172
  by (auto simp add: set_mset_def)
haftmann@62430
   173
haftmann@62430
   174
lemma not_in_iff:
haftmann@62430
   175
  "x \<notin># M \<longleftrightarrow> count M x = 0"
haftmann@62430
   176
  by (auto simp add: count_eq_zero_iff)
haftmann@62430
   177
haftmann@62430
   178
lemma count_greater_zero_iff [simp]:
haftmann@62430
   179
  "count M x > 0 \<longleftrightarrow> x \<in># M"
haftmann@62430
   180
  by (auto simp add: set_mset_def)
haftmann@62430
   181
haftmann@62430
   182
lemma count_inI:
haftmann@62430
   183
  assumes "count M x = 0 \<Longrightarrow> False"
haftmann@62430
   184
  shows "x \<in># M"
haftmann@62430
   185
proof (rule ccontr)
haftmann@62430
   186
  assume "x \<notin># M"
haftmann@62430
   187
  with assms show False by (simp add: not_in_iff)
haftmann@62430
   188
qed
haftmann@62430
   189
haftmann@62430
   190
lemma in_countE:
haftmann@62430
   191
  assumes "x \<in># M"
haftmann@62430
   192
  obtains n where "count M x = Suc n"
haftmann@62430
   193
proof -
haftmann@62430
   194
  from assms have "count M x > 0" by simp
haftmann@62430
   195
  then obtain n where "count M x = Suc n"
haftmann@62430
   196
    using gr0_conv_Suc by blast
haftmann@62430
   197
  with that show thesis .
haftmann@62430
   198
qed
haftmann@62430
   199
haftmann@62430
   200
lemma count_greater_eq_Suc_zero_iff [simp]:
haftmann@62430
   201
  "count M x \<ge> Suc 0 \<longleftrightarrow> x \<in># M"
haftmann@62430
   202
  by (simp add: Suc_le_eq)
haftmann@62430
   203
haftmann@62430
   204
lemma count_greater_eq_one_iff [simp]:
haftmann@62430
   205
  "count M x \<ge> 1 \<longleftrightarrow> x \<in># M"
haftmann@62430
   206
  by simp
haftmann@62430
   207
haftmann@62430
   208
lemma set_mset_empty [simp]:
haftmann@62430
   209
  "set_mset {#} = {}"
haftmann@62430
   210
  by (simp add: set_mset_def)
haftmann@62430
   211
haftmann@62430
   212
lemma set_mset_single [simp]:
haftmann@62430
   213
  "set_mset {#b#} = {b}"
haftmann@62430
   214
  by (simp add: set_mset_def)
haftmann@62430
   215
haftmann@62430
   216
lemma set_mset_eq_empty_iff [simp]:
haftmann@62430
   217
  "set_mset M = {} \<longleftrightarrow> M = {#}"
haftmann@62430
   218
  by (auto simp add: multiset_eq_iff count_eq_zero_iff)
haftmann@62430
   219
haftmann@62430
   220
lemma finite_set_mset [iff]:
haftmann@62430
   221
  "finite (set_mset M)"
haftmann@62430
   222
  using count [of M] by (simp add: multiset_def)
haftmann@62430
   223
haftmann@62430
   224
wenzelm@60500
   225
subsubsection \<open>Union\<close>
wenzelm@10249
   226
haftmann@62430
   227
lemma count_union [simp]:
haftmann@62430
   228
  "count (M + N) a = count M a + count N a"
bulwahn@47429
   229
  by (simp add: plus_multiset.rep_eq)
wenzelm@10249
   230
haftmann@62430
   231
lemma set_mset_union [simp]:
haftmann@62430
   232
  "set_mset (M + N) = set_mset M \<union> set_mset N"
haftmann@62430
   233
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_union) simp
haftmann@62430
   234
wenzelm@10249
   235
wenzelm@60500
   236
subsubsection \<open>Difference\<close>
wenzelm@10249
   237
haftmann@62430
   238
instance multiset :: (type) comm_monoid_diff
haftmann@62430
   239
  by standard (transfer; simp add: fun_eq_iff)
haftmann@62430
   240
haftmann@62430
   241
lemma count_diff [simp]:
haftmann@62430
   242
  "count (M - N) a = count M a - count N a"
bulwahn@47429
   243
  by (simp add: minus_multiset.rep_eq)
haftmann@34943
   244
haftmann@62430
   245
lemma in_diff_count:
haftmann@62430
   246
  "a \<in># M - N \<longleftrightarrow> count N a < count M a"
haftmann@62430
   247
  by (simp add: set_mset_def)
haftmann@62430
   248
haftmann@62430
   249
lemma count_in_diffI:
haftmann@62430
   250
  assumes "\<And>n. count N x = n + count M x \<Longrightarrow> False"
haftmann@62430
   251
  shows "x \<in># M - N"
haftmann@62430
   252
proof (rule ccontr)
haftmann@62430
   253
  assume "x \<notin># M - N"
haftmann@62430
   254
  then have "count N x = (count N x - count M x) + count M x"
haftmann@62430
   255
    by (simp add: in_diff_count not_less)
haftmann@62430
   256
  with assms show False by auto
haftmann@62430
   257
qed
haftmann@62430
   258
haftmann@62430
   259
lemma in_diff_countE:
haftmann@62430
   260
  assumes "x \<in># M - N"
haftmann@62430
   261
  obtains n where "count M x = Suc n + count N x"
haftmann@62430
   262
proof -
haftmann@62430
   263
  from assms have "count M x - count N x > 0" by (simp add: in_diff_count)
haftmann@62430
   264
  then have "count M x > count N x" by simp
haftmann@62430
   265
  then obtain n where "count M x = Suc n + count N x"
haftmann@62430
   266
    using less_iff_Suc_add by auto
haftmann@62430
   267
  with that show thesis .
haftmann@62430
   268
qed
haftmann@62430
   269
haftmann@62430
   270
lemma in_diffD:
haftmann@62430
   271
  assumes "a \<in># M - N"
haftmann@62430
   272
  shows "a \<in># M"
haftmann@62430
   273
proof -
haftmann@62430
   274
  have "0 \<le> count N a" by simp
haftmann@62430
   275
  also from assms have "count N a < count M a"
haftmann@62430
   276
    by (simp add: in_diff_count)
haftmann@62430
   277
  finally show ?thesis by simp
haftmann@62430
   278
qed
haftmann@62430
   279
haftmann@62430
   280
lemma set_mset_diff:
haftmann@62430
   281
  "set_mset (M - N) = {a. count N a < count M a}"
haftmann@62430
   282
  by (simp add: set_mset_def)
haftmann@62430
   283
wenzelm@17161
   284
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
haftmann@52289
   285
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
nipkow@36903
   286
haftmann@62430
   287
lemma diff_cancel [simp]: "A - A = {#}"
haftmann@52289
   288
  by (fact Groups.diff_cancel)
wenzelm@10249
   289
nipkow@36903
   290
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
haftmann@52289
   291
  by (fact add_diff_cancel_right')
wenzelm@10249
   292
nipkow@36903
   293
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
haftmann@52289
   294
  by (fact add_diff_cancel_left')
haftmann@34943
   295
haftmann@52289
   296
lemma diff_right_commute:
wenzelm@60606
   297
  fixes M N Q :: "'a multiset"
wenzelm@60606
   298
  shows "M - N - Q = M - Q - N"
haftmann@52289
   299
  by (fact diff_right_commute)
haftmann@52289
   300
haftmann@52289
   301
lemma diff_add:
wenzelm@60606
   302
  fixes M N Q :: "'a multiset"
wenzelm@60606
   303
  shows "M - (N + Q) = M - N - Q"
haftmann@52289
   304
  by (rule sym) (fact diff_diff_add)
blanchet@58425
   305
wenzelm@60606
   306
lemma insert_DiffM: "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
nipkow@39302
   307
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   308
wenzelm@60606
   309
lemma insert_DiffM2 [simp]: "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
nipkow@39302
   310
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   311
wenzelm@60606
   312
lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
nipkow@39302
   313
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   314
haftmann@62430
   315
lemma diff_union_single_conv:
haftmann@62430
   316
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
haftmann@62430
   317
  by (simp add: multiset_eq_iff Suc_le_eq)
haftmann@62430
   318
haftmann@62430
   319
lemma mset_add [elim?]:
haftmann@62430
   320
  assumes "a \<in># A"
haftmann@62430
   321
  obtains B where "A = B + {#a#}"
haftmann@62430
   322
proof -
haftmann@62430
   323
  from assms have "A = (A - {#a#}) + {#a#}"
haftmann@62430
   324
    by simp
haftmann@62430
   325
  with that show thesis .
haftmann@62430
   326
qed
haftmann@62430
   327
haftmann@62430
   328
lemma union_iff:
haftmann@62430
   329
  "a \<in># A + B \<longleftrightarrow> a \<in># A \<or> a \<in># B"
haftmann@62430
   330
  by auto
bulwahn@26143
   331
wenzelm@10249
   332
wenzelm@60500
   333
subsubsection \<open>Equality of multisets\<close>
haftmann@34943
   334
haftmann@34943
   335
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
nipkow@39302
   336
  by (simp add: multiset_eq_iff)
haftmann@34943
   337
haftmann@34943
   338
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
nipkow@39302
   339
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   340
haftmann@34943
   341
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   342
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   343
haftmann@34943
   344
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   345
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   346
haftmann@34943
   347
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
nipkow@39302
   348
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   349
wenzelm@60606
   350
lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
haftmann@62430
   351
  by (auto simp add: multiset_eq_iff not_in_iff)
haftmann@34943
   352
wenzelm@60606
   353
lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
haftmann@34943
   354
  by auto
haftmann@34943
   355
wenzelm@60606
   356
lemma union_single_eq_diff: "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
haftmann@34943
   357
  by (auto dest: sym)
haftmann@34943
   358
wenzelm@60606
   359
lemma union_single_eq_member: "M + {#x#} = N \<Longrightarrow> x \<in># N"
haftmann@34943
   360
  by auto
haftmann@34943
   361
haftmann@62430
   362
lemma union_is_single:
haftmann@62430
   363
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N = {#} \<or> M = {#} \<and> N = {#a#}"
wenzelm@60606
   364
  (is "?lhs = ?rhs")
wenzelm@46730
   365
proof
wenzelm@60606
   366
  show ?lhs if ?rhs using that by auto
wenzelm@60606
   367
  show ?rhs if ?lhs
haftmann@62430
   368
    by (metis Multiset.diff_cancel add.commute add_diff_cancel_left' diff_add_zero diff_single_trivial insert_DiffM that)
haftmann@34943
   369
qed
haftmann@34943
   370
wenzelm@60606
   371
lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
haftmann@34943
   372
  by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
haftmann@34943
   373
haftmann@34943
   374
lemma add_eq_conv_diff:
wenzelm@60606
   375
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"
wenzelm@60606
   376
  (is "?lhs \<longleftrightarrow> ?rhs")
nipkow@44890
   377
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
haftmann@34943
   378
proof
wenzelm@60606
   379
  show ?lhs if ?rhs
wenzelm@60606
   380
    using that
wenzelm@60606
   381
    by (auto simp add: add.assoc add.commute [of "{#b#}"])
wenzelm@60606
   382
      (drule sym, simp add: add.assoc [symmetric])
wenzelm@60606
   383
  show ?rhs if ?lhs
haftmann@34943
   384
  proof (cases "a = b")
wenzelm@60500
   385
    case True with \<open>?lhs\<close> show ?thesis by simp
haftmann@34943
   386
  next
haftmann@34943
   387
    case False
wenzelm@60500
   388
    from \<open>?lhs\<close> have "a \<in># N + {#b#}" by (rule union_single_eq_member)
haftmann@34943
   389
    with False have "a \<in># N" by auto
wenzelm@60500
   390
    moreover from \<open>?lhs\<close> have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
haftmann@34943
   391
    moreover note False
haftmann@34943
   392
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
haftmann@34943
   393
  qed
haftmann@34943
   394
qed
haftmann@34943
   395
blanchet@58425
   396
lemma insert_noteq_member:
haftmann@34943
   397
  assumes BC: "B + {#b#} = C + {#c#}"
haftmann@34943
   398
   and bnotc: "b \<noteq> c"
haftmann@34943
   399
  shows "c \<in># B"
haftmann@34943
   400
proof -
haftmann@34943
   401
  have "c \<in># C + {#c#}" by simp
haftmann@34943
   402
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
haftmann@34943
   403
  then have "c \<in># B + {#b#}" using BC by simp
haftmann@34943
   404
  then show "c \<in># B" using nc by simp
haftmann@34943
   405
qed
haftmann@34943
   406
haftmann@34943
   407
lemma add_eq_conv_ex:
haftmann@34943
   408
  "(M + {#a#} = N + {#b#}) =
haftmann@34943
   409
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
haftmann@34943
   410
  by (auto simp add: add_eq_conv_diff)
haftmann@34943
   411
wenzelm@60606
   412
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
wenzelm@60678
   413
  by (rule exI [where x = "M - {#x#}"]) simp
haftmann@51600
   414
blanchet@58425
   415
lemma multiset_add_sub_el_shuffle:
wenzelm@60606
   416
  assumes "c \<in># B"
wenzelm@60606
   417
    and "b \<noteq> c"
haftmann@58098
   418
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
haftmann@58098
   419
proof -
wenzelm@60500
   420
  from \<open>c \<in># B\<close> obtain A where B: "B = A + {#c#}"
haftmann@58098
   421
    by (blast dest: multi_member_split)
haftmann@58098
   422
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
blanchet@58425
   423
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
haftmann@58098
   424
    by (simp add: ac_simps)
haftmann@58098
   425
  then show ?thesis using B by simp
haftmann@58098
   426
qed
haftmann@58098
   427
haftmann@34943
   428
eberlm@63099
   429
eberlm@63099
   430
wenzelm@60500
   431
subsubsection \<open>Pointwise ordering induced by count\<close>
haftmann@34943
   432
wenzelm@61955
   433
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<subseteq>#" 50)
wenzelm@61955
   434
  where "A \<subseteq># B = (\<forall>a. count A a \<le> count B a)"
wenzelm@61955
   435
wenzelm@61955
   436
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50)
wenzelm@61955
   437
  where "A \<subset># B = (A \<subseteq># B \<and> A \<noteq> B)"
wenzelm@61955
   438
haftmann@62430
   439
abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supseteq>#" 50)
haftmann@62430
   440
  where "supseteq_mset A B \<equiv> B \<subseteq># A"
haftmann@62430
   441
haftmann@62430
   442
abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supset>#" 50)
haftmann@62430
   443
  where "supset_mset A B \<equiv> B \<subset># A"
blanchet@62208
   444
wenzelm@61955
   445
notation (input)
blanchet@62208
   446
  subseteq_mset  (infix "\<le>#" 50) and
haftmann@62430
   447
  supseteq_mset  (infix "\<ge>#" 50)
wenzelm@61955
   448
wenzelm@61955
   449
notation (ASCII)
wenzelm@61955
   450
  subseteq_mset  (infix "<=#" 50) and
blanchet@62208
   451
  subset_mset  (infix "<#" 50) and
blanchet@62208
   452
  supseteq_mset  (infix ">=#" 50) and
blanchet@62208
   453
  supset_mset  (infix ">#" 50)
Mathias@60397
   454
wenzelm@60606
   455
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#"
wenzelm@60678
   456
  by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym)
wenzelm@62837
   457
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
haftmann@62430
   458
haftmann@62430
   459
lemma mset_less_eqI:
haftmann@62430
   460
  "(\<And>a. count A a \<le> count B a) \<Longrightarrow> A \<subseteq># B"
Mathias@60397
   461
  by (simp add: subseteq_mset_def)
haftmann@34943
   462
haftmann@62430
   463
lemma mset_less_eq_count:
haftmann@62430
   464
  "A \<subseteq># B \<Longrightarrow> count A a \<le> count B a"
haftmann@62430
   465
  by (simp add: subseteq_mset_def)
haftmann@62430
   466
haftmann@62430
   467
lemma mset_le_exists_conv: "(A::'a multiset) \<subseteq># B \<longleftrightarrow> (\<exists>C. B = A + C)"
wenzelm@60678
   468
  unfolding subseteq_mset_def
wenzelm@60678
   469
  apply (rule iffI)
wenzelm@60678
   470
   apply (rule exI [where x = "B - A"])
wenzelm@60678
   471
   apply (auto intro: multiset_eq_iff [THEN iffD2])
wenzelm@60678
   472
  done
haftmann@34943
   473
hoelzl@62376
   474
interpretation subset_mset: ordered_cancel_comm_monoid_diff  "op +" 0 "op \<le>#" "op <#" "op -"
wenzelm@60678
   475
  by standard (simp, fact mset_le_exists_conv)
haftmann@52289
   476
hoelzl@62378
   477
declare subset_mset.zero_order[simp del]
wenzelm@62837
   478
  \<comment> \<open>this removes some simp rules not in the usual order for multisets\<close>
hoelzl@62378
   479
haftmann@62430
   480
lemma mset_le_mono_add_right_cancel [simp]: "(A::'a multiset) + C \<subseteq># B + C \<longleftrightarrow> A \<subseteq># B"
haftmann@62430
   481
   by (fact subset_mset.add_le_cancel_right)
haftmann@62430
   482
 
haftmann@62430
   483
lemma mset_le_mono_add_left_cancel [simp]: "C + (A::'a multiset) \<subseteq># C + B \<longleftrightarrow> A \<subseteq># B"
haftmann@62430
   484
   by (fact subset_mset.add_le_cancel_left)
haftmann@62430
   485
 
haftmann@62430
   486
lemma mset_le_mono_add: "(A::'a multiset) \<subseteq># B \<Longrightarrow> C \<subseteq># D \<Longrightarrow> A + C \<subseteq># B + D"
haftmann@62430
   487
   by (fact subset_mset.add_mono)
haftmann@62430
   488
 
haftmann@62430
   489
lemma mset_le_add_left [simp]: "(A::'a multiset) \<subseteq># A + B"
haftmann@62430
   490
   unfolding subseteq_mset_def by auto
haftmann@62430
   491
 
haftmann@62430
   492
lemma mset_le_add_right [simp]: "B \<subseteq># (A::'a multiset) + B"
haftmann@62430
   493
   unfolding subseteq_mset_def by auto
haftmann@62430
   494
 
haftmann@62430
   495
lemma single_subset_iff [simp]:
haftmann@62430
   496
  "{#a#} \<subseteq># M \<longleftrightarrow> a \<in># M"
haftmann@62430
   497
  by (auto simp add: subseteq_mset_def Suc_le_eq)
haftmann@62430
   498
haftmann@62430
   499
lemma mset_le_single: "a \<in># B \<Longrightarrow> {#a#} \<subseteq># B"
haftmann@62430
   500
  by (simp add: subseteq_mset_def Suc_le_eq)
haftmann@62430
   501
 
haftmann@35268
   502
lemma multiset_diff_union_assoc:
wenzelm@60606
   503
  fixes A B C D :: "'a multiset"
haftmann@62430
   504
  shows "C \<subseteq># B \<Longrightarrow> A + B - C = A + (B - C)"
haftmann@62430
   505
  by (fact subset_mset.diff_add_assoc)
haftmann@62430
   506
 
haftmann@34943
   507
lemma mset_le_multiset_union_diff_commute:
wenzelm@60606
   508
  fixes A B C D :: "'a multiset"
haftmann@62430
   509
  shows "B \<subseteq># A \<Longrightarrow> A - B + C = A + C - B"
haftmann@62430
   510
  by (fact subset_mset.add_diff_assoc2)
haftmann@62430
   511
haftmann@62430
   512
lemma diff_le_self[simp]:
haftmann@62430
   513
  "(M::'a multiset) - N \<subseteq># M"
haftmann@62430
   514
  by (simp add: subseteq_mset_def)
haftmann@62430
   515
haftmann@62430
   516
lemma mset_leD:
haftmann@62430
   517
  assumes "A \<subseteq># B" and "x \<in># A"
haftmann@62430
   518
  shows "x \<in># B"
haftmann@62430
   519
proof -
haftmann@62430
   520
  from \<open>x \<in># A\<close> have "count A x > 0" by simp
haftmann@62430
   521
  also from \<open>A \<subseteq># B\<close> have "count A x \<le> count B x"
haftmann@62430
   522
    by (simp add: subseteq_mset_def)
haftmann@62430
   523
  finally show ?thesis by simp
haftmann@62430
   524
qed
haftmann@62430
   525
  
haftmann@62430
   526
lemma mset_lessD:
haftmann@62430
   527
  "A \<subset># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
haftmann@62430
   528
  by (auto intro: mset_leD [of A])
haftmann@62430
   529
haftmann@62430
   530
lemma set_mset_mono:
haftmann@62430
   531
  "A \<subseteq># B \<Longrightarrow> set_mset A \<subseteq> set_mset B"
haftmann@62430
   532
  by (metis mset_leD subsetI)
haftmann@62430
   533
haftmann@62430
   534
lemma mset_le_insertD:
haftmann@62430
   535
  "A + {#x#} \<subseteq># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
haftmann@34943
   536
apply (rule conjI)
haftmann@34943
   537
 apply (simp add: mset_leD)
haftmann@62430
   538
 apply (clarsimp simp: subset_mset_def subseteq_mset_def)
haftmann@62430
   539
 apply safe
haftmann@62430
   540
  apply (erule_tac x = a in allE)
haftmann@62430
   541
  apply (auto split: if_split_asm)
haftmann@34943
   542
done
haftmann@34943
   543
haftmann@62430
   544
lemma mset_less_insertD:
haftmann@62430
   545
  "A + {#x#} \<subset># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
haftmann@62430
   546
  by (rule mset_le_insertD) simp
haftmann@62430
   547
haftmann@62430
   548
lemma mset_less_of_empty[simp]: "A \<subset># {#} \<longleftrightarrow> False"
Mathias@60397
   549
  by (auto simp add: subseteq_mset_def subset_mset_def multiset_eq_iff)
Mathias@60397
   550
haftmann@62430
   551
lemma empty_le [simp]: "{#} \<subseteq># A"
nipkow@55808
   552
  unfolding mset_le_exists_conv by auto
haftmann@62430
   553
 
haftmann@62430
   554
lemma insert_subset_eq_iff:
haftmann@62430
   555
  "{#a#} + A \<subseteq># B \<longleftrightarrow> a \<in># B \<and> A \<subseteq># B - {#a#}"
haftmann@62430
   556
  using le_diff_conv2 [of "Suc 0" "count B a" "count A a"]
haftmann@62430
   557
  apply (auto simp add: subseteq_mset_def not_in_iff Suc_le_eq)
haftmann@62430
   558
  apply (rule ccontr)
haftmann@62430
   559
  apply (auto simp add: not_in_iff)
haftmann@62430
   560
  done
haftmann@62430
   561
haftmann@62430
   562
lemma insert_union_subset_iff:
haftmann@62430
   563
  "{#a#} + A \<subset># B \<longleftrightarrow> a \<in># B \<and> A \<subset># B - {#a#}"
haftmann@62430
   564
  by (auto simp add: insert_subset_eq_iff subset_mset_def insert_DiffM)
haftmann@62430
   565
haftmann@62430
   566
lemma subset_eq_diff_conv:
haftmann@62430
   567
  "A - C \<subseteq># B \<longleftrightarrow> A \<subseteq># B + C"
haftmann@62430
   568
  by (simp add: subseteq_mset_def le_diff_conv)
haftmann@62430
   569
haftmann@62430
   570
lemma le_empty [simp]: "M \<subseteq># {#} \<longleftrightarrow> M = {#}"
haftmann@62430
   571
  unfolding mset_le_exists_conv by auto
haftmann@62430
   572
haftmann@62430
   573
lemma multi_psub_of_add_self[simp]: "A \<subset># A + {#x#}"
Mathias@60397
   574
  by (auto simp: subset_mset_def subseteq_mset_def)
Mathias@60397
   575
haftmann@62430
   576
lemma multi_psub_self[simp]: "(A::'a multiset) \<subset># A = False"
haftmann@35268
   577
  by simp
haftmann@34943
   578
haftmann@62430
   579
lemma mset_less_add_bothsides: "N + {#x#} \<subset># M + {#x#} \<Longrightarrow> N \<subset># M"
Mathias@60397
   580
  by (fact subset_mset.add_less_imp_less_right)
haftmann@35268
   581
haftmann@62430
   582
lemma mset_less_empty_nonempty: "{#} \<subset># S \<longleftrightarrow> S \<noteq> {#}"
hoelzl@62378
   583
  by (fact subset_mset.zero_less_iff_neq_zero)
haftmann@35268
   584
haftmann@62430
   585
lemma mset_less_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} \<subset># B"
haftmann@62430
   586
  by (auto simp: subset_mset_def elim: mset_add)
haftmann@35268
   587
haftmann@35268
   588
wenzelm@60500
   589
subsubsection \<open>Intersection\<close>
haftmann@35268
   590
Mathias@60397
   591
definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
Mathias@60397
   592
  multiset_inter_def: "inf_subset_mset A B = A - (A - B)"
Mathias@60397
   593
haftmann@62430
   594
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<subseteq>#" "op \<subset>#"
wenzelm@46921
   595
proof -
wenzelm@60678
   596
  have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat
wenzelm@60678
   597
    by arith
haftmann@62430
   598
  show "class.semilattice_inf op #\<inter> op \<subseteq># op \<subset>#"
wenzelm@60678
   599
    by standard (auto simp add: multiset_inter_def subseteq_mset_def)
haftmann@35268
   600
qed
wenzelm@62837
   601
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
haftmann@34943
   602
haftmann@41069
   603
lemma multiset_inter_count [simp]:
wenzelm@60606
   604
  fixes A B :: "'a multiset"
wenzelm@60606
   605
  shows "count (A #\<inter> B) x = min (count A x) (count B x)"
bulwahn@47429
   606
  by (simp add: multiset_inter_def)
haftmann@35268
   607
haftmann@62430
   608
lemma set_mset_inter [simp]:
haftmann@62430
   609
  "set_mset (A #\<inter> B) = set_mset A \<inter> set_mset B"
haftmann@62430
   610
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] multiset_inter_count) simp
haftmann@62430
   611
haftmann@62430
   612
lemma diff_intersect_left_idem [simp]:
haftmann@62430
   613
  "M - M #\<inter> N = M - N"
haftmann@62430
   614
  by (simp add: multiset_eq_iff min_def)
haftmann@62430
   615
haftmann@62430
   616
lemma diff_intersect_right_idem [simp]:
haftmann@62430
   617
  "M - N #\<inter> M = M - N"
haftmann@62430
   618
  by (simp add: multiset_eq_iff min_def)
haftmann@62430
   619
haftmann@35268
   620
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
wenzelm@46730
   621
  by (rule multiset_eqI) auto
haftmann@34943
   622
haftmann@35268
   623
lemma multiset_union_diff_commute:
haftmann@35268
   624
  assumes "B #\<inter> C = {#}"
haftmann@35268
   625
  shows "A + B - C = A - C + B"
nipkow@39302
   626
proof (rule multiset_eqI)
haftmann@35268
   627
  fix x
haftmann@35268
   628
  from assms have "min (count B x) (count C x) = 0"
wenzelm@46730
   629
    by (auto simp add: multiset_eq_iff)
haftmann@35268
   630
  then have "count B x = 0 \<or> count C x = 0"
haftmann@62430
   631
    unfolding min_def by (auto split: if_splits)
haftmann@35268
   632
  then show "count (A + B - C) x = count (A - C + B) x"
haftmann@35268
   633
    by auto
haftmann@35268
   634
qed
haftmann@35268
   635
haftmann@62430
   636
lemma disjunct_not_in:
haftmann@62430
   637
  "A #\<inter> B = {#} \<longleftrightarrow> (\<forall>a. a \<notin># A \<or> a \<notin># B)" (is "?P \<longleftrightarrow> ?Q")
haftmann@62430
   638
proof
haftmann@62430
   639
  assume ?P
haftmann@62430
   640
  show ?Q
haftmann@62430
   641
  proof
haftmann@62430
   642
    fix a
haftmann@62430
   643
    from \<open>?P\<close> have "min (count A a) (count B a) = 0"
haftmann@62430
   644
      by (simp add: multiset_eq_iff)
haftmann@62430
   645
    then have "count A a = 0 \<or> count B a = 0"
haftmann@62430
   646
      by (cases "count A a \<le> count B a") (simp_all add: min_def)
haftmann@62430
   647
    then show "a \<notin># A \<or> a \<notin># B"
haftmann@62430
   648
      by (simp add: not_in_iff)
haftmann@62430
   649
  qed
haftmann@62430
   650
next
haftmann@62430
   651
  assume ?Q
haftmann@62430
   652
  show ?P
haftmann@62430
   653
  proof (rule multiset_eqI)
haftmann@62430
   654
    fix a
haftmann@62430
   655
    from \<open>?Q\<close> have "count A a = 0 \<or> count B a = 0"
haftmann@62430
   656
      by (auto simp add: not_in_iff)
haftmann@62430
   657
    then show "count (A #\<inter> B) a = count {#} a"
haftmann@62430
   658
      by auto
haftmann@62430
   659
  qed
haftmann@62430
   660
qed
haftmann@62430
   661
wenzelm@60606
   662
lemma empty_inter [simp]: "{#} #\<inter> M = {#}"
haftmann@51600
   663
  by (simp add: multiset_eq_iff)
haftmann@51600
   664
wenzelm@60606
   665
lemma inter_empty [simp]: "M #\<inter> {#} = {#}"
haftmann@51600
   666
  by (simp add: multiset_eq_iff)
haftmann@51600
   667
wenzelm@60606
   668
lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
haftmann@62430
   669
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@51600
   670
wenzelm@60606
   671
lemma inter_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
haftmann@62430
   672
  by (auto simp add: multiset_eq_iff elim: mset_add)
haftmann@51600
   673
wenzelm@60606
   674
lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
haftmann@62430
   675
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@51600
   676
wenzelm@60606
   677
lemma inter_add_right2: "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
haftmann@62430
   678
  by (auto simp add: multiset_eq_iff elim: mset_add)
haftmann@62430
   679
haftmann@62430
   680
lemma disjunct_set_mset_diff:
haftmann@62430
   681
  assumes "M #\<inter> N = {#}"
haftmann@62430
   682
  shows "set_mset (M - N) = set_mset M"
haftmann@62430
   683
proof (rule set_eqI)
haftmann@62430
   684
  fix a
haftmann@62430
   685
  from assms have "a \<notin># M \<or> a \<notin># N"
haftmann@62430
   686
    by (simp add: disjunct_not_in)
haftmann@62430
   687
  then show "a \<in># M - N \<longleftrightarrow> a \<in># M"
haftmann@62430
   688
    by (auto dest: in_diffD) (simp add: in_diff_count not_in_iff)
haftmann@62430
   689
qed
haftmann@62430
   690
haftmann@62430
   691
lemma at_most_one_mset_mset_diff:
haftmann@62430
   692
  assumes "a \<notin># M - {#a#}"
haftmann@62430
   693
  shows "set_mset (M - {#a#}) = set_mset M - {a}"
haftmann@62430
   694
  using assms by (auto simp add: not_in_iff in_diff_count set_eq_iff)
haftmann@62430
   695
haftmann@62430
   696
lemma more_than_one_mset_mset_diff:
haftmann@62430
   697
  assumes "a \<in># M - {#a#}"
haftmann@62430
   698
  shows "set_mset (M - {#a#}) = set_mset M"
haftmann@62430
   699
proof (rule set_eqI)
haftmann@62430
   700
  fix b
haftmann@62430
   701
  have "Suc 0 < count M b \<Longrightarrow> count M b > 0" by arith
haftmann@62430
   702
  then show "b \<in># M - {#a#} \<longleftrightarrow> b \<in># M"
haftmann@62430
   703
    using assms by (auto simp add: in_diff_count)
haftmann@62430
   704
qed
haftmann@62430
   705
haftmann@62430
   706
lemma inter_iff:
haftmann@62430
   707
  "a \<in># A #\<inter> B \<longleftrightarrow> a \<in># A \<and> a \<in># B"
haftmann@62430
   708
  by simp
haftmann@62430
   709
haftmann@62430
   710
lemma inter_union_distrib_left:
haftmann@62430
   711
  "A #\<inter> B + C = (A + C) #\<inter> (B + C)"
haftmann@62430
   712
  by (simp add: multiset_eq_iff min_add_distrib_left)
haftmann@62430
   713
haftmann@62430
   714
lemma inter_union_distrib_right:
haftmann@62430
   715
  "C + A #\<inter> B = (C + A) #\<inter> (C + B)"
haftmann@62430
   716
  using inter_union_distrib_left [of A B C] by (simp add: ac_simps)
haftmann@62430
   717
haftmann@62430
   718
lemma inter_subset_eq_union:
haftmann@62430
   719
  "A #\<inter> B \<subseteq># A + B"
haftmann@62430
   720
  by (auto simp add: subseteq_mset_def)
haftmann@51600
   721
haftmann@35268
   722
wenzelm@60500
   723
subsubsection \<open>Bounded union\<close>
wenzelm@60678
   724
wenzelm@60678
   725
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "#\<union>" 70)
wenzelm@62837
   726
  where "sup_subset_mset A B = A + (B - A)" \<comment> \<open>FIXME irregular fact name\<close>
haftmann@62430
   727
haftmann@62430
   728
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<subseteq>#" "op \<subset>#"
haftmann@51623
   729
proof -
wenzelm@60678
   730
  have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat
wenzelm@60678
   731
    by arith
haftmann@62430
   732
  show "class.semilattice_sup op #\<union> op \<subseteq># op \<subset>#"
wenzelm@60678
   733
    by standard (auto simp add: sup_subset_mset_def subseteq_mset_def)
haftmann@51623
   734
qed
wenzelm@62837
   735
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
wenzelm@62837
   736
wenzelm@62837
   737
lemma sup_subset_mset_count [simp]: \<comment> \<open>FIXME irregular fact name\<close>
haftmann@62430
   738
  "count (A #\<union> B) x = max (count A x) (count B x)"
Mathias@60397
   739
  by (simp add: sup_subset_mset_def)
haftmann@51623
   740
haftmann@62430
   741
lemma set_mset_sup [simp]:
haftmann@62430
   742
  "set_mset (A #\<union> B) = set_mset A \<union> set_mset B"
haftmann@62430
   743
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] sup_subset_mset_count)
haftmann@62430
   744
    (auto simp add: not_in_iff elim: mset_add)
haftmann@62430
   745
wenzelm@60606
   746
lemma empty_sup [simp]: "{#} #\<union> M = M"
haftmann@51623
   747
  by (simp add: multiset_eq_iff)
haftmann@51623
   748
wenzelm@60606
   749
lemma sup_empty [simp]: "M #\<union> {#} = M"
haftmann@51623
   750
  by (simp add: multiset_eq_iff)
haftmann@51623
   751
haftmann@62430
   752
lemma sup_union_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
haftmann@62430
   753
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@62430
   754
haftmann@62430
   755
lemma sup_union_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
haftmann@51623
   756
  by (simp add: multiset_eq_iff)
haftmann@51623
   757
haftmann@62430
   758
lemma sup_union_right1: "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
haftmann@62430
   759
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@62430
   760
haftmann@62430
   761
lemma sup_union_right2: "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
haftmann@51623
   762
  by (simp add: multiset_eq_iff)
haftmann@51623
   763
haftmann@62430
   764
lemma sup_union_distrib_left:
haftmann@62430
   765
  "A #\<union> B + C = (A + C) #\<union> (B + C)"
haftmann@62430
   766
  by (simp add: multiset_eq_iff max_add_distrib_left)
haftmann@62430
   767
haftmann@62430
   768
lemma union_sup_distrib_right:
haftmann@62430
   769
  "C + A #\<union> B = (C + A) #\<union> (C + B)"
haftmann@62430
   770
  using sup_union_distrib_left [of A B C] by (simp add: ac_simps)
haftmann@62430
   771
haftmann@62430
   772
lemma union_diff_inter_eq_sup:
haftmann@62430
   773
  "A + B - A #\<inter> B = A #\<union> B"
haftmann@62430
   774
  by (auto simp add: multiset_eq_iff)
haftmann@62430
   775
haftmann@62430
   776
lemma union_diff_sup_eq_inter:
haftmann@62430
   777
  "A + B - A #\<union> B = A #\<inter> B"
haftmann@62430
   778
  by (auto simp add: multiset_eq_iff)
haftmann@62430
   779
haftmann@51623
   780
wenzelm@60500
   781
subsubsection \<open>Subset is an order\<close>
haftmann@62430
   782
Mathias@60397
   783
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto
haftmann@51623
   784
haftmann@62430
   785
wenzelm@60500
   786
subsubsection \<open>Filter (with comprehension syntax)\<close>
wenzelm@60500
   787
wenzelm@60500
   788
text \<open>Multiset comprehension\<close>
haftmann@41069
   789
nipkow@59998
   790
lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
nipkow@59998
   791
is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
bulwahn@47429
   792
by (rule filter_preserves_multiset)
haftmann@35268
   793
haftmann@62430
   794
syntax (ASCII)
haftmann@62430
   795
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
haftmann@62430
   796
syntax
haftmann@62430
   797
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
haftmann@62430
   798
translations
haftmann@62430
   799
  "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
haftmann@62430
   800
haftmann@62430
   801
lemma count_filter_mset [simp]:
haftmann@62430
   802
  "count (filter_mset P M) a = (if P a then count M a else 0)"
nipkow@59998
   803
  by (simp add: filter_mset.rep_eq)
nipkow@59998
   804
haftmann@62430
   805
lemma set_mset_filter [simp]:
haftmann@62430
   806
  "set_mset (filter_mset P M) = {a \<in> set_mset M. P a}"
haftmann@62430
   807
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_filter_mset) simp
haftmann@62430
   808
wenzelm@60606
   809
lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
nipkow@59998
   810
  by (rule multiset_eqI) simp
nipkow@59998
   811
wenzelm@60606
   812
lemma filter_single_mset [simp]: "filter_mset P {#x#} = (if P x then {#x#} else {#})"
nipkow@39302
   813
  by (rule multiset_eqI) simp
haftmann@35268
   814
wenzelm@60606
   815
lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N"
haftmann@41069
   816
  by (rule multiset_eqI) simp
haftmann@41069
   817
wenzelm@60606
   818
lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N"
nipkow@39302
   819
  by (rule multiset_eqI) simp
haftmann@35268
   820
wenzelm@60606
   821
lemma filter_inter_mset [simp]: "filter_mset P (M #\<inter> N) = filter_mset P M #\<inter> filter_mset P N"
haftmann@41069
   822
  by (rule multiset_eqI) simp
haftmann@41069
   823
haftmann@62430
   824
lemma multiset_filter_subset[simp]: "filter_mset f M \<subseteq># M"
Mathias@60397
   825
  by (simp add: mset_less_eqI)
Mathias@60397
   826
wenzelm@60606
   827
lemma multiset_filter_mono:
haftmann@62430
   828
  assumes "A \<subseteq># B"
haftmann@62430
   829
  shows "filter_mset f A \<subseteq># filter_mset f B"
blanchet@58035
   830
proof -
blanchet@58035
   831
  from assms[unfolded mset_le_exists_conv]
blanchet@58035
   832
  obtain C where B: "B = A + C" by auto
blanchet@58035
   833
  show ?thesis unfolding B by auto
blanchet@58035
   834
qed
blanchet@58035
   835
haftmann@62430
   836
lemma filter_mset_eq_conv:
haftmann@62430
   837
  "filter_mset P M = N \<longleftrightarrow> N \<subseteq># M \<and> (\<forall>b\<in>#N. P b) \<and> (\<forall>a\<in>#M - N. \<not> P a)" (is "?P \<longleftrightarrow> ?Q")
haftmann@62430
   838
proof
haftmann@62430
   839
  assume ?P then show ?Q by auto (simp add: multiset_eq_iff in_diff_count)
haftmann@62430
   840
next
haftmann@62430
   841
  assume ?Q
haftmann@62430
   842
  then obtain Q where M: "M = N + Q"
haftmann@62430
   843
    by (auto simp add: mset_le_exists_conv)
haftmann@62430
   844
  then have MN: "M - N = Q" by simp
haftmann@62430
   845
  show ?P
haftmann@62430
   846
  proof (rule multiset_eqI)
haftmann@62430
   847
    fix a
haftmann@62430
   848
    from \<open>?Q\<close> MN have *: "\<not> P a \<Longrightarrow> a \<notin># N" "P a \<Longrightarrow> a \<notin># Q"
haftmann@62430
   849
      by auto
haftmann@62430
   850
    show "count (filter_mset P M) a = count N a"
haftmann@62430
   851
    proof (cases "a \<in># M")
haftmann@62430
   852
      case True
haftmann@62430
   853
      with * show ?thesis
haftmann@62430
   854
        by (simp add: not_in_iff M)
haftmann@62430
   855
    next
haftmann@62430
   856
      case False then have "count M a = 0"
haftmann@62430
   857
        by (simp add: not_in_iff)
haftmann@62430
   858
      with M show ?thesis by simp
haftmann@62430
   859
    qed 
haftmann@62430
   860
  qed
haftmann@62430
   861
qed
blanchet@59813
   862
blanchet@59813
   863
wenzelm@60500
   864
subsubsection \<open>Size\<close>
wenzelm@10249
   865
blanchet@56656
   866
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
blanchet@56656
   867
blanchet@56656
   868
lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a"
blanchet@56656
   869
  by (auto simp: wcount_def add_mult_distrib)
blanchet@56656
   870
blanchet@56656
   871
definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
nipkow@60495
   872
  "size_multiset f M = setsum (wcount f M) (set_mset M)"
blanchet@56656
   873
blanchet@56656
   874
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
blanchet@56656
   875
wenzelm@60606
   876
instantiation multiset :: (type) size
wenzelm@60606
   877
begin
wenzelm@60606
   878
blanchet@56656
   879
definition size_multiset where
blanchet@56656
   880
  size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)"
haftmann@34943
   881
instance ..
wenzelm@60606
   882
haftmann@34943
   883
end
haftmann@34943
   884
blanchet@56656
   885
lemmas size_multiset_overloaded_eq =
blanchet@56656
   886
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
blanchet@56656
   887
blanchet@56656
   888
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
blanchet@56656
   889
by (simp add: size_multiset_def)
blanchet@56656
   890
haftmann@28708
   891
lemma size_empty [simp]: "size {#} = 0"
blanchet@56656
   892
by (simp add: size_multiset_overloaded_def)
blanchet@56656
   893
blanchet@56656
   894
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"
blanchet@56656
   895
by (simp add: size_multiset_eq)
wenzelm@10249
   896
haftmann@28708
   897
lemma size_single [simp]: "size {#b#} = 1"
blanchet@56656
   898
by (simp add: size_multiset_overloaded_def)
blanchet@56656
   899
blanchet@56656
   900
lemma setsum_wcount_Int:
nipkow@60495
   901
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A"
haftmann@62430
   902
  by (induct rule: finite_induct)
haftmann@62430
   903
    (simp_all add: Int_insert_left wcount_def count_eq_zero_iff)
blanchet@56656
   904
blanchet@56656
   905
lemma size_multiset_union [simp]:
blanchet@56656
   906
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
haftmann@57418
   907
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union)
blanchet@56656
   908
apply (subst Int_commute)
blanchet@56656
   909
apply (simp add: setsum_wcount_Int)
nipkow@26178
   910
done
wenzelm@10249
   911
haftmann@28708
   912
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
blanchet@56656
   913
by (auto simp add: size_multiset_overloaded_def)
blanchet@56656
   914
haftmann@62430
   915
lemma size_multiset_eq_0_iff_empty [iff]:
haftmann@62430
   916
  "size_multiset f M = 0 \<longleftrightarrow> M = {#}"
haftmann@62430
   917
  by (auto simp add: size_multiset_eq count_eq_zero_iff)
wenzelm@10249
   918
wenzelm@17161
   919
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
blanchet@56656
   920
by (auto simp add: size_multiset_overloaded_def)
nipkow@26016
   921
nipkow@26016
   922
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
nipkow@26178
   923
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
wenzelm@10249
   924
wenzelm@60607
   925
lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M"
blanchet@56656
   926
apply (unfold size_multiset_overloaded_eq)
nipkow@26178
   927
apply (drule setsum_SucD)
nipkow@26178
   928
apply auto
nipkow@26178
   929
done
wenzelm@10249
   930
haftmann@34943
   931
lemma size_eq_Suc_imp_eq_union:
haftmann@34943
   932
  assumes "size M = Suc n"
haftmann@34943
   933
  shows "\<exists>a N. M = N + {#a#}"
haftmann@34943
   934
proof -
haftmann@34943
   935
  from assms obtain a where "a \<in># M"
haftmann@34943
   936
    by (erule size_eq_Suc_imp_elem [THEN exE])
haftmann@34943
   937
  then have "M = M - {#a#} + {#a#}" by simp
haftmann@34943
   938
  then show ?thesis by blast
nipkow@23611
   939
qed
kleing@15869
   940
wenzelm@60606
   941
lemma size_mset_mono:
wenzelm@60606
   942
  fixes A B :: "'a multiset"
haftmann@62430
   943
  assumes "A \<subseteq># B"
wenzelm@60606
   944
  shows "size A \<le> size B"
nipkow@59949
   945
proof -
nipkow@59949
   946
  from assms[unfolded mset_le_exists_conv]
nipkow@59949
   947
  obtain C where B: "B = A + C" by auto
wenzelm@60606
   948
  show ?thesis unfolding B by (induct C) auto
nipkow@59949
   949
qed
nipkow@59949
   950
nipkow@59998
   951
lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M"
nipkow@59949
   952
by (rule size_mset_mono[OF multiset_filter_subset])
nipkow@59949
   953
nipkow@59949
   954
lemma size_Diff_submset:
haftmann@62430
   955
  "M \<subseteq># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)"
nipkow@59949
   956
by (metis add_diff_cancel_left' size_union mset_le_exists_conv)
nipkow@26016
   957
haftmann@62430
   958
wenzelm@60500
   959
subsection \<open>Induction and case splits\<close>
wenzelm@10249
   960
wenzelm@18258
   961
theorem multiset_induct [case_names empty add, induct type: multiset]:
huffman@48009
   962
  assumes empty: "P {#}"
huffman@48009
   963
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
huffman@48009
   964
  shows "P M"
huffman@48009
   965
proof (induct n \<equiv> "size M" arbitrary: M)
huffman@48009
   966
  case 0 thus "P M" by (simp add: empty)
huffman@48009
   967
next
huffman@48009
   968
  case (Suc k)
huffman@48009
   969
  obtain N x where "M = N + {#x#}"
wenzelm@60500
   970
    using \<open>Suc k = size M\<close> [symmetric]
huffman@48009
   971
    using size_eq_Suc_imp_eq_union by fast
huffman@48009
   972
  with Suc add show "P M" by simp
wenzelm@10249
   973
qed
wenzelm@10249
   974
kleing@25610
   975
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
nipkow@26178
   976
by (induct M) auto
kleing@25610
   977
wenzelm@55913
   978
lemma multiset_cases [cases type]:
wenzelm@55913
   979
  obtains (empty) "M = {#}"
wenzelm@55913
   980
    | (add) N x where "M = N + {#x#}"
wenzelm@63092
   981
  by (induct M) simp_all
kleing@25610
   982
haftmann@34943
   983
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
haftmann@34943
   984
by (cases "B = {#}") (auto dest: multi_member_split)
haftmann@34943
   985
wenzelm@60607
   986
lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}"
nipkow@39302
   987
apply (subst multiset_eq_iff)
nipkow@26178
   988
apply auto
nipkow@26178
   989
done
wenzelm@10249
   990
haftmann@62430
   991
lemma mset_less_size: "(A::'a multiset) \<subset># B \<Longrightarrow> size A < size B"
haftmann@34943
   992
proof (induct A arbitrary: B)
haftmann@34943
   993
  case (empty M)
haftmann@34943
   994
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
blanchet@58425
   995
  then obtain M' x where "M = M' + {#x#}"
haftmann@34943
   996
    by (blast dest: multi_nonempty_split)
haftmann@34943
   997
  then show ?case by simp
haftmann@34943
   998
next
haftmann@34943
   999
  case (add S x T)
haftmann@62430
  1000
  have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact
haftmann@62430
  1001
  have SxsubT: "S + {#x#} \<subset># T" by fact
haftmann@62430
  1002
  then have "x \<in># T" and "S \<subset># T"
haftmann@62430
  1003
    by (auto dest: mset_less_insertD)
blanchet@58425
  1004
  then obtain T' where T: "T = T' + {#x#}"
haftmann@34943
  1005
    by (blast dest: multi_member_split)
haftmann@62430
  1006
  then have "S \<subset># T'" using SxsubT
haftmann@34943
  1007
    by (blast intro: mset_less_add_bothsides)
haftmann@34943
  1008
  then have "size S < size T'" using IH by simp
haftmann@34943
  1009
  then show ?case using T by simp
haftmann@34943
  1010
qed
haftmann@34943
  1011
nipkow@59949
  1012
lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}"
nipkow@59949
  1013
by (cases M) auto
nipkow@59949
  1014
haftmann@62430
  1015
wenzelm@60500
  1016
subsubsection \<open>Strong induction and subset induction for multisets\<close>
wenzelm@60500
  1017
wenzelm@60500
  1018
text \<open>Well-foundedness of strict subset relation\<close>
haftmann@58098
  1019
haftmann@62430
  1020
lemma wf_less_mset_rel: "wf {(M, N :: 'a multiset). M \<subset># N}"
haftmann@34943
  1021
apply (rule wf_measure [THEN wf_subset, where f1=size])
haftmann@34943
  1022
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
haftmann@34943
  1023
done
haftmann@34943
  1024
haftmann@34943
  1025
lemma full_multiset_induct [case_names less]:
haftmann@62430
  1026
assumes ih: "\<And>B. \<forall>(A::'a multiset). A \<subset># B \<longrightarrow> P A \<Longrightarrow> P B"
haftmann@34943
  1027
shows "P B"
haftmann@58098
  1028
apply (rule wf_less_mset_rel [THEN wf_induct])
haftmann@58098
  1029
apply (rule ih, auto)
haftmann@34943
  1030
done
haftmann@34943
  1031
haftmann@34943
  1032
lemma multi_subset_induct [consumes 2, case_names empty add]:
haftmann@62430
  1033
  assumes "F \<subseteq># A"
wenzelm@60606
  1034
    and empty: "P {#}"
wenzelm@60606
  1035
    and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
wenzelm@60606
  1036
  shows "P F"
haftmann@34943
  1037
proof -
haftmann@62430
  1038
  from \<open>F \<subseteq># A\<close>
haftmann@34943
  1039
  show ?thesis
haftmann@34943
  1040
  proof (induct F)
haftmann@34943
  1041
    show "P {#}" by fact
haftmann@34943
  1042
  next
haftmann@34943
  1043
    fix x F
haftmann@62430
  1044
    assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "F + {#x#} \<subseteq># A"
haftmann@34943
  1045
    show "P (F + {#x#})"
haftmann@34943
  1046
    proof (rule insert)
haftmann@34943
  1047
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
haftmann@62430
  1048
      from i have "F \<subseteq># A" by (auto dest: mset_le_insertD)
haftmann@34943
  1049
      with P show "P F" .
haftmann@34943
  1050
    qed
haftmann@34943
  1051
  qed
haftmann@34943
  1052
qed
wenzelm@26145
  1053
wenzelm@17161
  1054
wenzelm@60500
  1055
subsection \<open>The fold combinator\<close>
huffman@48023
  1056
nipkow@59998
  1057
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
huffman@48023
  1058
where
nipkow@60495
  1059
  "fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)"
huffman@48023
  1060
wenzelm@60606
  1061
lemma fold_mset_empty [simp]: "fold_mset f s {#} = s"
nipkow@59998
  1062
  by (simp add: fold_mset_def)
huffman@48023
  1063
huffman@48023
  1064
context comp_fun_commute
huffman@48023
  1065
begin
huffman@48023
  1066
wenzelm@60606
  1067
lemma fold_mset_insert: "fold_mset f s (M + {#x#}) = f x (fold_mset f s M)"
haftmann@49822
  1068
proof -
haftmann@49822
  1069
  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
haftmann@49822
  1070
    by (fact comp_fun_commute_funpow)
haftmann@49822
  1071
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
haftmann@49822
  1072
    by (fact comp_fun_commute_funpow)
haftmann@49822
  1073
  show ?thesis
nipkow@60495
  1074
  proof (cases "x \<in> set_mset M")
haftmann@49822
  1075
    case False
haftmann@62430
  1076
    then have *: "count (M + {#x#}) x = 1"
haftmann@62430
  1077
      by (simp add: not_in_iff)
nipkow@60495
  1078
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_mset M) =
nipkow@60495
  1079
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)"
haftmann@49822
  1080
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
  1081
    with False * show ?thesis
nipkow@59998
  1082
      by (simp add: fold_mset_def del: count_union)
huffman@48023
  1083
  next
haftmann@49822
  1084
    case True
wenzelm@63040
  1085
    define N where "N = set_mset M - {x}"
nipkow@60495
  1086
    from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto
haftmann@49822
  1087
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
haftmann@49822
  1088
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
haftmann@49822
  1089
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
nipkow@59998
  1090
    with * show ?thesis by (simp add: fold_mset_def del: count_union) simp
huffman@48023
  1091
  qed
huffman@48023
  1092
qed
huffman@48023
  1093
wenzelm@60606
  1094
corollary fold_mset_single [simp]: "fold_mset f s {#x#} = f x s"
haftmann@49822
  1095
proof -
nipkow@59998
  1096
  have "fold_mset f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
haftmann@49822
  1097
  then show ?thesis by simp
haftmann@49822
  1098
qed
huffman@48023
  1099
wenzelm@60606
  1100
lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M"
haftmann@49822
  1101
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
huffman@48023
  1102
wenzelm@60606
  1103
lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N"
haftmann@49822
  1104
proof (induct M)
huffman@48023
  1105
  case empty then show ?case by simp
huffman@48023
  1106
next
haftmann@49822
  1107
  case (add M x)
haftmann@49822
  1108
  have "M + {#x#} + N = (M + N) + {#x#}"
haftmann@57514
  1109
    by (simp add: ac_simps)
haftmann@51548
  1110
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
huffman@48023
  1111
qed
huffman@48023
  1112
huffman@48023
  1113
lemma fold_mset_fusion:
huffman@48023
  1114
  assumes "comp_fun_commute g"
wenzelm@60606
  1115
    and *: "\<And>x y. h (g x y) = f x (h y)"
wenzelm@60606
  1116
  shows "h (fold_mset g w A) = fold_mset f (h w) A"
huffman@48023
  1117
proof -
huffman@48023
  1118
  interpret comp_fun_commute g by (fact assms)
wenzelm@60606
  1119
  from * show ?thesis by (induct A) auto
huffman@48023
  1120
qed
huffman@48023
  1121
huffman@48023
  1122
end
huffman@48023
  1123
wenzelm@60500
  1124
text \<open>
huffman@48023
  1125
  A note on code generation: When defining some function containing a
nipkow@59998
  1126
  subterm @{term "fold_mset F"}, code generation is not automatic. When
wenzelm@61585
  1127
  interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the
nipkow@59998
  1128
  would be code thms for @{const fold_mset} become thms like
wenzelm@61585
  1129
  @{term "fold_mset F z {#} = z"} where \<open>F\<close> is not a pattern but
huffman@48023
  1130
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
wenzelm@61585
  1131
  constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below.
wenzelm@60500
  1132
\<close>
wenzelm@60500
  1133
wenzelm@60500
  1134
wenzelm@60500
  1135
subsection \<open>Image\<close>
huffman@48023
  1136
huffman@48023
  1137
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
wenzelm@60607
  1138
  "image_mset f = fold_mset (plus \<circ> single \<circ> f) {#}"
wenzelm@60607
  1139
wenzelm@60607
  1140
lemma comp_fun_commute_mset_image: "comp_fun_commute (plus \<circ> single \<circ> f)"
haftmann@49823
  1141
proof
haftmann@57514
  1142
qed (simp add: ac_simps fun_eq_iff)
huffman@48023
  1143
huffman@48023
  1144
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
haftmann@49823
  1145
  by (simp add: image_mset_def)
huffman@48023
  1146
huffman@48023
  1147
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
haftmann@49823
  1148
proof -
wenzelm@60607
  1149
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
haftmann@49823
  1150
    by (fact comp_fun_commute_mset_image)
haftmann@49823
  1151
  show ?thesis by (simp add: image_mset_def)
haftmann@49823
  1152
qed
huffman@48023
  1153
wenzelm@60606
  1154
lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N"
haftmann@49823
  1155
proof -
wenzelm@60607
  1156
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
haftmann@49823
  1157
    by (fact comp_fun_commute_mset_image)
haftmann@57514
  1158
  show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps)
haftmann@49823
  1159
qed
haftmann@49823
  1160
wenzelm@60606
  1161
corollary image_mset_insert: "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
haftmann@49823
  1162
  by simp
huffman@48023
  1163
wenzelm@60606
  1164
lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)"
haftmann@49823
  1165
  by (induct M) simp_all
huffman@48040
  1166
wenzelm@60606
  1167
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
haftmann@49823
  1168
  by (induct M) simp_all
huffman@48023
  1169
wenzelm@60606
  1170
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
haftmann@49823
  1171
  by (cases M) auto
huffman@48023
  1172
eberlm@63099
  1173
lemma image_mset_If:
eberlm@63099
  1174
  "image_mset (\<lambda>x. if P x then f x else g x) A = 
eberlm@63099
  1175
     image_mset f (filter_mset P A) + image_mset g (filter_mset (\<lambda>x. \<not>P x) A)"
eberlm@63099
  1176
  by (induction A) (auto simp: add_ac)
eberlm@63099
  1177
eberlm@63099
  1178
lemma image_mset_Diff: 
eberlm@63099
  1179
  assumes "B \<subseteq># A"
eberlm@63099
  1180
  shows   "image_mset f (A - B) = image_mset f A - image_mset f B"
eberlm@63099
  1181
proof -
eberlm@63099
  1182
  have "image_mset f (A - B + B) = image_mset f (A - B) + image_mset f B"
eberlm@63099
  1183
    by simp
eberlm@63099
  1184
  also from assms have "A - B + B = A"
eberlm@63099
  1185
    by (simp add: subset_mset.diff_add) 
eberlm@63099
  1186
  finally show ?thesis by simp
eberlm@63099
  1187
qed
eberlm@63099
  1188
eberlm@63099
  1189
lemma count_image_mset: 
eberlm@63099
  1190
  "count (image_mset f A) x = (\<Sum>y\<in>f -` {x} \<inter> set_mset A. count A y)"
eberlm@63099
  1191
  by (induction A)
eberlm@63099
  1192
     (auto simp: setsum.distrib setsum.delta' intro!: setsum.mono_neutral_left)
eberlm@63099
  1193
eberlm@63099
  1194
wenzelm@61955
  1195
syntax (ASCII)
wenzelm@61955
  1196
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
huffman@48023
  1197
syntax
wenzelm@61955
  1198
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
blanchet@59813
  1199
translations
wenzelm@61955
  1200
  "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
wenzelm@61955
  1201
wenzelm@61955
  1202
syntax (ASCII)
wenzelm@61955
  1203
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
huffman@48023
  1204
syntax
wenzelm@61955
  1205
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
blanchet@59813
  1206
translations
wenzelm@60606
  1207
  "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
blanchet@59813
  1208
wenzelm@60500
  1209
text \<open>
wenzelm@60607
  1210
  This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"}
wenzelm@60607
  1211
  but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source]
wenzelm@60607
  1212
  "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
wenzelm@60607
  1213
  @{term "{#x+x|x\<in>#M. x<c#}"}.
wenzelm@60500
  1214
\<close>
huffman@48023
  1215
nipkow@60495
  1216
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
haftmann@62430
  1217
by (metis set_image_mset)
blanchet@59813
  1218
blanchet@55467
  1219
functor image_mset: image_mset
huffman@48023
  1220
proof -
huffman@48023
  1221
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
huffman@48023
  1222
  proof
huffman@48023
  1223
    fix A
huffman@48023
  1224
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
huffman@48023
  1225
      by (induct A) simp_all
huffman@48023
  1226
  qed
huffman@48023
  1227
  show "image_mset id = id"
huffman@48023
  1228
  proof
huffman@48023
  1229
    fix A
huffman@48023
  1230
    show "image_mset id A = id A"
huffman@48023
  1231
      by (induct A) simp_all
huffman@48023
  1232
  qed
huffman@48023
  1233
qed
huffman@48023
  1234
blanchet@59813
  1235
declare
blanchet@59813
  1236
  image_mset.id [simp]
blanchet@59813
  1237
  image_mset.identity [simp]
blanchet@59813
  1238
blanchet@59813
  1239
lemma image_mset_id[simp]: "image_mset id x = x"
blanchet@59813
  1240
  unfolding id_def by auto
blanchet@59813
  1241
blanchet@59813
  1242
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
  1243
  by (induct M) auto
blanchet@59813
  1244
blanchet@59813
  1245
lemma image_mset_cong_pair:
blanchet@59813
  1246
  "(\<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
  1247
  by (metis image_mset_cong split_cong)
haftmann@49717
  1248
huffman@48023
  1249
wenzelm@60500
  1250
subsection \<open>Further conversions\<close>
haftmann@34943
  1251
nipkow@60515
  1252
primrec mset :: "'a list \<Rightarrow> 'a multiset" where
nipkow@60515
  1253
  "mset [] = {#}" |
nipkow@60515
  1254
  "mset (a # x) = mset x + {# a #}"
haftmann@34943
  1255
haftmann@37107
  1256
lemma in_multiset_in_set:
nipkow@60515
  1257
  "x \<in># mset xs \<longleftrightarrow> x \<in> set xs"
haftmann@37107
  1258
  by (induct xs) simp_all
haftmann@37107
  1259
nipkow@60515
  1260
lemma count_mset:
nipkow@60515
  1261
  "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@37107
  1262
  by (induct xs) simp_all
haftmann@37107
  1263
nipkow@60515
  1264
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
blanchet@59813
  1265
  by (induct x) auto
haftmann@34943
  1266
nipkow@60515
  1267
lemma mset_zero_iff_right[simp]: "({#} = mset x) = (x = [])"
haftmann@34943
  1268
by (induct x) auto
haftmann@34943
  1269
nipkow@60515
  1270
lemma set_mset_mset[simp]: "set_mset (mset x) = set x"
haftmann@34943
  1271
by (induct x) auto
haftmann@34943
  1272
haftmann@62430
  1273
lemma set_mset_comp_mset [simp]: "set_mset \<circ> mset = set"
haftmann@62430
  1274
  by (simp add: fun_eq_iff)
haftmann@34943
  1275
nipkow@60515
  1276
lemma size_mset [simp]: "size (mset xs) = length xs"
huffman@48012
  1277
  by (induct xs) simp_all
huffman@48012
  1278
wenzelm@60606
  1279
lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys"
haftmann@57514
  1280
  by (induct xs arbitrary: ys) (auto simp: ac_simps)
haftmann@34943
  1281
wenzelm@60607
  1282
lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
haftmann@40303
  1283
  by (induct xs) simp_all
haftmann@40303
  1284
nipkow@60515
  1285
lemma mset_rev [simp]:
nipkow@60515
  1286
  "mset (rev xs) = mset xs"
haftmann@40950
  1287
  by (induct xs) simp_all
haftmann@40950
  1288
nipkow@60515
  1289
lemma surj_mset: "surj mset"
haftmann@34943
  1290
apply (unfold surj_def)
haftmann@34943
  1291
apply (rule allI)
haftmann@34943
  1292
apply (rule_tac M = y in multiset_induct)
haftmann@34943
  1293
 apply auto
haftmann@34943
  1294
apply (rule_tac x = "x # xa" in exI)
haftmann@34943
  1295
apply auto
haftmann@34943
  1296
done
haftmann@34943
  1297
haftmann@34943
  1298
lemma distinct_count_atmost_1:
wenzelm@60606
  1299
  "distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))"
haftmann@62430
  1300
proof (induct x)
haftmann@62430
  1301
  case Nil then show ?case by simp
haftmann@62430
  1302
next
haftmann@62430
  1303
  case (Cons x xs) show ?case (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@62430
  1304
  proof
haftmann@62430
  1305
    assume ?lhs then show ?rhs using Cons by simp
haftmann@62430
  1306
  next
haftmann@62430
  1307
    assume ?rhs then have "x \<notin> set xs"
haftmann@62430
  1308
      by (simp split: if_splits)
haftmann@62430
  1309
    moreover from \<open>?rhs\<close> have "(\<forall>a. count (mset xs) a =
haftmann@62430
  1310
       (if a \<in> set xs then 1 else 0))"
haftmann@62430
  1311
      by (auto split: if_splits simp add: count_eq_zero_iff)
haftmann@62430
  1312
    ultimately show ?lhs using Cons by simp
haftmann@62430
  1313
  qed
haftmann@62430
  1314
qed
haftmann@62430
  1315
haftmann@62430
  1316
lemma mset_eq_setD:
haftmann@62430
  1317
  assumes "mset xs = mset ys"
haftmann@62430
  1318
  shows "set xs = set ys"
haftmann@62430
  1319
proof -
haftmann@62430
  1320
  from assms have "set_mset (mset xs) = set_mset (mset ys)"
haftmann@62430
  1321
    by simp
haftmann@62430
  1322
  then show ?thesis by simp
haftmann@62430
  1323
qed
haftmann@34943
  1324
nipkow@60515
  1325
lemma set_eq_iff_mset_eq_distinct:
haftmann@34943
  1326
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
nipkow@60515
  1327
    (set x = set y) = (mset x = mset y)"
nipkow@39302
  1328
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
haftmann@34943
  1329
nipkow@60515
  1330
lemma set_eq_iff_mset_remdups_eq:
nipkow@60515
  1331
   "(set x = set y) = (mset (remdups x) = mset (remdups y))"
haftmann@34943
  1332
apply (rule iffI)
nipkow@60515
  1333
apply (simp add: set_eq_iff_mset_eq_distinct[THEN iffD1])
haftmann@34943
  1334
apply (drule distinct_remdups [THEN distinct_remdups
nipkow@60515
  1335
      [THEN set_eq_iff_mset_eq_distinct [THEN iffD2]]])
haftmann@34943
  1336
apply simp
haftmann@34943
  1337
done
haftmann@34943
  1338
wenzelm@60606
  1339
lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs"
haftmann@57514
  1340
  by (induct xs) (auto simp: ac_simps)
haftmann@34943
  1341
wenzelm@60607
  1342
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls"
wenzelm@60678
  1343
proof (induct ls arbitrary: i)
wenzelm@60678
  1344
  case Nil
wenzelm@60678
  1345
  then show ?case by simp
wenzelm@60678
  1346
next
wenzelm@60678
  1347
  case Cons
wenzelm@60678
  1348
  then show ?case by (cases i) auto
wenzelm@60678
  1349
qed
haftmann@34943
  1350
wenzelm@60606
  1351
lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
wenzelm@60678
  1352
  by (induct xs) (auto simp add: multiset_eq_iff)
haftmann@34943
  1353
nipkow@60515
  1354
lemma mset_eq_length:
nipkow@60515
  1355
  assumes "mset xs = mset ys"
haftmann@37107
  1356
  shows "length xs = length ys"
nipkow@60515
  1357
  using assms by (metis size_mset)
nipkow@60515
  1358
nipkow@60515
  1359
lemma mset_eq_length_filter:
nipkow@60515
  1360
  assumes "mset xs = mset ys"
haftmann@39533
  1361
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
nipkow@60515
  1362
  using assms by (metis count_mset)
haftmann@39533
  1363
haftmann@45989
  1364
lemma fold_multiset_equiv:
haftmann@45989
  1365
  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
  1366
    and equiv: "mset xs = mset ys"
haftmann@49822
  1367
  shows "List.fold f xs = List.fold f ys"
wenzelm@60606
  1368
  using f equiv [symmetric]
wenzelm@46921
  1369
proof (induct xs arbitrary: ys)
wenzelm@60678
  1370
  case Nil
wenzelm@60678
  1371
  then show ?case by simp
haftmann@45989
  1372
next
haftmann@45989
  1373
  case (Cons x xs)
wenzelm@60678
  1374
  then have *: "set ys = set (x # xs)"
wenzelm@60678
  1375
    by (blast dest: mset_eq_setD)
blanchet@58425
  1376
  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
  1377
    by (rule Cons.prems(1)) (simp_all add: *)
wenzelm@60678
  1378
  moreover from * have "x \<in> set ys"
wenzelm@60678
  1379
    by simp
wenzelm@60678
  1380
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x"
wenzelm@60678
  1381
    by (fact fold_remove1_split)
wenzelm@60678
  1382
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)"
wenzelm@60678
  1383
    by (auto intro: Cons.hyps)
haftmann@45989
  1384
  ultimately show ?case by simp
haftmann@45989
  1385
qed
haftmann@45989
  1386
wenzelm@60606
  1387
lemma mset_insort [simp]: "mset (insort x xs) = mset xs + {#x#}"
haftmann@51548
  1388
  by (induct xs) (simp_all add: ac_simps)
haftmann@51548
  1389
wenzelm@60606
  1390
lemma mset_map: "mset (map f xs) = image_mset f (mset xs)"
haftmann@51600
  1391
  by (induct xs) simp_all
haftmann@51600
  1392
haftmann@61890
  1393
global_interpretation mset_set: folding "\<lambda>x M. {#x#} + M" "{#}"
haftmann@61832
  1394
  defines mset_set = "folding.F (\<lambda>x M. {#x#} + M) {#}"
haftmann@61832
  1395
  by standard (simp add: fun_eq_iff ac_simps)
haftmann@51548
  1396
nipkow@60513
  1397
lemma count_mset_set [simp]:
nipkow@60513
  1398
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P")
nipkow@60513
  1399
  "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
nipkow@60513
  1400
  "x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R")
haftmann@51600
  1401
proof -
wenzelm@60606
  1402
  have *: "count (mset_set A) x = 0" if "x \<notin> A" for A
wenzelm@60606
  1403
  proof (cases "finite A")
wenzelm@60606
  1404
    case False then show ?thesis by simp
wenzelm@60606
  1405
  next
wenzelm@60606
  1406
    case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
wenzelm@60606
  1407
  qed
haftmann@51600
  1408
  then show "PROP ?P" "PROP ?Q" "PROP ?R"
haftmann@51600
  1409
  by (auto elim!: Set.set_insert)
wenzelm@61585
  1410
qed \<comment> \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
nipkow@60513
  1411
nipkow@60513
  1412
lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A"
blanchet@59813
  1413
  by (induct A rule: finite_induct) simp_all
blanchet@59813
  1414
eberlm@63099
  1415
lemma mset_set_Union: 
eberlm@63099
  1416
  "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> mset_set (A \<union> B) = mset_set A + mset_set B"
eberlm@63099
  1417
  by (induction A rule: finite_induct) (auto simp: add_ac)
eberlm@63099
  1418
eberlm@63099
  1419
lemma filter_mset_mset_set [simp]:
eberlm@63099
  1420
  "finite A \<Longrightarrow> filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
eberlm@63099
  1421
proof (induction A rule: finite_induct)
eberlm@63099
  1422
  case (insert x A)
eberlm@63099
  1423
  from insert.hyps have "filter_mset P (mset_set (insert x A)) = 
eberlm@63099
  1424
      filter_mset P (mset_set A) + mset_set (if P x then {x} else {})"
eberlm@63099
  1425
    by (simp add: add_ac)
eberlm@63099
  1426
  also have "filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
eberlm@63099
  1427
    by (rule insert.IH)
eberlm@63099
  1428
  also from insert.hyps 
eberlm@63099
  1429
    have "\<dots> + mset_set (if P x then {x} else {}) =
eberlm@63099
  1430
            mset_set ({x \<in> A. P x} \<union> (if P x then {x} else {}))" (is "_ = mset_set ?A")
eberlm@63099
  1431
     by (intro mset_set_Union [symmetric]) simp_all
eberlm@63099
  1432
  also from insert.hyps have "?A = {y\<in>insert x A. P y}" by auto
eberlm@63099
  1433
  finally show ?case .
eberlm@63099
  1434
qed simp_all
eberlm@63099
  1435
eberlm@63099
  1436
lemma mset_set_Diff:
eberlm@63099
  1437
  assumes "finite A" "B \<subseteq> A"
eberlm@63099
  1438
  shows  "mset_set (A - B) = mset_set A - mset_set B"
eberlm@63099
  1439
proof -
eberlm@63099
  1440
  from assms have "mset_set ((A - B) \<union> B) = mset_set (A - B) + mset_set B"
eberlm@63099
  1441
    by (intro mset_set_Union) (auto dest: finite_subset)
eberlm@63099
  1442
  also from assms have "A - B \<union> B = A" by blast
eberlm@63099
  1443
  finally show ?thesis by simp
eberlm@63099
  1444
qed
eberlm@63099
  1445
eberlm@63099
  1446
lemma mset_set_set: "distinct xs \<Longrightarrow> mset_set (set xs) = mset xs"
eberlm@63099
  1447
  by (induction xs) (simp_all add: add_ac)
eberlm@63099
  1448
haftmann@51548
  1449
context linorder
haftmann@51548
  1450
begin
haftmann@51548
  1451
haftmann@51548
  1452
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
haftmann@51548
  1453
where
nipkow@59998
  1454
  "sorted_list_of_multiset M = fold_mset insort [] M"
haftmann@51548
  1455
haftmann@51548
  1456
lemma sorted_list_of_multiset_empty [simp]:
haftmann@51548
  1457
  "sorted_list_of_multiset {#} = []"
haftmann@51548
  1458
  by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1459
haftmann@51548
  1460
lemma sorted_list_of_multiset_singleton [simp]:
haftmann@51548
  1461
  "sorted_list_of_multiset {#x#} = [x]"
haftmann@51548
  1462
proof -
haftmann@51548
  1463
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1464
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1465
qed
haftmann@51548
  1466
haftmann@51548
  1467
lemma sorted_list_of_multiset_insert [simp]:
haftmann@51548
  1468
  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
haftmann@51548
  1469
proof -
haftmann@51548
  1470
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1471
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1472
qed
haftmann@51548
  1473
haftmann@51548
  1474
end
haftmann@51548
  1475
nipkow@60515
  1476
lemma mset_sorted_list_of_multiset [simp]:
nipkow@60515
  1477
  "mset (sorted_list_of_multiset M) = M"
nipkow@60513
  1478
by (induct M) simp_all
haftmann@51548
  1479
nipkow@60515
  1480
lemma sorted_list_of_multiset_mset [simp]:
nipkow@60515
  1481
  "sorted_list_of_multiset (mset xs) = sort xs"
nipkow@60513
  1482
by (induct xs) simp_all
nipkow@60513
  1483
nipkow@60513
  1484
lemma finite_set_mset_mset_set[simp]:
nipkow@60513
  1485
  "finite A \<Longrightarrow> set_mset (mset_set A) = A"
nipkow@60513
  1486
by (induct A rule: finite_induct) simp_all
nipkow@60513
  1487
eberlm@63099
  1488
lemma mset_set_empty_iff: "mset_set A = {#} \<longleftrightarrow> A = {} \<or> infinite A"
eberlm@63099
  1489
  using finite_set_mset_mset_set by fastforce
eberlm@63099
  1490
nipkow@60513
  1491
lemma infinite_set_mset_mset_set:
nipkow@60513
  1492
  "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
nipkow@60513
  1493
by simp
haftmann@51548
  1494
haftmann@51548
  1495
lemma set_sorted_list_of_multiset [simp]:
nipkow@60495
  1496
  "set (sorted_list_of_multiset M) = set_mset M"
nipkow@60513
  1497
by (induct M) (simp_all add: set_insort)
nipkow@60513
  1498
nipkow@60513
  1499
lemma sorted_list_of_mset_set [simp]:
nipkow@60513
  1500
  "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
nipkow@60513
  1501
by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
haftmann@51548
  1502
eberlm@63099
  1503
lemma mset_upt [simp]: "mset [m..<n] = mset_set {m..<n}"
eberlm@63099
  1504
  by (induction n) (simp_all add: atLeastLessThanSuc add_ac)
eberlm@63099
  1505
eberlm@63099
  1506
lemma image_mset_map_of: 
eberlm@63099
  1507
  "distinct (map fst xs) \<Longrightarrow> {#the (map_of xs i). i \<in># mset (map fst xs)#} = mset (map snd xs)"
eberlm@63099
  1508
proof (induction xs)
eberlm@63099
  1509
  case (Cons x xs)
eberlm@63099
  1510
  have "{#the (map_of (x # xs) i). i \<in># mset (map fst (x # xs))#} = 
eberlm@63099
  1511
          {#the (if i = fst x then Some (snd x) else map_of xs i). 
eberlm@63099
  1512
             i \<in># mset (map fst xs)#} + {#snd x#}" (is "_ = ?A + _") by simp
eberlm@63099
  1513
  also from Cons.prems have "?A = {#the (map_of xs i). i :# mset (map fst xs)#}"
eberlm@63099
  1514
    by (cases x, intro image_mset_cong) (auto simp: in_multiset_in_set)
eberlm@63099
  1515
  also from Cons.prems have "\<dots> = mset (map snd xs)" by (intro Cons.IH) simp_all
eberlm@63099
  1516
  finally show ?case by simp
eberlm@63099
  1517
qed simp_all  
eberlm@63099
  1518
haftmann@51548
  1519
haftmann@60804
  1520
subsection \<open>Replicate operation\<close>
haftmann@60804
  1521
haftmann@60804
  1522
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where
haftmann@60804
  1523
  "replicate_mset n x = ((op + {#x#}) ^^ n) {#}"
haftmann@60804
  1524
haftmann@60804
  1525
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
haftmann@60804
  1526
  unfolding replicate_mset_def by simp
haftmann@60804
  1527
haftmann@60804
  1528
lemma replicate_mset_Suc[simp]: "replicate_mset (Suc n) x = replicate_mset n x + {#x#}"
haftmann@60804
  1529
  unfolding replicate_mset_def by (induct n) (auto intro: add.commute)
haftmann@60804
  1530
haftmann@60804
  1531
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y"
haftmann@62430
  1532
  unfolding replicate_mset_def by (induct n) auto
haftmann@60804
  1533
haftmann@60804
  1534
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)"
haftmann@60804
  1535
  unfolding replicate_mset_def by (induct n) simp_all
haftmann@60804
  1536
haftmann@60804
  1537
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
haftmann@60804
  1538
  by (auto split: if_splits)
haftmann@60804
  1539
haftmann@60804
  1540
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n"
haftmann@60804
  1541
  by (induct n, simp_all)
haftmann@60804
  1542
haftmann@62430
  1543
lemma count_le_replicate_mset_le: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<subseteq># M"
wenzelm@63092
  1544
  by (auto simp add: mset_less_eqI) (metis count_replicate_mset subseteq_mset_def)
haftmann@60804
  1545
haftmann@60804
  1546
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
haftmann@60804
  1547
  by (induct D) simp_all
haftmann@60804
  1548
haftmann@61031
  1549
lemma replicate_count_mset_eq_filter_eq:
haftmann@61031
  1550
  "replicate (count (mset xs) k) k = filter (HOL.eq k) xs"
haftmann@61031
  1551
  by (induct xs) auto
haftmann@61031
  1552
haftmann@62366
  1553
lemma replicate_mset_eq_empty_iff [simp]:
haftmann@62366
  1554
  "replicate_mset n a = {#} \<longleftrightarrow> n = 0"
haftmann@62366
  1555
  by (induct n) simp_all
haftmann@62366
  1556
haftmann@62366
  1557
lemma replicate_mset_eq_iff:
haftmann@62366
  1558
  "replicate_mset m a = replicate_mset n b \<longleftrightarrow>
haftmann@62366
  1559
    m = 0 \<and> n = 0 \<or> m = n \<and> a = b"
haftmann@62366
  1560
  by (auto simp add: multiset_eq_iff)
haftmann@62366
  1561
haftmann@60804
  1562
wenzelm@60500
  1563
subsection \<open>Big operators\<close>
haftmann@51548
  1564
haftmann@51548
  1565
no_notation times (infixl "*" 70)
haftmann@51548
  1566
no_notation Groups.one ("1")
haftmann@51548
  1567
haftmann@51548
  1568
locale comm_monoid_mset = comm_monoid
haftmann@51548
  1569
begin
haftmann@51548
  1570
haftmann@51548
  1571
definition F :: "'a multiset \<Rightarrow> 'a"
wenzelm@60606
  1572
  where eq_fold: "F M = fold_mset f 1 M"
wenzelm@60606
  1573
wenzelm@60606
  1574
lemma empty [simp]: "F {#} = 1"
haftmann@51548
  1575
  by (simp add: eq_fold)
haftmann@51548
  1576
wenzelm@60678
  1577
lemma singleton [simp]: "F {#x#} = x"
haftmann@51548
  1578
proof -
haftmann@51548
  1579
  interpret comp_fun_commute
wenzelm@60678
  1580
    by standard (simp add: fun_eq_iff left_commute)
haftmann@51548
  1581
  show ?thesis by (simp add: eq_fold)
haftmann@51548
  1582
qed
haftmann@51548
  1583
wenzelm@60606
  1584
lemma union [simp]: "F (M + N) = F M * F N"
haftmann@51548
  1585
proof -
haftmann@51548
  1586
  interpret comp_fun_commute f
wenzelm@60678
  1587
    by standard (simp add: fun_eq_iff left_commute)
wenzelm@60678
  1588
  show ?thesis
wenzelm@60678
  1589
    by (induct N) (simp_all add: left_commute eq_fold)
haftmann@51548
  1590
qed
haftmann@51548
  1591
haftmann@51548
  1592
end
haftmann@51548
  1593
wenzelm@61076
  1594
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
wenzelm@60678
  1595
  by standard (simp add: add_ac comp_def)
blanchet@59813
  1596
blanchet@59813
  1597
declare comp_fun_commute.fold_mset_insert[OF comp_fun_commute_plus_mset, simp]
blanchet@59813
  1598
nipkow@59998
  1599
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
  1600
  by (induct NN) auto
blanchet@59813
  1601
haftmann@51548
  1602
notation times (infixl "*" 70)
haftmann@51548
  1603
notation Groups.one ("1")
haftmann@51548
  1604
haftmann@54868
  1605
context comm_monoid_add
haftmann@54868
  1606
begin
haftmann@54868
  1607
wenzelm@61605
  1608
sublocale msetsum: comm_monoid_mset plus 0
haftmann@61832
  1609
  defines msetsum = msetsum.F ..
haftmann@51548
  1610
haftmann@60804
  1611
lemma (in semiring_1) msetsum_replicate_mset [simp]:
haftmann@60804
  1612
  "msetsum (replicate_mset n a) = of_nat n * a"
haftmann@60804
  1613
  by (induct n) (simp_all add: algebra_simps)
haftmann@60804
  1614
haftmann@51548
  1615
lemma setsum_unfold_msetsum:
nipkow@60513
  1616
  "setsum f A = msetsum (image_mset f (mset_set A))"
haftmann@51548
  1617
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1618
haftmann@51548
  1619
end
haftmann@51548
  1620
blanchet@59813
  1621
lemma msetsum_diff:
wenzelm@61076
  1622
  fixes M N :: "('a :: ordered_cancel_comm_monoid_diff) multiset"
haftmann@62430
  1623
  shows "N \<subseteq># M \<Longrightarrow> msetsum (M - N) = msetsum M - msetsum N"
Mathias@60397
  1624
  by (metis add_diff_cancel_right' msetsum.union subset_mset.diff_add)
blanchet@59813
  1625
nipkow@59949
  1626
lemma size_eq_msetsum: "size M = msetsum (image_mset (\<lambda>_. 1) M)"
nipkow@59949
  1627
proof (induct M)
nipkow@59949
  1628
  case empty then show ?case by simp
nipkow@59949
  1629
next
nipkow@59949
  1630
  case (add M x) then show ?case
nipkow@60495
  1631
    by (cases "x \<in> set_mset M")
haftmann@62430
  1632
      (simp_all add: size_multiset_overloaded_eq setsum.distrib setsum.delta' insert_absorb not_in_iff)
nipkow@59949
  1633
qed
nipkow@59949
  1634
eberlm@63099
  1635
lemma size_mset_set [simp]: "size (mset_set A) = card A"
eberlm@63099
  1636
  by (simp only: size_eq_msetsum card_eq_setsum setsum_unfold_msetsum)
eberlm@63099
  1637
haftmann@62366
  1638
syntax (ASCII)
haftmann@62366
  1639
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
haftmann@62366
  1640
syntax
haftmann@62366
  1641
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@62366
  1642
translations
haftmann@62366
  1643
  "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST msetsum (CONST image_mset (\<lambda>i. b) A)"
nipkow@59949
  1644
wenzelm@61955
  1645
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
wenzelm@62837
  1646
  where "\<Union># MM \<equiv> msetsum MM" \<comment> \<open>FIXME ambiguous notation --
wenzelm@62837
  1647
    could likewise refer to \<open>\<Squnion>#\<close>\<close>
blanchet@59813
  1648
nipkow@60495
  1649
lemma set_mset_Union_mset[simp]: "set_mset (\<Union># MM) = (\<Union>M \<in> set_mset MM. set_mset M)"
blanchet@59813
  1650
  by (induct MM) auto
blanchet@59813
  1651
blanchet@59813
  1652
lemma in_Union_mset_iff[iff]: "x \<in># \<Union># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)"
blanchet@59813
  1653
  by (induct MM) auto
blanchet@59813
  1654
haftmann@62366
  1655
lemma count_setsum:
haftmann@62366
  1656
  "count (setsum f A) x = setsum (\<lambda>a. count (f a) x) A"
haftmann@62366
  1657
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@62366
  1658
haftmann@62366
  1659
lemma setsum_eq_empty_iff:
haftmann@62366
  1660
  assumes "finite A"
haftmann@62366
  1661
  shows "setsum f A = {#} \<longleftrightarrow> (\<forall>a\<in>A. f a = {#})"
haftmann@62366
  1662
  using assms by induct simp_all
haftmann@51548
  1663
haftmann@54868
  1664
context comm_monoid_mult
haftmann@54868
  1665
begin
haftmann@54868
  1666
wenzelm@61605
  1667
sublocale msetprod: comm_monoid_mset times 1
haftmann@61832
  1668
  defines msetprod = msetprod.F ..
haftmann@51548
  1669
haftmann@51548
  1670
lemma msetprod_empty:
haftmann@51548
  1671
  "msetprod {#} = 1"
haftmann@51548
  1672
  by (fact msetprod.empty)
haftmann@51548
  1673
haftmann@51548
  1674
lemma msetprod_singleton:
haftmann@51548
  1675
  "msetprod {#x#} = x"
haftmann@51548
  1676
  by (fact msetprod.singleton)
haftmann@51548
  1677
haftmann@51548
  1678
lemma msetprod_Un:
blanchet@58425
  1679
  "msetprod (A + B) = msetprod A * msetprod B"
haftmann@51548
  1680
  by (fact msetprod.union)
haftmann@51548
  1681
haftmann@60804
  1682
lemma msetprod_replicate_mset [simp]:
haftmann@60804
  1683
  "msetprod (replicate_mset n a) = a ^ n"
haftmann@60804
  1684
  by (induct n) (simp_all add: ac_simps)
haftmann@60804
  1685
haftmann@51548
  1686
lemma setprod_unfold_msetprod:
nipkow@60513
  1687
  "setprod f A = msetprod (image_mset f (mset_set A))"
haftmann@51548
  1688
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1689
haftmann@51548
  1690
lemma msetprod_multiplicity:
nipkow@60495
  1691
  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
nipkow@59998
  1692
  by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
haftmann@51548
  1693
haftmann@51548
  1694
end
haftmann@51548
  1695
wenzelm@61955
  1696
syntax (ASCII)
wenzelm@61955
  1697
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3PROD _:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1698
syntax
wenzelm@61955
  1699
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1700
translations
wenzelm@61955
  1701
  "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST msetprod (CONST image_mset (\<lambda>i. b) A)"
haftmann@51548
  1702
haftmann@51548
  1703
lemma (in comm_semiring_1) dvd_msetprod:
haftmann@51548
  1704
  assumes "x \<in># A"
haftmann@51548
  1705
  shows "x dvd msetprod A"
haftmann@51548
  1706
proof -
haftmann@51548
  1707
  from assms have "A = (A - {#x#}) + {#x#}" by simp
haftmann@51548
  1708
  then obtain B where "A = B + {#x#}" ..
haftmann@51548
  1709
  then show ?thesis by simp
haftmann@51548
  1710
qed
haftmann@51548
  1711
haftmann@62430
  1712
lemma (in semidom) msetprod_zero_iff [iff]:
haftmann@62430
  1713
  "msetprod A = 0 \<longleftrightarrow> 0 \<in># A"
haftmann@62366
  1714
  by (induct A) auto
haftmann@62366
  1715
haftmann@62430
  1716
lemma (in semidom_divide) msetprod_diff:
haftmann@62430
  1717
  assumes "B \<subseteq># A" and "0 \<notin># B"
haftmann@62430
  1718
  shows "msetprod (A - B) = msetprod A div msetprod B"
haftmann@62430
  1719
proof -
haftmann@62430
  1720
  from assms obtain C where "A = B + C"
haftmann@62430
  1721
    by (metis subset_mset.add_diff_inverse)
haftmann@62430
  1722
  with assms show ?thesis by simp
haftmann@62430
  1723
qed
haftmann@62430
  1724
haftmann@62430
  1725
lemma (in semidom_divide) msetprod_minus:
haftmann@62430
  1726
  assumes "a \<in># A" and "a \<noteq> 0"
haftmann@62430
  1727
  shows "msetprod (A - {#a#}) = msetprod A div a"
haftmann@62430
  1728
  using assms msetprod_diff [of "{#a#}" A]
haftmann@62430
  1729
    by (auto simp add: single_subset_iff)
haftmann@62430
  1730
haftmann@62430
  1731
lemma (in normalization_semidom) normalized_msetprodI:
haftmann@62430
  1732
  assumes "\<And>a. a \<in># A \<Longrightarrow> normalize a = a"
haftmann@62430
  1733
  shows "normalize (msetprod A) = msetprod A"
haftmann@62430
  1734
  using assms by (induct A) (simp_all add: normalize_mult)
haftmann@62430
  1735
haftmann@51548
  1736
wenzelm@60500
  1737
subsection \<open>Alternative representations\<close>
wenzelm@60500
  1738
wenzelm@60500
  1739
subsubsection \<open>Lists\<close>
haftmann@51548
  1740
haftmann@39533
  1741
context linorder
haftmann@39533
  1742
begin
haftmann@39533
  1743
nipkow@60515
  1744
lemma mset_insort [simp]:
nipkow@60515
  1745
  "mset (insort_key k x xs) = {#x#} + mset xs"
haftmann@37107
  1746
  by (induct xs) (simp_all add: ac_simps)
blanchet@58425
  1747
nipkow@60515
  1748
lemma mset_sort [simp]:
nipkow@60515
  1749
  "mset (sort_key k xs) = mset xs"
haftmann@37107
  1750
  by (induct xs) (simp_all add: ac_simps)
haftmann@37107
  1751
wenzelm@60500
  1752
text \<open>
haftmann@34943
  1753
  This lemma shows which properties suffice to show that a function
wenzelm@61585
  1754
  \<open>f\<close> with \<open>f xs = ys\<close> behaves like sort.
wenzelm@60500
  1755
\<close>
haftmann@37074
  1756
haftmann@39533
  1757
lemma properties_for_sort_key:
nipkow@60515
  1758
  assumes "mset ys = mset xs"
wenzelm@60606
  1759
    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
  1760
    and "sorted (map f ys)"
haftmann@39533
  1761
  shows "sort_key f xs = ys"
wenzelm@60606
  1762
  using assms
wenzelm@46921
  1763
proof (induct xs arbitrary: ys)
haftmann@34943
  1764
  case Nil then show ?case by simp
haftmann@34943
  1765
next
haftmann@34943
  1766
  case (Cons x xs)
haftmann@39533
  1767
  from Cons.prems(2) have
haftmann@40305
  1768
    "\<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
  1769
    by (simp add: filter_remove1)
haftmann@39533
  1770
  with Cons.prems have "sort_key f xs = remove1 x ys"
haftmann@39533
  1771
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
haftmann@62430
  1772
  moreover from Cons.prems have "x \<in># mset ys"
haftmann@62430
  1773
    by auto
haftmann@62430
  1774
  then have "x \<in> set ys"
haftmann@62430
  1775
    by simp
haftmann@39533
  1776
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
haftmann@34943
  1777
qed
haftmann@34943
  1778
haftmann@39533
  1779
lemma properties_for_sort:
nipkow@60515
  1780
  assumes multiset: "mset ys = mset xs"
wenzelm@60606
  1781
    and "sorted ys"
haftmann@39533
  1782
  shows "sort xs = ys"
haftmann@39533
  1783
proof (rule properties_for_sort_key)
nipkow@60515
  1784
  from multiset show "mset ys = mset xs" .
wenzelm@60500
  1785
  from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp
wenzelm@60678
  1786
  from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k
nipkow@60515
  1787
    by (rule mset_eq_length_filter)
wenzelm@60678
  1788
  then have "replicate (length (filter (\<lambda>y. k = y) ys)) k =
wenzelm@60678
  1789
    replicate (length (filter (\<lambda>x. k = x) xs)) k" for k
haftmann@39533
  1790
    by simp
wenzelm@60678
  1791
  then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k
haftmann@39533
  1792
    by (simp add: replicate_length_filter)
haftmann@39533
  1793
qed
haftmann@39533
  1794
haftmann@61031
  1795
lemma sort_key_inj_key_eq:
haftmann@61031
  1796
  assumes mset_equal: "mset xs = mset ys"
haftmann@61031
  1797
    and "inj_on f (set xs)"
haftmann@61031
  1798
    and "sorted (map f ys)"
haftmann@61031
  1799
  shows "sort_key f xs = ys"
haftmann@61031
  1800
proof (rule properties_for_sort_key)
haftmann@61031
  1801
  from mset_equal
haftmann@61031
  1802
  show "mset ys = mset xs" by simp
wenzelm@61188
  1803
  from \<open>sorted (map f ys)\<close>
haftmann@61031
  1804
  show "sorted (map f ys)" .
haftmann@61031
  1805
  show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k
haftmann@61031
  1806
  proof -
haftmann@61031
  1807
    from mset_equal
haftmann@61031
  1808
    have set_equal: "set xs = set ys" by (rule mset_eq_setD)
haftmann@61031
  1809
    with that have "insert k (set ys) = set ys" by auto
wenzelm@61188
  1810
    with \<open>inj_on f (set xs)\<close> have inj: "inj_on f (insert k (set ys))"
haftmann@61031
  1811
      by (simp add: set_equal)
haftmann@61031
  1812
    from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys"
haftmann@61031
  1813
      by (auto intro!: inj_on_filter_key_eq)
haftmann@61031
  1814
    also have "\<dots> = replicate (count (mset ys) k) k"
haftmann@61031
  1815
      by (simp add: replicate_count_mset_eq_filter_eq)
haftmann@61031
  1816
    also have "\<dots> = replicate (count (mset xs) k) k"
haftmann@61031
  1817
      using mset_equal by simp
haftmann@61031
  1818
    also have "\<dots> = filter (HOL.eq k) xs"
haftmann@61031
  1819
      by (simp add: replicate_count_mset_eq_filter_eq)
haftmann@61031
  1820
    also have "\<dots> = [x\<leftarrow>xs . f k = f x]"
haftmann@61031
  1821
      using inj by (auto intro!: inj_on_filter_key_eq [symmetric] simp add: set_equal)
haftmann@61031
  1822
    finally show ?thesis .
haftmann@61031
  1823
  qed
haftmann@61031
  1824
qed
haftmann@61031
  1825
haftmann@61031
  1826
lemma sort_key_eq_sort_key:
haftmann@61031
  1827
  assumes "mset xs = mset ys"
haftmann@61031
  1828
    and "inj_on f (set xs)"
haftmann@61031
  1829
  shows "sort_key f xs = sort_key f ys"
haftmann@61031
  1830
  by (rule sort_key_inj_key_eq) (simp_all add: assms)
haftmann@61031
  1831
haftmann@40303
  1832
lemma sort_key_by_quicksort:
haftmann@40303
  1833
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
haftmann@40303
  1834
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
haftmann@40303
  1835
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
haftmann@40303
  1836
proof (rule properties_for_sort_key)
nipkow@60515
  1837
  show "mset ?rhs = mset ?lhs"
nipkow@60515
  1838
    by (rule multiset_eqI) (auto simp add: mset_filter)
haftmann@40303
  1839
  show "sorted (map f ?rhs)"
haftmann@40303
  1840
    by (auto simp add: sorted_append intro: sorted_map_same)
haftmann@40303
  1841
next
haftmann@40305
  1842
  fix l
haftmann@40305
  1843
  assume "l \<in> set ?rhs"
haftmann@40346
  1844
  let ?pivot = "f (xs ! (length xs div 2))"
haftmann@40346
  1845
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
haftmann@40306
  1846
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
haftmann@40305
  1847
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
haftmann@40346
  1848
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
haftmann@40346
  1849
  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
  1850
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
haftmann@40346
  1851
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
haftmann@40346
  1852
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
haftmann@40306
  1853
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
haftmann@40305
  1854
  proof (cases "f l" ?pivot rule: linorder_cases)
wenzelm@46730
  1855
    case less
wenzelm@46730
  1856
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
wenzelm@46730
  1857
    with less show ?thesis
haftmann@40346
  1858
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40305
  1859
  next
haftmann@40306
  1860
    case equal then show ?thesis
haftmann@40346
  1861
      by (simp add: * less_le)
haftmann@40305
  1862
  next
wenzelm@46730
  1863
    case greater
wenzelm@46730
  1864
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
wenzelm@46730
  1865
    with greater show ?thesis
haftmann@40346
  1866
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40306
  1867
  qed
haftmann@40303
  1868
qed
haftmann@40303
  1869
haftmann@40303
  1870
lemma sort_by_quicksort:
haftmann@40303
  1871
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
haftmann@40303
  1872
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
haftmann@40303
  1873
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
haftmann@40303
  1874
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
haftmann@40303
  1875
wenzelm@60500
  1876
text \<open>A stable parametrized quicksort\<close>
haftmann@40347
  1877
haftmann@40347
  1878
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
haftmann@40347
  1879
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
haftmann@40347
  1880
haftmann@40347
  1881
lemma part_code [code]:
haftmann@40347
  1882
  "part f pivot [] = ([], [], [])"
haftmann@40347
  1883
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
haftmann@40347
  1884
     if x' < pivot then (x # lts, eqs, gts)
haftmann@40347
  1885
     else if x' > pivot then (lts, eqs, x # gts)
haftmann@40347
  1886
     else (lts, x # eqs, gts))"
haftmann@40347
  1887
  by (auto simp add: part_def Let_def split_def)
haftmann@40347
  1888
haftmann@40347
  1889
lemma sort_key_by_quicksort_code [code]:
wenzelm@60606
  1890
  "sort_key f xs =
wenzelm@60606
  1891
    (case xs of
wenzelm@60606
  1892
      [] \<Rightarrow> []
haftmann@40347
  1893
    | [x] \<Rightarrow> xs
haftmann@40347
  1894
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
wenzelm@60606
  1895
    | _ \<Rightarrow>
wenzelm@60606
  1896
        let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
wenzelm@60606
  1897
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1898
proof (cases xs)
haftmann@40347
  1899
  case Nil then show ?thesis by simp
haftmann@40347
  1900
next
wenzelm@46921
  1901
  case (Cons _ ys) note hyps = Cons show ?thesis
wenzelm@46921
  1902
  proof (cases ys)
haftmann@40347
  1903
    case Nil with hyps show ?thesis by simp
haftmann@40347
  1904
  next
wenzelm@46921
  1905
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
wenzelm@46921
  1906
    proof (cases zs)
haftmann@40347
  1907
      case Nil with hyps show ?thesis by auto
haftmann@40347
  1908
    next
blanchet@58425
  1909
      case Cons
haftmann@40347
  1910
      from sort_key_by_quicksort [of f xs]
haftmann@40347
  1911
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1912
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1913
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
haftmann@40347
  1914
      with hyps Cons show ?thesis by (simp only: list.cases)
haftmann@40347
  1915
    qed
haftmann@40347
  1916
  qed
haftmann@40347
  1917
qed
haftmann@40347
  1918
haftmann@39533
  1919
end
haftmann@39533
  1920
haftmann@40347
  1921
hide_const (open) part
haftmann@40347
  1922
haftmann@62430
  1923
lemma mset_remdups_le: "mset (remdups xs) \<subseteq># mset xs"
Mathias@60397
  1924
  by (induct xs) (auto intro: subset_mset.order_trans)
haftmann@34943
  1925
nipkow@60515
  1926
lemma mset_update:
nipkow@60515
  1927
  "i < length ls \<Longrightarrow> mset (ls[i := v]) = mset ls - {#ls ! i#} + {#v#}"
haftmann@34943
  1928
proof (induct ls arbitrary: i)
haftmann@34943
  1929
  case Nil then show ?case by simp
haftmann@34943
  1930
next
haftmann@34943
  1931
  case (Cons x xs)
haftmann@34943
  1932
  show ?case
haftmann@34943
  1933
  proof (cases i)
haftmann@34943
  1934
    case 0 then show ?thesis by simp
haftmann@34943
  1935
  next
haftmann@34943
  1936
    case (Suc i')
haftmann@34943
  1937
    with Cons show ?thesis
haftmann@34943
  1938
      apply simp
haftmann@57512
  1939
      apply (subst add.assoc)
haftmann@57512
  1940
      apply (subst add.commute [of "{#v#}" "{#x#}"])
haftmann@57512
  1941
      apply (subst add.assoc [symmetric])
haftmann@34943
  1942
      apply simp
haftmann@34943
  1943
      apply (rule mset_le_multiset_union_diff_commute)
nipkow@60515
  1944
      apply (simp add: mset_le_single nth_mem_mset)
haftmann@34943
  1945
      done
haftmann@34943
  1946
  qed
haftmann@34943
  1947
qed
haftmann@34943
  1948
nipkow@60515
  1949
lemma mset_swap:
haftmann@34943
  1950
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
nipkow@60515
  1951
    mset (ls[j := ls ! i, i := ls ! j]) = mset ls"
nipkow@60515
  1952
  by (cases "i = j") (simp_all add: mset_update nth_mem_mset)
haftmann@34943
  1953
haftmann@34943
  1954
wenzelm@60500
  1955
subsection \<open>The multiset order\<close>
wenzelm@60500
  1956
wenzelm@60500
  1957
subsubsection \<open>Well-foundedness\<close>
wenzelm@10249
  1958
wenzelm@60606
  1959
definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1960
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
wenzelm@60607
  1961
      (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}"
wenzelm@60606
  1962
wenzelm@60606
  1963
definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1964
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
  1965
haftmann@62430
  1966
lemma mult1I:
haftmann@62430
  1967
  assumes "M = M0 + {#a#}" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
haftmann@62430
  1968
  shows "(N, M) \<in> mult1 r"
haftmann@62430
  1969
  using assms unfolding mult1_def by blast
haftmann@62430
  1970
haftmann@62430
  1971
lemma mult1E:
haftmann@62430
  1972
  assumes "(N, M) \<in> mult1 r"
haftmann@62430
  1973
  obtains a M0 K where "M = M0 + {#a#}" "N = M0 + K" "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
haftmann@62430
  1974
  using assms unfolding mult1_def by blast
haftmann@62430
  1975
berghofe@23751
  1976
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
nipkow@26178
  1977
by (simp add: mult1_def)
wenzelm@10249
  1978
wenzelm@60608
  1979
lemma less_add:
wenzelm@60608
  1980
  assumes mult1: "(N, M0 + {#a#}) \<in> mult1 r"
wenzelm@60608
  1981
  shows
wenzelm@60608
  1982
    "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
wenzelm@60608
  1983
     (\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@60608
  1984
proof -
wenzelm@60607
  1985
  let ?r = "\<lambda>K a. \<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r"
nipkow@11464
  1986
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
wenzelm@60608
  1987
  obtain a' M0' K where M0: "M0 + {#a#} = M0' + {#a'#}"
wenzelm@60608
  1988
    and N: "N = M0' + K"
wenzelm@60608
  1989
    and r: "?r K a'"
wenzelm@60608
  1990
    using mult1 unfolding mult1_def by auto
wenzelm@60608
  1991
  show ?thesis (is "?case1 \<or> ?case2")
wenzelm@60606
  1992
  proof -
wenzelm@60606
  1993
    from M0 consider "M0 = M0'" "a = a'"
wenzelm@60606
  1994
      | K' where "M0 = K' + {#a'#}" "M0' = K' + {#a#}"
wenzelm@60606
  1995
      by atomize_elim (simp only: add_eq_conv_ex)
wenzelm@18258
  1996
    then show ?thesis
wenzelm@60606
  1997
    proof cases
wenzelm@60606
  1998
      case 1
nipkow@11464
  1999
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@60606
  2000
      then have ?case2 ..
wenzelm@60606
  2001
      then show ?thesis ..
wenzelm@10249
  2002
    next
wenzelm@60606
  2003
      case 2
wenzelm@60606
  2004
      from N 2(2) have n: "N = K' + K + {#a#}" by (simp add: ac_simps)
wenzelm@60606
  2005
      with r 2(1) have "?R (K' + K) M0" by blast
wenzelm@60608
  2006
      with n have ?case1 by (simp add: mult1_def)
wenzelm@60606
  2007
      then show ?thesis ..
wenzelm@10249
  2008
    qed
wenzelm@10249
  2009
  qed
wenzelm@10249
  2010
qed
wenzelm@10249
  2011
wenzelm@60608
  2012
lemma all_accessible:
wenzelm@60608
  2013
  assumes "wf r"
wenzelm@60608
  2014
  shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)"
wenzelm@10249
  2015
proof
wenzelm@10249
  2016
  let ?R = "mult1 r"
haftmann@54295
  2017
  let ?W = "Wellfounded.acc ?R"
wenzelm@10249
  2018
  {
wenzelm@10249
  2019
    fix M M0 a
berghofe@23751
  2020
    assume M0: "M0 \<in> ?W"
wenzelm@60606
  2021
      and wf_hyp: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
wenzelm@60606
  2022
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W"
berghofe@23751
  2023
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
  2024
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
  2025
      fix N
berghofe@23751
  2026
      assume "(N, M0 + {#a#}) \<in> ?R"
wenzelm@60608
  2027
      then consider M where "(M, M0) \<in> ?R" "N = M + {#a#}"
wenzelm@60608
  2028
        | K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K"
wenzelm@60608
  2029
        by atomize_elim (rule less_add)
berghofe@23751
  2030
      then show "N \<in> ?W"
wenzelm@60608
  2031
      proof cases
wenzelm@60608
  2032
        case 1
wenzelm@60606
  2033
        from acc_hyp have "(M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W" ..
wenzelm@60500
  2034
        from this and \<open>(M, M0) \<in> ?R\<close> have "M + {#a#} \<in> ?W" ..
wenzelm@60608
  2035
        then show "N \<in> ?W" by (simp only: \<open>N = M + {#a#}\<close>)
wenzelm@10249
  2036
      next
wenzelm@60608
  2037
        case 2
wenzelm@60608
  2038
        from this(1) have "M0 + K \<in> ?W"
wenzelm@10249
  2039
        proof (induct K)
wenzelm@18730
  2040
          case empty
berghofe@23751
  2041
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
  2042
        next
wenzelm@18730
  2043
          case (add K x)
berghofe@23751
  2044
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
  2045
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
  2046
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
  2047
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
haftmann@57512
  2048
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add.assoc)
wenzelm@10249
  2049
        qed
wenzelm@60608
  2050
        then show "N \<in> ?W" by (simp only: 2(2))
wenzelm@10249
  2051
      qed
wenzelm@10249
  2052
    qed
wenzelm@10249
  2053
  } note tedious_reasoning = this
wenzelm@10249
  2054
wenzelm@60608
  2055
  show "M \<in> ?W" for M
wenzelm@10249
  2056
  proof (induct M)
berghofe@23751
  2057
    show "{#} \<in> ?W"
wenzelm@10249
  2058
    proof (rule accI)
berghofe@23751
  2059
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
  2060
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
  2061
    qed
wenzelm@10249
  2062
berghofe@23751
  2063
    fix M a assume "M \<in> ?W"
wenzelm@60608
  2064
    from \<open>wf r\<close> have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  2065
    proof induct
wenzelm@10249
  2066
      fix a
wenzelm@60606
  2067
      assume r: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  2068
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  2069
      proof
berghofe@23751
  2070
        fix M assume "M \<in> ?W"
berghofe@23751
  2071
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
  2072
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
  2073
      qed
wenzelm@10249
  2074
    qed
wenzelm@60500
  2075
    from this and \<open>M \<in> ?W\<close> show "M + {#a#} \<in> ?W" ..
wenzelm@10249
  2076
  qed
wenzelm@10249
  2077
qed
wenzelm@10249
  2078
wenzelm@60606
  2079
theorem wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)"
nipkow@26178
  2080
by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
  2081
wenzelm@60606
  2082
theorem wf_mult: "wf r \<Longrightarrow> wf (mult r)"
nipkow@26178
  2083
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
  2084
wenzelm@10249
  2085
wenzelm@60500
  2086
subsubsection \<open>Closure-free presentation\<close>
wenzelm@60500
  2087
wenzelm@60500
  2088
text \<open>One direction.\<close>
wenzelm@10249
  2089
wenzelm@10249
  2090
lemma mult_implies_one_step:
wenzelm@60606
  2091
  "trans r \<Longrightarrow> (M, N) \<in> mult r \<Longrightarrow>
nipkow@11464
  2092
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
nipkow@60495
  2093
    (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)"
haftmann@62430
  2094
apply (unfold mult_def mult1_def)
nipkow@26178
  2095
apply (erule converse_trancl_induct, clarify)
nipkow@26178
  2096
 apply (rule_tac x = M0 in exI, simp, clarify)
wenzelm@60607
  2097
apply (case_tac "a \<in># K")
nipkow@26178
  2098
 apply (rule_tac x = I in exI)
nipkow@26178
  2099
 apply (simp (no_asm))
nipkow@26178
  2100
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
haftmann@57512
  2101
 apply (simp (no_asm_simp) add: add.assoc [symmetric])
wenzelm@59807
  2102
 apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong)
nipkow@26178
  2103
 apply (simp add: diff_union_single_conv)
nipkow@26178
  2104
 apply (simp (no_asm_use) add: trans_def)
haftmann@62430
  2105
 apply (metis (no_types, hide_lams) Multiset.diff_right_commute Un_iff diff_single_trivial multi_drop_mem_not_eq)
wenzelm@60607
  2106
apply (subgoal_tac "a \<in># I")
nipkow@26178
  2107
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow@26178
  2108
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow@26178
  2109
 apply (rule_tac x = "K + Ka" in exI)
nipkow@26178
  2110
 apply (rule conjI)
nipkow@39302
  2111
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  2112
 apply (rule conjI)
wenzelm@59807
  2113
  apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong, simp)
nipkow@39302
  2114
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  2115
 apply (simp (no_asm_use) add: trans_def)
haftmann@62430
  2116
apply (subgoal_tac "a \<in># (M0 + {#a#})")
haftmann@62430
  2117
 apply (simp_all add: not_in_iff)
nipkow@26178
  2118
 apply blast
haftmann@62430
  2119
 apply (metis add.comm_neutral add_diff_cancel_right' count_eq_zero_iff diff_single_trivial multi_self_add_other_not_self plus_multiset.rep_eq)
nipkow@26178
  2120
done
wenzelm@10249
  2121
wenzelm@10249
  2122
lemma one_step_implies_mult_aux:
wenzelm@60678
  2123
  "\<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
  2124
    \<longrightarrow> (I + K, I + J) \<in> mult r"
wenzelm@60678
  2125
apply (induct n)
wenzelm@60678
  2126
 apply auto
nipkow@26178
  2127
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow@26178
  2128
apply (rename_tac "J'", simp)
nipkow@26178
  2129
apply (erule notE, auto)
nipkow@26178
  2130
apply (case_tac "J' = {#}")
nipkow@26178
  2131
 apply (simp add: mult_def)
nipkow@26178
  2132
 apply (rule r_into_trancl)
haftmann@62430
  2133
 apply (simp add: mult1_def, blast)
wenzelm@60500
  2134
txt \<open>Now we know @{term "J' \<noteq> {#}"}.\<close>
nipkow@26178
  2135
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@60495
  2136
apply (erule_tac P = "\<forall>k \<in> set_mset K. P k" for P in rev_mp)
nipkow@26178
  2137
apply (erule ssubst)
nipkow@26178
  2138
apply (simp add: Ball_def, auto)
nipkow@26178
  2139
apply (subgoal_tac
wenzelm@60607
  2140
  "((I + {# x \<in># K. (x, a) \<in> r #}) + {# x \<in># K. (x, a) \<notin> r #},
wenzelm@60607
  2141
    (I + {# x \<in># K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow@26178
  2142
 prefer 2
nipkow@26178
  2143
 apply force
haftmann@57512
  2144
apply (simp (no_asm_use) add: add.assoc [symmetric] mult_def)
nipkow@26178
  2145
apply (erule trancl_trans)
nipkow@26178
  2146
apply (rule r_into_trancl)
haftmann@62430
  2147
apply (simp add: mult1_def)
nipkow@26178
  2148
apply (rule_tac x = a in exI)
nipkow@26178
  2149
apply (rule_tac x = "I + J'" in exI)
haftmann@57514
  2150
apply (simp add: ac_simps)
nipkow@26178
  2151
done
wenzelm@10249
  2152
wenzelm@17161
  2153
lemma one_step_implies_mult:
nipkow@62651
  2154
  "J \<noteq> {#} \<Longrightarrow> \<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r
wenzelm@60606
  2155
    \<Longrightarrow> (I + K, I + J) \<in> mult r"
nipkow@26178
  2156
using one_step_implies_mult_aux by blast
wenzelm@10249
  2157
haftmann@63089
  2158
subsection \<open>A quasi-executable characterization\<close>
haftmann@63088
  2159
haftmann@63088
  2160
text \<open>
haftmann@63088
  2161
  The decreasing parts \<open>A\<close> and \<open>B\<close> of multisets in a multiset-comparison
haftmann@63088
  2162
  \<open>(I + B, I + A) \<in> mult r\<close>, can always be made disjoint.
haftmann@63088
  2163
\<close>
haftmann@63088
  2164
lemma decreasing_parts_disj:
haftmann@63088
  2165
  assumes "irrefl r" and "trans r"
haftmann@63088
  2166
    and "A \<noteq> {#}" and *: "\<forall>b\<in>#B. \<exists>a\<in>#A. (b, a) \<in> r"
haftmann@63088
  2167
  defines "Z \<equiv> A #\<inter> B"
haftmann@63088
  2168
  defines "X \<equiv> A - Z"
haftmann@63088
  2169
  defines "Y \<equiv> B - Z"
haftmann@63088
  2170
  shows "X \<noteq> {#} \<and> X #\<inter> Y = {#} \<and>
haftmann@63088
  2171
    A = X + Z \<and> B = Y + Z \<and> (\<forall>y\<in>#Y. \<exists>x\<in>#X. (y, x) \<in> r)"
haftmann@63088
  2172
proof -
haftmann@63088
  2173
  define D
haftmann@63088
  2174
    where "D = set_mset A \<union> set_mset B"
haftmann@63088
  2175
  let ?r = "r \<inter> D \<times> D"
haftmann@63088
  2176
  have "irrefl ?r" and "trans ?r" and "finite ?r"
haftmann@63088
  2177
    using \<open>irrefl r\<close> and \<open>trans r\<close> by (auto simp: D_def irrefl_def trans_Restr)
haftmann@63088
  2178
  note wf_converse_induct = wf_induct [OF wf_converse [OF this]]
haftmann@63088
  2179
  { fix b assume "b \<in># B"
haftmann@63088
  2180
    then have "\<exists>x. x \<in># X \<and> (b, x) \<in> r"
haftmann@63088
  2181
    proof (induction rule: wf_converse_induct)
haftmann@63088
  2182
      case (1 b)
haftmann