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