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