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