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