src/HOL/Library/Multiset.thy
author haftmann
Fri Dec 07 15:07:59 2007 +0100 (2007-12-07)
changeset 25571 c9e39eafc7a0
parent 25507 d13468d40131
child 25595 6c48275f9c76
permissions -rw-r--r--
instantiation target rather than legacy instance
wenzelm@10249
     1
(*  Title:      HOL/Library/Multiset.thy
wenzelm@10249
     2
    ID:         $Id$
paulson@15072
     3
    Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
wenzelm@10249
     4
*)
wenzelm@10249
     5
wenzelm@14706
     6
header {* Multisets *}
wenzelm@10249
     7
nipkow@15131
     8
theory Multiset
krauss@19564
     9
imports Main
nipkow@15131
    10
begin
wenzelm@10249
    11
wenzelm@10249
    12
subsection {* The type of multisets *}
wenzelm@10249
    13
nipkow@25162
    14
typedef 'a multiset = "{f::'a => nat. finite {x . f x > 0}}"
wenzelm@10249
    15
proof
nipkow@11464
    16
  show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
wenzelm@10249
    17
qed
wenzelm@10249
    18
wenzelm@10249
    19
lemmas multiset_typedef [simp] =
wenzelm@10277
    20
    Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
wenzelm@10277
    21
  and [simp] = Rep_multiset_inject [symmetric]
wenzelm@10249
    22
wenzelm@19086
    23
definition
wenzelm@21404
    24
  Mempty :: "'a multiset"  ("{#}") where
wenzelm@19086
    25
  "{#} = Abs_multiset (\<lambda>a. 0)"
wenzelm@10249
    26
wenzelm@21404
    27
definition
nipkow@25507
    28
  single :: "'a => 'a multiset" where
nipkow@25507
    29
  "single a = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
wenzelm@10249
    30
wenzelm@21404
    31
definition
wenzelm@21404
    32
  count :: "'a multiset => 'a => nat" where
wenzelm@19086
    33
  "count = Rep_multiset"
wenzelm@10249
    34
wenzelm@21404
    35
definition
wenzelm@21404
    36
  MCollect :: "'a multiset => ('a => bool) => 'a multiset" where
wenzelm@19086
    37
  "MCollect M P = Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
wenzelm@19086
    38
wenzelm@19363
    39
abbreviation
wenzelm@21404
    40
  Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
nipkow@25162
    41
  "a :# M == count M a > 0"
wenzelm@10249
    42
wenzelm@10249
    43
syntax
wenzelm@10249
    44
  "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
wenzelm@10249
    45
translations
wenzelm@20770
    46
  "{#x:M. P#}" == "CONST MCollect M (\<lambda>x. P)"
wenzelm@10249
    47
wenzelm@19086
    48
definition
wenzelm@21404
    49
  set_of :: "'a multiset => 'a set" where
wenzelm@19086
    50
  "set_of M = {x. x :# M}"
wenzelm@10249
    51
haftmann@25571
    52
instantiation multiset :: (type) "{plus, minus, zero, size}" 
haftmann@25571
    53
begin
haftmann@25571
    54
haftmann@25571
    55
definition
nipkow@11464
    56
  union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)"
haftmann@25571
    57
haftmann@25571
    58
definition
nipkow@11464
    59
  diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
haftmann@25571
    60
haftmann@25571
    61
definition
wenzelm@11701
    62
  Zero_multiset_def [simp]: "0 == {#}"
haftmann@25571
    63
haftmann@25571
    64
definition
haftmann@25571
    65
  size_def: "size M == setsum (count M) (set_of M)"
haftmann@25571
    66
haftmann@25571
    67
instance ..
haftmann@25571
    68
haftmann@25571
    69
end
wenzelm@10249
    70
wenzelm@19086
    71
definition
wenzelm@21404
    72
  multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"  (infixl "#\<inter>" 70) where
wenzelm@19086
    73
  "multiset_inter A B = A - (A - B)"
kleing@15869
    74
nipkow@25507
    75
syntax -- "Multiset Enumeration"
nipkow@25507
    76
  "@multiset" :: "args => 'a multiset"    ("{#(_)#}")
nipkow@25507
    77
nipkow@25507
    78
translations
nipkow@25507
    79
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
    80
  "{#x#}" == "CONST single x"
nipkow@25507
    81
wenzelm@10249
    82
wenzelm@10249
    83
text {*
wenzelm@10249
    84
 \medskip Preservation of the representing set @{term multiset}.
wenzelm@10249
    85
*}
wenzelm@10249
    86
nipkow@11464
    87
lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
wenzelm@17161
    88
  by (simp add: multiset_def)
wenzelm@10249
    89
wenzelm@11701
    90
lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
wenzelm@17161
    91
  by (simp add: multiset_def)
wenzelm@10249
    92
wenzelm@10249
    93
lemma union_preserves_multiset [simp]:
nipkow@11464
    94
    "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
wenzelm@17161
    95
  apply (simp add: multiset_def)
wenzelm@17161
    96
  apply (drule (1) finite_UnI)
wenzelm@10249
    97
  apply (simp del: finite_Un add: Un_def)
wenzelm@10249
    98
  done
wenzelm@10249
    99
wenzelm@10249
   100
lemma diff_preserves_multiset [simp]:
nipkow@11464
   101
    "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
wenzelm@17161
   102
  apply (simp add: multiset_def)
wenzelm@10249
   103
  apply (rule finite_subset)
wenzelm@17161
   104
   apply auto
wenzelm@10249
   105
  done
wenzelm@10249
   106
wenzelm@10249
   107
wenzelm@10249
   108
subsection {* Algebraic properties of multisets *}
wenzelm@10249
   109
wenzelm@10249
   110
subsubsection {* Union *}
wenzelm@10249
   111
wenzelm@17161
   112
lemma union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
wenzelm@17161
   113
  by (simp add: union_def Mempty_def)
wenzelm@10249
   114
wenzelm@17161
   115
lemma union_commute: "M + N = N + (M::'a multiset)"
wenzelm@17161
   116
  by (simp add: union_def add_ac)
wenzelm@17161
   117
wenzelm@17161
   118
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
wenzelm@17161
   119
  by (simp add: union_def add_ac)
wenzelm@10249
   120
wenzelm@17161
   121
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
wenzelm@17161
   122
proof -
wenzelm@17161
   123
  have "M + (N + K) = (N + K) + M"
wenzelm@17161
   124
    by (rule union_commute)
wenzelm@17161
   125
  also have "\<dots> = N + (K + M)"
wenzelm@17161
   126
    by (rule union_assoc)
wenzelm@17161
   127
  also have "K + M = M + K"
wenzelm@17161
   128
    by (rule union_commute)
wenzelm@17161
   129
  finally show ?thesis .
wenzelm@17161
   130
qed
wenzelm@10249
   131
wenzelm@17161
   132
lemmas union_ac = union_assoc union_commute union_lcomm
wenzelm@10249
   133
obua@14738
   134
instance multiset :: (type) comm_monoid_add
wenzelm@17200
   135
proof
obua@14722
   136
  fix a b c :: "'a multiset"
obua@14722
   137
  show "(a + b) + c = a + (b + c)" by (rule union_assoc)
obua@14722
   138
  show "a + b = b + a" by (rule union_commute)
obua@14722
   139
  show "0 + a = a" by simp
obua@14722
   140
qed
wenzelm@10277
   141
wenzelm@10249
   142
wenzelm@10249
   143
subsubsection {* Difference *}
wenzelm@10249
   144
wenzelm@17161
   145
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
wenzelm@17161
   146
  by (simp add: Mempty_def diff_def)
wenzelm@10249
   147
wenzelm@17161
   148
lemma diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
wenzelm@17161
   149
  by (simp add: union_def diff_def)
wenzelm@10249
   150
wenzelm@10249
   151
wenzelm@10249
   152
subsubsection {* Count of elements *}
wenzelm@10249
   153
wenzelm@17161
   154
lemma count_empty [simp]: "count {#} a = 0"
wenzelm@17161
   155
  by (simp add: count_def Mempty_def)
wenzelm@10249
   156
wenzelm@17161
   157
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
wenzelm@17161
   158
  by (simp add: count_def single_def)
wenzelm@10249
   159
wenzelm@17161
   160
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
wenzelm@17161
   161
  by (simp add: count_def union_def)
wenzelm@10249
   162
wenzelm@17161
   163
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
wenzelm@17161
   164
  by (simp add: count_def diff_def)
wenzelm@10249
   165
wenzelm@10249
   166
wenzelm@10249
   167
subsubsection {* Set of elements *}
wenzelm@10249
   168
wenzelm@17161
   169
lemma set_of_empty [simp]: "set_of {#} = {}"
wenzelm@17161
   170
  by (simp add: set_of_def)
wenzelm@10249
   171
wenzelm@17161
   172
lemma set_of_single [simp]: "set_of {#b#} = {b}"
wenzelm@17161
   173
  by (simp add: set_of_def)
wenzelm@10249
   174
wenzelm@17161
   175
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
wenzelm@17161
   176
  by (auto simp add: set_of_def)
wenzelm@10249
   177
wenzelm@17161
   178
lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
wenzelm@17161
   179
  by (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
wenzelm@10249
   180
wenzelm@17161
   181
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
wenzelm@17161
   182
  by (auto simp add: set_of_def)
wenzelm@10249
   183
wenzelm@10249
   184
wenzelm@10249
   185
subsubsection {* Size *}
wenzelm@10249
   186
wenzelm@17161
   187
lemma size_empty [simp]: "size {#} = 0"
wenzelm@17161
   188
  by (simp add: size_def)
wenzelm@10249
   189
wenzelm@17161
   190
lemma size_single [simp]: "size {#b#} = 1"
wenzelm@17161
   191
  by (simp add: size_def)
wenzelm@10249
   192
wenzelm@17161
   193
lemma finite_set_of [iff]: "finite (set_of M)"
wenzelm@17161
   194
  using Rep_multiset [of M]
wenzelm@17161
   195
  by (simp add: multiset_def set_of_def count_def)
wenzelm@10249
   196
wenzelm@17161
   197
lemma setsum_count_Int:
nipkow@11464
   198
    "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
wenzelm@18258
   199
  apply (induct rule: finite_induct)
wenzelm@17161
   200
   apply simp
wenzelm@10249
   201
  apply (simp add: Int_insert_left set_of_def)
wenzelm@10249
   202
  done
wenzelm@10249
   203
wenzelm@17161
   204
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
wenzelm@10249
   205
  apply (unfold size_def)
nipkow@11464
   206
  apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
wenzelm@10249
   207
   prefer 2
paulson@15072
   208
   apply (rule ext, simp)
nipkow@15402
   209
  apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
wenzelm@10249
   210
  apply (subst Int_commute)
wenzelm@10249
   211
  apply (simp (no_asm_simp) add: setsum_count_Int)
wenzelm@10249
   212
  done
wenzelm@10249
   213
wenzelm@17161
   214
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
paulson@15072
   215
  apply (unfold size_def Mempty_def count_def, auto)
wenzelm@10249
   216
  apply (simp add: set_of_def count_def expand_fun_eq)
wenzelm@10249
   217
  done
wenzelm@10249
   218
wenzelm@17161
   219
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
wenzelm@10249
   220
  apply (unfold size_def)
paulson@15072
   221
  apply (drule setsum_SucD, auto)
wenzelm@10249
   222
  done
wenzelm@10249
   223
wenzelm@10249
   224
wenzelm@10249
   225
subsubsection {* Equality of multisets *}
wenzelm@10249
   226
wenzelm@17161
   227
lemma multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
wenzelm@17161
   228
  by (simp add: count_def expand_fun_eq)
wenzelm@10249
   229
wenzelm@17161
   230
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
wenzelm@17161
   231
  by (simp add: single_def Mempty_def expand_fun_eq)
wenzelm@10249
   232
wenzelm@17161
   233
lemma single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
wenzelm@17161
   234
  by (auto simp add: single_def expand_fun_eq)
wenzelm@10249
   235
wenzelm@17161
   236
lemma union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
wenzelm@17161
   237
  by (auto simp add: union_def Mempty_def expand_fun_eq)
wenzelm@10249
   238
wenzelm@17161
   239
lemma empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
wenzelm@17161
   240
  by (auto simp add: union_def Mempty_def expand_fun_eq)
wenzelm@10249
   241
wenzelm@17161
   242
lemma union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
wenzelm@17161
   243
  by (simp add: union_def expand_fun_eq)
wenzelm@10249
   244
wenzelm@17161
   245
lemma union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
wenzelm@17161
   246
  by (simp add: union_def expand_fun_eq)
wenzelm@10249
   247
wenzelm@17161
   248
lemma union_is_single:
nipkow@11464
   249
    "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
paulson@15072
   250
  apply (simp add: Mempty_def single_def union_def add_is_1 expand_fun_eq)
wenzelm@10249
   251
  apply blast
wenzelm@10249
   252
  done
wenzelm@10249
   253
wenzelm@17161
   254
lemma single_is_union:
paulson@15072
   255
     "({#a#} = M + N) = ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
wenzelm@10249
   256
  apply (unfold Mempty_def single_def union_def)
nipkow@11464
   257
  apply (simp add: add_is_1 one_is_add expand_fun_eq)
wenzelm@10249
   258
  apply (blast dest: sym)
wenzelm@10249
   259
  done
wenzelm@10249
   260
wenzelm@17161
   261
lemma add_eq_conv_diff:
wenzelm@10249
   262
  "(M + {#a#} = N + {#b#}) =
paulson@15072
   263
   (M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
wenzelm@24035
   264
  using [[simproc del: neq]]
wenzelm@10249
   265
  apply (unfold single_def union_def diff_def)
wenzelm@10249
   266
  apply (simp (no_asm) add: expand_fun_eq)
paulson@15072
   267
  apply (rule conjI, force, safe, simp_all)
berghofe@13601
   268
  apply (simp add: eq_sym_conv)
wenzelm@10249
   269
  done
wenzelm@10249
   270
kleing@15869
   271
declare Rep_multiset_inject [symmetric, simp del]
kleing@15869
   272
nipkow@23611
   273
instance multiset :: (type) cancel_ab_semigroup_add
nipkow@23611
   274
proof
nipkow@23611
   275
  fix a b c :: "'a multiset"
nipkow@23611
   276
  show "a + b = a + c \<Longrightarrow> b = c" by simp
nipkow@23611
   277
qed
kleing@15869
   278
kleing@15869
   279
subsubsection {* Intersection *}
kleing@15869
   280
kleing@15869
   281
lemma multiset_inter_count:
wenzelm@17161
   282
    "count (A #\<inter> B) x = min (count A x) (count B x)"
wenzelm@17161
   283
  by (simp add: multiset_inter_def min_def)
kleing@15869
   284
kleing@15869
   285
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
wenzelm@17200
   286
  by (simp add: multiset_eq_conv_count_eq multiset_inter_count
haftmann@21214
   287
    min_max.inf_commute)
kleing@15869
   288
kleing@15869
   289
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
wenzelm@17200
   290
  by (simp add: multiset_eq_conv_count_eq multiset_inter_count
haftmann@21214
   291
    min_max.inf_assoc)
kleing@15869
   292
kleing@15869
   293
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
kleing@15869
   294
  by (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def)
kleing@15869
   295
wenzelm@17161
   296
lemmas multiset_inter_ac =
wenzelm@17161
   297
  multiset_inter_commute
wenzelm@17161
   298
  multiset_inter_assoc
wenzelm@17161
   299
  multiset_inter_left_commute
kleing@15869
   300
kleing@15869
   301
lemma multiset_union_diff_commute: "B #\<inter> C = {#} \<Longrightarrow> A + B - C = A - C + B"
wenzelm@17200
   302
  apply (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def
wenzelm@17161
   303
    split: split_if_asm)
kleing@15869
   304
  apply clarsimp
wenzelm@17161
   305
  apply (erule_tac x = a in allE)
kleing@15869
   306
  apply auto
kleing@15869
   307
  done
kleing@15869
   308
wenzelm@10249
   309
wenzelm@10249
   310
subsection {* Induction over multisets *}
wenzelm@10249
   311
wenzelm@10249
   312
lemma setsum_decr:
wenzelm@11701
   313
  "finite F ==> (0::nat) < f a ==>
paulson@15072
   314
    setsum (f (a := f a - 1)) F = (if a\<in>F then setsum f F - 1 else setsum f F)"
wenzelm@18258
   315
  apply (induct rule: finite_induct)
wenzelm@18258
   316
   apply auto
paulson@15072
   317
  apply (drule_tac a = a in mk_disjoint_insert, auto)
wenzelm@10249
   318
  done
wenzelm@10249
   319
wenzelm@10313
   320
lemma rep_multiset_induct_aux:
wenzelm@18730
   321
  assumes 1: "P (\<lambda>a. (0::nat))"
wenzelm@18730
   322
    and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))"
nipkow@25134
   323
  shows "\<forall>f. f \<in> multiset --> setsum f {x. f x \<noteq> 0} = n --> P f"
wenzelm@18730
   324
  apply (unfold multiset_def)
wenzelm@18730
   325
  apply (induct_tac n, simp, clarify)
wenzelm@18730
   326
   apply (subgoal_tac "f = (\<lambda>a.0)")
wenzelm@18730
   327
    apply simp
wenzelm@18730
   328
    apply (rule 1)
wenzelm@18730
   329
   apply (rule ext, force, clarify)
wenzelm@18730
   330
  apply (frule setsum_SucD, clarify)
wenzelm@18730
   331
  apply (rename_tac a)
nipkow@25162
   332
  apply (subgoal_tac "finite {x. (f (a := f a - 1)) x > 0}")
wenzelm@18730
   333
   prefer 2
wenzelm@18730
   334
   apply (rule finite_subset)
wenzelm@18730
   335
    prefer 2
wenzelm@18730
   336
    apply assumption
wenzelm@18730
   337
   apply simp
wenzelm@18730
   338
   apply blast
wenzelm@18730
   339
  apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
wenzelm@18730
   340
   prefer 2
wenzelm@18730
   341
   apply (rule ext)
wenzelm@18730
   342
   apply (simp (no_asm_simp))
wenzelm@18730
   343
   apply (erule ssubst, rule 2 [unfolded multiset_def], blast)
wenzelm@18730
   344
  apply (erule allE, erule impE, erule_tac [2] mp, blast)
wenzelm@18730
   345
  apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
nipkow@25134
   346
  apply (subgoal_tac "{x. x \<noteq> a --> f x \<noteq> 0} = {x. f x \<noteq> 0}")
wenzelm@18730
   347
   prefer 2
wenzelm@18730
   348
   apply blast
nipkow@25134
   349
  apply (subgoal_tac "{x. x \<noteq> a \<and> f x \<noteq> 0} = {x. f x \<noteq> 0} - {a}")
wenzelm@18730
   350
   prefer 2
wenzelm@18730
   351
   apply blast
wenzelm@18730
   352
  apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong)
wenzelm@18730
   353
  done
wenzelm@10249
   354
wenzelm@10313
   355
theorem rep_multiset_induct:
nipkow@11464
   356
  "f \<in> multiset ==> P (\<lambda>a. 0) ==>
wenzelm@11701
   357
    (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
wenzelm@17161
   358
  using rep_multiset_induct_aux by blast
wenzelm@10249
   359
wenzelm@18258
   360
theorem multiset_induct [case_names empty add, induct type: multiset]:
wenzelm@18258
   361
  assumes empty: "P {#}"
wenzelm@18258
   362
    and add: "!!M x. P M ==> P (M + {#x#})"
wenzelm@17161
   363
  shows "P M"
wenzelm@10249
   364
proof -
wenzelm@10249
   365
  note defns = union_def single_def Mempty_def
wenzelm@10249
   366
  show ?thesis
wenzelm@10249
   367
    apply (rule Rep_multiset_inverse [THEN subst])
wenzelm@10313
   368
    apply (rule Rep_multiset [THEN rep_multiset_induct])
wenzelm@18258
   369
     apply (rule empty [unfolded defns])
paulson@15072
   370
    apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))")
wenzelm@10249
   371
     prefer 2
wenzelm@10249
   372
     apply (simp add: expand_fun_eq)
wenzelm@10249
   373
    apply (erule ssubst)
wenzelm@17200
   374
    apply (erule Abs_multiset_inverse [THEN subst])
wenzelm@18258
   375
    apply (erule add [unfolded defns, simplified])
wenzelm@10249
   376
    done
wenzelm@10249
   377
qed
wenzelm@10249
   378
wenzelm@10249
   379
lemma MCollect_preserves_multiset:
nipkow@11464
   380
    "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
wenzelm@10249
   381
  apply (simp add: multiset_def)
paulson@15072
   382
  apply (rule finite_subset, auto)
wenzelm@10249
   383
  done
wenzelm@10249
   384
wenzelm@17161
   385
lemma count_MCollect [simp]:
wenzelm@10249
   386
    "count {# x:M. P x #} a = (if P a then count M a else 0)"
paulson@15072
   387
  by (simp add: count_def MCollect_def MCollect_preserves_multiset)
wenzelm@10249
   388
wenzelm@17161
   389
lemma set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
wenzelm@17161
   390
  by (auto simp add: set_of_def)
wenzelm@10249
   391
wenzelm@17161
   392
lemma multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
wenzelm@17161
   393
  by (subst multiset_eq_conv_count_eq, auto)
wenzelm@10249
   394
wenzelm@17161
   395
lemma add_eq_conv_ex:
wenzelm@17161
   396
  "(M + {#a#} = N + {#b#}) =
wenzelm@17161
   397
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
paulson@15072
   398
  by (auto simp add: add_eq_conv_diff)
wenzelm@10249
   399
kleing@15869
   400
declare multiset_typedef [simp del]
wenzelm@10249
   401
wenzelm@17161
   402
wenzelm@10249
   403
subsection {* Multiset orderings *}
wenzelm@10249
   404
wenzelm@10249
   405
subsubsection {* Well-foundedness *}
wenzelm@10249
   406
wenzelm@19086
   407
definition
berghofe@23751
   408
  mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
wenzelm@19086
   409
  "mult1 r =
berghofe@23751
   410
    {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
berghofe@23751
   411
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
wenzelm@10249
   412
wenzelm@21404
   413
definition
berghofe@23751
   414
  mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
berghofe@23751
   415
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
   416
berghofe@23751
   417
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
wenzelm@10277
   418
  by (simp add: mult1_def)
wenzelm@10249
   419
berghofe@23751
   420
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
berghofe@23751
   421
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
berghofe@23751
   422
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@19582
   423
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
wenzelm@10249
   424
proof (unfold mult1_def)
berghofe@23751
   425
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
nipkow@11464
   426
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
berghofe@23751
   427
  let ?case1 = "?case1 {(N, M). ?R N M}"
wenzelm@10249
   428
berghofe@23751
   429
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
wenzelm@18258
   430
  then have "\<exists>a' M0' K.
nipkow@11464
   431
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
wenzelm@18258
   432
  then show "?case1 \<or> ?case2"
wenzelm@10249
   433
  proof (elim exE conjE)
wenzelm@10249
   434
    fix a' M0' K
wenzelm@10249
   435
    assume N: "N = M0' + K" and r: "?r K a'"
wenzelm@10249
   436
    assume "M0 + {#a#} = M0' + {#a'#}"
wenzelm@18258
   437
    then have "M0 = M0' \<and> a = a' \<or>
nipkow@11464
   438
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
wenzelm@10249
   439
      by (simp only: add_eq_conv_ex)
wenzelm@18258
   440
    then show ?thesis
wenzelm@10249
   441
    proof (elim disjE conjE exE)
wenzelm@10249
   442
      assume "M0 = M0'" "a = a'"
nipkow@11464
   443
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@18258
   444
      then have ?case2 .. then show ?thesis ..
wenzelm@10249
   445
    next
wenzelm@10249
   446
      fix K'
wenzelm@10249
   447
      assume "M0' = K' + {#a#}"
wenzelm@10249
   448
      with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
wenzelm@10249
   449
wenzelm@10249
   450
      assume "M0 = K' + {#a'#}"
wenzelm@10249
   451
      with r have "?R (K' + K) M0" by blast
wenzelm@18258
   452
      with n have ?case1 by simp then show ?thesis ..
wenzelm@10249
   453
    qed
wenzelm@10249
   454
  qed
wenzelm@10249
   455
qed
wenzelm@10249
   456
berghofe@23751
   457
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
wenzelm@10249
   458
proof
wenzelm@10249
   459
  let ?R = "mult1 r"
wenzelm@10249
   460
  let ?W = "acc ?R"
wenzelm@10249
   461
  {
wenzelm@10249
   462
    fix M M0 a
berghofe@23751
   463
    assume M0: "M0 \<in> ?W"
berghofe@23751
   464
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
   465
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
berghofe@23751
   466
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
   467
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
   468
      fix N
berghofe@23751
   469
      assume "(N, M0 + {#a#}) \<in> ?R"
berghofe@23751
   470
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
berghofe@23751
   471
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
wenzelm@10249
   472
        by (rule less_add)
berghofe@23751
   473
      then show "N \<in> ?W"
wenzelm@10249
   474
      proof (elim exE disjE conjE)
berghofe@23751
   475
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
berghofe@23751
   476
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
berghofe@23751
   477
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
berghofe@23751
   478
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
   479
      next
wenzelm@10249
   480
        fix K
wenzelm@10249
   481
        assume N: "N = M0 + K"
berghofe@23751
   482
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
berghofe@23751
   483
        then have "M0 + K \<in> ?W"
wenzelm@10249
   484
        proof (induct K)
wenzelm@18730
   485
          case empty
berghofe@23751
   486
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
   487
        next
wenzelm@18730
   488
          case (add K x)
berghofe@23751
   489
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
   490
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
   491
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
   492
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
berghofe@23751
   493
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
wenzelm@10249
   494
        qed
berghofe@23751
   495
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
   496
      qed
wenzelm@10249
   497
    qed
wenzelm@10249
   498
  } note tedious_reasoning = this
wenzelm@10249
   499
berghofe@23751
   500
  assume wf: "wf r"
wenzelm@10249
   501
  fix M
berghofe@23751
   502
  show "M \<in> ?W"
wenzelm@10249
   503
  proof (induct M)
berghofe@23751
   504
    show "{#} \<in> ?W"
wenzelm@10249
   505
    proof (rule accI)
berghofe@23751
   506
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
   507
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
   508
    qed
wenzelm@10249
   509
berghofe@23751
   510
    fix M a assume "M \<in> ?W"
berghofe@23751
   511
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
   512
    proof induct
wenzelm@10249
   513
      fix a
berghofe@23751
   514
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
   515
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
   516
      proof
berghofe@23751
   517
        fix M assume "M \<in> ?W"
berghofe@23751
   518
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
   519
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
   520
      qed
wenzelm@10249
   521
    qed
berghofe@23751
   522
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
wenzelm@10249
   523
  qed
wenzelm@10249
   524
qed
wenzelm@10249
   525
berghofe@23751
   526
theorem wf_mult1: "wf r ==> wf (mult1 r)"
wenzelm@23373
   527
  by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
   528
berghofe@23751
   529
theorem wf_mult: "wf r ==> wf (mult r)"
berghofe@23751
   530
  unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
   531
wenzelm@10249
   532
wenzelm@10249
   533
subsubsection {* Closure-free presentation *}
wenzelm@10249
   534
wenzelm@10249
   535
(*Badly needed: a linear arithmetic procedure for multisets*)
wenzelm@10249
   536
wenzelm@10249
   537
lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
wenzelm@23373
   538
  by (simp add: multiset_eq_conv_count_eq)
wenzelm@10249
   539
wenzelm@10249
   540
text {* One direction. *}
wenzelm@10249
   541
wenzelm@10249
   542
lemma mult_implies_one_step:
berghofe@23751
   543
  "trans r ==> (M, N) \<in> mult r ==>
nipkow@11464
   544
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
berghofe@23751
   545
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
wenzelm@10249
   546
  apply (unfold mult_def mult1_def set_of_def)
berghofe@23751
   547
  apply (erule converse_trancl_induct, clarify)
paulson@15072
   548
   apply (rule_tac x = M0 in exI, simp, clarify)
berghofe@23751
   549
  apply (case_tac "a :# K")
wenzelm@10249
   550
   apply (rule_tac x = I in exI)
wenzelm@10249
   551
   apply (simp (no_asm))
berghofe@23751
   552
   apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
wenzelm@10249
   553
   apply (simp (no_asm_simp) add: union_assoc [symmetric])
nipkow@11464
   554
   apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
wenzelm@10249
   555
   apply (simp add: diff_union_single_conv)
wenzelm@10249
   556
   apply (simp (no_asm_use) add: trans_def)
wenzelm@10249
   557
   apply blast
wenzelm@10249
   558
  apply (subgoal_tac "a :# I")
wenzelm@10249
   559
   apply (rule_tac x = "I - {#a#}" in exI)
wenzelm@10249
   560
   apply (rule_tac x = "J + {#a#}" in exI)
wenzelm@10249
   561
   apply (rule_tac x = "K + Ka" in exI)
wenzelm@10249
   562
   apply (rule conjI)
wenzelm@10249
   563
    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
wenzelm@10249
   564
   apply (rule conjI)
paulson@15072
   565
    apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
wenzelm@10249
   566
    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
wenzelm@10249
   567
   apply (simp (no_asm_use) add: trans_def)
wenzelm@10249
   568
   apply blast
wenzelm@10277
   569
  apply (subgoal_tac "a :# (M0 + {#a#})")
wenzelm@10249
   570
   apply simp
wenzelm@10249
   571
  apply (simp (no_asm))
wenzelm@10249
   572
  done
wenzelm@10249
   573
wenzelm@10249
   574
lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
wenzelm@23373
   575
  by (simp add: multiset_eq_conv_count_eq)
wenzelm@10249
   576
nipkow@11464
   577
lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
wenzelm@10249
   578
  apply (erule size_eq_Suc_imp_elem [THEN exE])
paulson@15072
   579
  apply (drule elem_imp_eq_diff_union, auto)
wenzelm@10249
   580
  done
wenzelm@10249
   581
wenzelm@10249
   582
lemma one_step_implies_mult_aux:
berghofe@23751
   583
  "trans r ==>
berghofe@23751
   584
    \<forall>I J K. (size J = n \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r))
berghofe@23751
   585
      --> (I + K, I + J) \<in> mult r"
paulson@15072
   586
  apply (induct_tac n, auto)
paulson@15072
   587
  apply (frule size_eq_Suc_imp_eq_union, clarify)
paulson@15072
   588
  apply (rename_tac "J'", simp)
paulson@15072
   589
  apply (erule notE, auto)
wenzelm@10249
   590
  apply (case_tac "J' = {#}")
wenzelm@10249
   591
   apply (simp add: mult_def)
berghofe@23751
   592
   apply (rule r_into_trancl)
paulson@15072
   593
   apply (simp add: mult1_def set_of_def, blast)
nipkow@11464
   594
  txt {* Now we know @{term "J' \<noteq> {#}"}. *}
berghofe@23751
   595
  apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@11464
   596
  apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
wenzelm@10249
   597
  apply (erule ssubst)
paulson@15072
   598
  apply (simp add: Ball_def, auto)
wenzelm@10249
   599
  apply (subgoal_tac
berghofe@23751
   600
    "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
berghofe@23751
   601
      (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
wenzelm@10249
   602
   prefer 2
wenzelm@10249
   603
   apply force
wenzelm@10249
   604
  apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
berghofe@23751
   605
  apply (erule trancl_trans)
berghofe@23751
   606
  apply (rule r_into_trancl)
wenzelm@10249
   607
  apply (simp add: mult1_def set_of_def)
wenzelm@10249
   608
  apply (rule_tac x = a in exI)
wenzelm@10249
   609
  apply (rule_tac x = "I + J'" in exI)
wenzelm@10249
   610
  apply (simp add: union_ac)
wenzelm@10249
   611
  done
wenzelm@10249
   612
wenzelm@17161
   613
lemma one_step_implies_mult:
berghofe@23751
   614
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
berghofe@23751
   615
    ==> (I + K, I + J) \<in> mult r"
wenzelm@23373
   616
  using one_step_implies_mult_aux by blast
wenzelm@10249
   617
wenzelm@10249
   618
wenzelm@10249
   619
subsubsection {* Partial-order properties *}
wenzelm@10249
   620
wenzelm@12338
   621
instance multiset :: (type) ord ..
wenzelm@10249
   622
wenzelm@10249
   623
defs (overloaded)
berghofe@23751
   624
  less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
nipkow@11464
   625
  le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
wenzelm@10249
   626
berghofe@23751
   627
lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
wenzelm@18730
   628
  unfolding trans_def by (blast intro: order_less_trans)
wenzelm@10249
   629
wenzelm@10249
   630
text {*
wenzelm@10249
   631
 \medskip Irreflexivity.
wenzelm@10249
   632
*}
wenzelm@10249
   633
wenzelm@10249
   634
lemma mult_irrefl_aux:
wenzelm@18258
   635
    "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) \<Longrightarrow> A = {}"
wenzelm@23373
   636
  by (induct rule: finite_induct) (auto intro: order_less_trans)
wenzelm@10249
   637
wenzelm@17161
   638
lemma mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
paulson@15072
   639
  apply (unfold less_multiset_def, auto)
paulson@15072
   640
  apply (drule trans_base_order [THEN mult_implies_one_step], auto)
wenzelm@10249
   641
  apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
wenzelm@10249
   642
  apply (simp add: set_of_eq_empty_iff)
wenzelm@10249
   643
  done
wenzelm@10249
   644
wenzelm@10249
   645
lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
wenzelm@23373
   646
  using insert mult_less_not_refl by fast
wenzelm@10249
   647
wenzelm@10249
   648
wenzelm@10249
   649
text {* Transitivity. *}
wenzelm@10249
   650
wenzelm@10249
   651
theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
berghofe@23751
   652
  unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
wenzelm@10249
   653
wenzelm@10249
   654
text {* Asymmetry. *}
wenzelm@10249
   655
nipkow@11464
   656
theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
wenzelm@10249
   657
  apply auto
wenzelm@10249
   658
  apply (rule mult_less_not_refl [THEN notE])
paulson@15072
   659
  apply (erule mult_less_trans, assumption)
wenzelm@10249
   660
  done
wenzelm@10249
   661
wenzelm@10249
   662
theorem mult_less_asym:
nipkow@11464
   663
    "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
paulson@15072
   664
  by (insert mult_less_not_sym, blast)
wenzelm@10249
   665
wenzelm@10249
   666
theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
wenzelm@18730
   667
  unfolding le_multiset_def by auto
wenzelm@10249
   668
wenzelm@10249
   669
text {* Anti-symmetry. *}
wenzelm@10249
   670
wenzelm@10249
   671
theorem mult_le_antisym:
wenzelm@10249
   672
    "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
wenzelm@18730
   673
  unfolding le_multiset_def by (blast dest: mult_less_not_sym)
wenzelm@10249
   674
wenzelm@10249
   675
text {* Transitivity. *}
wenzelm@10249
   676
wenzelm@10249
   677
theorem mult_le_trans:
wenzelm@10249
   678
    "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
wenzelm@18730
   679
  unfolding le_multiset_def by (blast intro: mult_less_trans)
wenzelm@10249
   680
wenzelm@11655
   681
theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
wenzelm@18730
   682
  unfolding le_multiset_def by auto
wenzelm@10249
   683
wenzelm@10277
   684
text {* Partial order. *}
wenzelm@10277
   685
wenzelm@10277
   686
instance multiset :: (order) order
wenzelm@10277
   687
  apply intro_classes
berghofe@23751
   688
  apply (rule mult_less_le)
berghofe@23751
   689
  apply (rule mult_le_refl)
berghofe@23751
   690
  apply (erule mult_le_trans, assumption)
berghofe@23751
   691
  apply (erule mult_le_antisym, assumption)
wenzelm@10277
   692
  done
wenzelm@10277
   693
wenzelm@10249
   694
wenzelm@10249
   695
subsubsection {* Monotonicity of multiset union *}
wenzelm@10249
   696
wenzelm@17161
   697
lemma mult1_union:
berghofe@23751
   698
    "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
paulson@15072
   699
  apply (unfold mult1_def, auto)
wenzelm@10249
   700
  apply (rule_tac x = a in exI)
wenzelm@10249
   701
  apply (rule_tac x = "C + M0" in exI)
wenzelm@10249
   702
  apply (simp add: union_assoc)
wenzelm@10249
   703
  done
wenzelm@10249
   704
wenzelm@10249
   705
lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
wenzelm@10249
   706
  apply (unfold less_multiset_def mult_def)
berghofe@23751
   707
  apply (erule trancl_induct)
berghofe@23751
   708
   apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
berghofe@23751
   709
  apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
wenzelm@10249
   710
  done
wenzelm@10249
   711
wenzelm@10249
   712
lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
wenzelm@10249
   713
  apply (subst union_commute [of B C])
wenzelm@10249
   714
  apply (subst union_commute [of D C])
wenzelm@10249
   715
  apply (erule union_less_mono2)
wenzelm@10249
   716
  done
wenzelm@10249
   717
wenzelm@17161
   718
lemma union_less_mono:
wenzelm@10249
   719
    "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
wenzelm@10249
   720
  apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
wenzelm@10249
   721
  done
wenzelm@10249
   722
wenzelm@17161
   723
lemma union_le_mono:
wenzelm@10249
   724
    "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
wenzelm@18730
   725
  unfolding le_multiset_def
wenzelm@18730
   726
  by (blast intro: union_less_mono union_less_mono1 union_less_mono2)
wenzelm@10249
   727
wenzelm@17161
   728
lemma empty_leI [iff]: "{#} <= (M::'a::order multiset)"
wenzelm@10249
   729
  apply (unfold le_multiset_def less_multiset_def)
wenzelm@10249
   730
  apply (case_tac "M = {#}")
wenzelm@10249
   731
   prefer 2
berghofe@23751
   732
   apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
wenzelm@10249
   733
    prefer 2
wenzelm@10249
   734
    apply (rule one_step_implies_mult)
berghofe@23751
   735
      apply (simp only: trans_def, auto)
wenzelm@10249
   736
  done
wenzelm@10249
   737
wenzelm@17161
   738
lemma union_upper1: "A <= A + (B::'a::order multiset)"
paulson@15072
   739
proof -
wenzelm@17200
   740
  have "A + {#} <= A + B" by (blast intro: union_le_mono)
wenzelm@18258
   741
  then show ?thesis by simp
paulson@15072
   742
qed
paulson@15072
   743
wenzelm@17161
   744
lemma union_upper2: "B <= A + (B::'a::order multiset)"
wenzelm@18258
   745
  by (subst union_commute) (rule union_upper1)
paulson@15072
   746
nipkow@23611
   747
instance multiset :: (order) pordered_ab_semigroup_add
nipkow@23611
   748
apply intro_classes
nipkow@23611
   749
apply(erule union_le_mono[OF mult_le_refl])
nipkow@23611
   750
done
paulson@15072
   751
wenzelm@17200
   752
subsection {* Link with lists *}
paulson@15072
   753
wenzelm@17200
   754
consts
paulson@15072
   755
  multiset_of :: "'a list \<Rightarrow> 'a multiset"
paulson@15072
   756
primrec
paulson@15072
   757
  "multiset_of [] = {#}"
paulson@15072
   758
  "multiset_of (a # x) = multiset_of x + {# a #}"
paulson@15072
   759
paulson@15072
   760
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
wenzelm@18258
   761
  by (induct x) auto
paulson@15072
   762
paulson@15072
   763
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
wenzelm@18258
   764
  by (induct x) auto
paulson@15072
   765
paulson@15072
   766
lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x"
wenzelm@18258
   767
  by (induct x) auto
kleing@15867
   768
kleing@15867
   769
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
kleing@15867
   770
  by (induct xs) auto
paulson@15072
   771
wenzelm@18258
   772
lemma multiset_of_append [simp]:
wenzelm@18258
   773
    "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
wenzelm@20503
   774
  by (induct xs arbitrary: ys) (auto simp: union_ac)
wenzelm@18730
   775
paulson@15072
   776
lemma surj_multiset_of: "surj multiset_of"
wenzelm@17200
   777
  apply (unfold surj_def, rule allI)
wenzelm@17200
   778
  apply (rule_tac M=y in multiset_induct, auto)
wenzelm@17200
   779
  apply (rule_tac x = "x # xa" in exI, auto)
wenzelm@10249
   780
  done
wenzelm@10249
   781
nipkow@25162
   782
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
wenzelm@18258
   783
  by (induct x) auto
paulson@15072
   784
wenzelm@17200
   785
lemma distinct_count_atmost_1:
paulson@15072
   786
   "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
wenzelm@18258
   787
   apply (induct x, simp, rule iffI, simp_all)
wenzelm@17200
   788
   apply (rule conjI)
wenzelm@17200
   789
   apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
paulson@15072
   790
   apply (erule_tac x=a in allE, simp, clarify)
wenzelm@17200
   791
   apply (erule_tac x=aa in allE, simp)
paulson@15072
   792
   done
paulson@15072
   793
wenzelm@17200
   794
lemma multiset_of_eq_setD:
kleing@15867
   795
  "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
kleing@15867
   796
  by (rule) (auto simp add:multiset_eq_conv_count_eq set_count_greater_0)
kleing@15867
   797
wenzelm@17200
   798
lemma set_eq_iff_multiset_of_eq_distinct:
wenzelm@17200
   799
  "\<lbrakk>distinct x; distinct y\<rbrakk>
paulson@15072
   800
   \<Longrightarrow> (set x = set y) = (multiset_of x = multiset_of y)"
wenzelm@17200
   801
  by (auto simp: multiset_eq_conv_count_eq distinct_count_atmost_1)
paulson@15072
   802
wenzelm@17200
   803
lemma set_eq_iff_multiset_of_remdups_eq:
paulson@15072
   804
   "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
wenzelm@17200
   805
  apply (rule iffI)
wenzelm@17200
   806
  apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
wenzelm@17200
   807
  apply (drule distinct_remdups[THEN distinct_remdups
wenzelm@17200
   808
                      [THEN set_eq_iff_multiset_of_eq_distinct[THEN iffD2]]])
paulson@15072
   809
  apply simp
wenzelm@10249
   810
  done
wenzelm@10249
   811
wenzelm@18258
   812
lemma multiset_of_compl_union [simp]:
nipkow@23281
   813
    "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
kleing@15630
   814
  by (induct xs) (auto simp: union_ac)
paulson@15072
   815
wenzelm@17200
   816
lemma count_filter:
nipkow@23281
   817
    "count (multiset_of xs) x = length [y \<leftarrow> xs. y = x]"
wenzelm@18258
   818
  by (induct xs) auto
kleing@15867
   819
kleing@15867
   820
paulson@15072
   821
subsection {* Pointwise ordering induced by count *}
paulson@15072
   822
wenzelm@19086
   823
definition
nipkow@23611
   824
mset_le :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<le>#" 50) where
nipkow@23611
   825
"(A \<le># B) = (\<forall>a. count A a \<le> count B a)"
nipkow@23611
   826
definition
nipkow@23611
   827
mset_less :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "<#" 50) where
nipkow@23611
   828
"(A <# B) = (A \<le># B \<and> A \<noteq> B)"
paulson@15072
   829
nipkow@23611
   830
lemma mset_le_refl[simp]: "A \<le># A"
wenzelm@18730
   831
  unfolding mset_le_def by auto
paulson@15072
   832
nipkow@23611
   833
lemma mset_le_trans: "\<lbrakk> A \<le># B; B \<le># C \<rbrakk> \<Longrightarrow> A \<le># C"
wenzelm@18730
   834
  unfolding mset_le_def by (fast intro: order_trans)
paulson@15072
   835
nipkow@23611
   836
lemma mset_le_antisym: "\<lbrakk> A \<le># B; B \<le># A \<rbrakk> \<Longrightarrow> A = B"
wenzelm@17200
   837
  apply (unfold mset_le_def)
wenzelm@17200
   838
  apply (rule multiset_eq_conv_count_eq[THEN iffD2])
paulson@15072
   839
  apply (blast intro: order_antisym)
paulson@15072
   840
  done
paulson@15072
   841
wenzelm@17200
   842
lemma mset_le_exists_conv:
nipkow@23611
   843
  "(A \<le># B) = (\<exists>C. B = A + C)"
nipkow@23611
   844
  apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
paulson@15072
   845
  apply (auto intro: multiset_eq_conv_count_eq [THEN iffD2])
paulson@15072
   846
  done
paulson@15072
   847
nipkow@23611
   848
lemma mset_le_mono_add_right_cancel[simp]: "(A + C \<le># B + C) = (A \<le># B)"
wenzelm@18730
   849
  unfolding mset_le_def by auto
paulson@15072
   850
nipkow@23611
   851
lemma mset_le_mono_add_left_cancel[simp]: "(C + A \<le># C + B) = (A \<le># B)"
wenzelm@18730
   852
  unfolding mset_le_def by auto
paulson@15072
   853
nipkow@23611
   854
lemma mset_le_mono_add: "\<lbrakk> A \<le># B; C \<le># D \<rbrakk> \<Longrightarrow> A + C \<le># B + D"
wenzelm@17200
   855
  apply (unfold mset_le_def)
wenzelm@17200
   856
  apply auto
paulson@15072
   857
  apply (erule_tac x=a in allE)+
paulson@15072
   858
  apply auto
paulson@15072
   859
  done
paulson@15072
   860
nipkow@23611
   861
lemma mset_le_add_left[simp]: "A \<le># A + B"
wenzelm@18730
   862
  unfolding mset_le_def by auto
paulson@15072
   863
nipkow@23611
   864
lemma mset_le_add_right[simp]: "B \<le># A + B"
wenzelm@18730
   865
  unfolding mset_le_def by auto
paulson@15072
   866
nipkow@23611
   867
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le># multiset_of xs"
nipkow@23611
   868
apply (induct xs)
nipkow@23611
   869
 apply auto
nipkow@23611
   870
apply (rule mset_le_trans)
nipkow@23611
   871
 apply auto
nipkow@23611
   872
done
nipkow@23611
   873
haftmann@25208
   874
interpretation mset_order:
haftmann@25208
   875
  order ["op \<le>#" "op <#"]
haftmann@25208
   876
  by (auto intro: order.intro mset_le_refl mset_le_antisym
haftmann@25208
   877
    mset_le_trans simp: mset_less_def)
nipkow@23611
   878
nipkow@23611
   879
interpretation mset_order_cancel_semigroup:
haftmann@25208
   880
  pordered_cancel_ab_semigroup_add ["op \<le>#" "op <#" "op +"]
haftmann@25208
   881
  by unfold_locales (erule mset_le_mono_add [OF mset_le_refl])
nipkow@23611
   882
nipkow@23611
   883
interpretation mset_order_semigroup_cancel:
haftmann@25208
   884
  pordered_ab_semigroup_add_imp_le ["op \<le>#" "op <#" "op +"]
haftmann@25208
   885
  by (unfold_locales) simp
paulson@15072
   886
wenzelm@10249
   887
end