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