src/HOL/Library/Multiset.thy
author kleing
Thu Dec 13 06:51:22 2007 +0100 (2007-12-13)
changeset 25610 72e1563aee09
parent 25595 6c48275f9c76
child 25622 6067d838041a
permissions -rw-r--r--
a fold operation for multisets + more lemmas
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
haftmann@25595
     9
imports List
nipkow@15131
    10
begin
wenzelm@10249
    11
wenzelm@10249
    12
subsection {* The type of multisets *}
wenzelm@10249
    13
nipkow@25162
    14
typedef 'a multiset = "{f::'a => nat. finite {x . f x > 0}}"
wenzelm@10249
    15
proof
nipkow@11464
    16
  show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
wenzelm@10249
    17
qed
wenzelm@10249
    18
wenzelm@10249
    19
lemmas multiset_typedef [simp] =
wenzelm@10277
    20
    Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
wenzelm@10277
    21
  and [simp] = Rep_multiset_inject [symmetric]
wenzelm@10249
    22
wenzelm@19086
    23
definition
wenzelm@21404
    24
  Mempty :: "'a multiset"  ("{#}") where
wenzelm@19086
    25
  "{#} = Abs_multiset (\<lambda>a. 0)"
wenzelm@10249
    26
wenzelm@21404
    27
definition
nipkow@25507
    28
  single :: "'a => 'a multiset" where
nipkow@25507
    29
  "single a = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
wenzelm@10249
    30
wenzelm@21404
    31
definition
wenzelm@21404
    32
  count :: "'a multiset => 'a => nat" where
wenzelm@19086
    33
  "count = Rep_multiset"
wenzelm@10249
    34
wenzelm@21404
    35
definition
wenzelm@21404
    36
  MCollect :: "'a multiset => ('a => bool) => 'a multiset" where
wenzelm@19086
    37
  "MCollect M P = Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
wenzelm@19086
    38
wenzelm@19363
    39
abbreviation
wenzelm@21404
    40
  Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
kleing@25610
    41
  "a :# M == 0 < count M a"
kleing@25610
    42
kleing@25610
    43
notation (xsymbols) Melem (infix "\<in>#" 50)
wenzelm@10249
    44
wenzelm@10249
    45
syntax
wenzelm@10249
    46
  "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
wenzelm@10249
    47
translations
wenzelm@20770
    48
  "{#x:M. P#}" == "CONST MCollect M (\<lambda>x. P)"
wenzelm@10249
    49
wenzelm@19086
    50
definition
wenzelm@21404
    51
  set_of :: "'a multiset => 'a set" where
wenzelm@19086
    52
  "set_of M = {x. x :# M}"
wenzelm@10249
    53
haftmann@25571
    54
instantiation multiset :: (type) "{plus, minus, zero, size}" 
haftmann@25571
    55
begin
haftmann@25571
    56
haftmann@25571
    57
definition
nipkow@11464
    58
  union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)"
haftmann@25571
    59
haftmann@25571
    60
definition
nipkow@11464
    61
  diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
haftmann@25571
    62
haftmann@25571
    63
definition
wenzelm@11701
    64
  Zero_multiset_def [simp]: "0 == {#}"
haftmann@25571
    65
haftmann@25571
    66
definition
haftmann@25571
    67
  size_def: "size M == setsum (count M) (set_of M)"
haftmann@25571
    68
haftmann@25571
    69
instance ..
haftmann@25571
    70
haftmann@25571
    71
end
wenzelm@10249
    72
wenzelm@19086
    73
definition
wenzelm@21404
    74
  multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"  (infixl "#\<inter>" 70) where
wenzelm@19086
    75
  "multiset_inter A B = A - (A - B)"
kleing@15869
    76
nipkow@25507
    77
syntax -- "Multiset Enumeration"
nipkow@25507
    78
  "@multiset" :: "args => 'a multiset"    ("{#(_)#}")
nipkow@25507
    79
nipkow@25507
    80
translations
nipkow@25507
    81
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
    82
  "{#x#}" == "CONST single x"
nipkow@25507
    83
wenzelm@10249
    84
wenzelm@10249
    85
text {*
wenzelm@10249
    86
 \medskip Preservation of the representing set @{term multiset}.
wenzelm@10249
    87
*}
wenzelm@10249
    88
nipkow@11464
    89
lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
wenzelm@17161
    90
  by (simp add: multiset_def)
wenzelm@10249
    91
wenzelm@11701
    92
lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
wenzelm@17161
    93
  by (simp add: multiset_def)
wenzelm@10249
    94
wenzelm@10249
    95
lemma union_preserves_multiset [simp]:
nipkow@11464
    96
    "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
wenzelm@17161
    97
  apply (simp add: multiset_def)
wenzelm@17161
    98
  apply (drule (1) finite_UnI)
wenzelm@10249
    99
  apply (simp del: finite_Un add: Un_def)
wenzelm@10249
   100
  done
wenzelm@10249
   101
wenzelm@10249
   102
lemma diff_preserves_multiset [simp]:
nipkow@11464
   103
    "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
wenzelm@17161
   104
  apply (simp add: multiset_def)
wenzelm@10249
   105
  apply (rule finite_subset)
wenzelm@17161
   106
   apply auto
wenzelm@10249
   107
  done
wenzelm@10249
   108
wenzelm@10249
   109
wenzelm@10249
   110
subsection {* Algebraic properties of multisets *}
wenzelm@10249
   111
wenzelm@10249
   112
subsubsection {* Union *}
wenzelm@10249
   113
wenzelm@17161
   114
lemma union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
wenzelm@17161
   115
  by (simp add: union_def Mempty_def)
wenzelm@10249
   116
wenzelm@17161
   117
lemma union_commute: "M + N = N + (M::'a multiset)"
wenzelm@17161
   118
  by (simp add: union_def add_ac)
wenzelm@17161
   119
wenzelm@17161
   120
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
wenzelm@17161
   121
  by (simp add: union_def add_ac)
wenzelm@10249
   122
wenzelm@17161
   123
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
wenzelm@17161
   124
proof -
wenzelm@17161
   125
  have "M + (N + K) = (N + K) + M"
wenzelm@17161
   126
    by (rule union_commute)
wenzelm@17161
   127
  also have "\<dots> = N + (K + M)"
wenzelm@17161
   128
    by (rule union_assoc)
wenzelm@17161
   129
  also have "K + M = M + K"
wenzelm@17161
   130
    by (rule union_commute)
wenzelm@17161
   131
  finally show ?thesis .
wenzelm@17161
   132
qed
wenzelm@10249
   133
wenzelm@17161
   134
lemmas union_ac = union_assoc union_commute union_lcomm
wenzelm@10249
   135
obua@14738
   136
instance multiset :: (type) comm_monoid_add
wenzelm@17200
   137
proof
obua@14722
   138
  fix a b c :: "'a multiset"
obua@14722
   139
  show "(a + b) + c = a + (b + c)" by (rule union_assoc)
obua@14722
   140
  show "a + b = b + a" by (rule union_commute)
obua@14722
   141
  show "0 + a = a" by simp
obua@14722
   142
qed
wenzelm@10277
   143
wenzelm@10249
   144
wenzelm@10249
   145
subsubsection {* Difference *}
wenzelm@10249
   146
wenzelm@17161
   147
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
wenzelm@17161
   148
  by (simp add: Mempty_def diff_def)
wenzelm@10249
   149
wenzelm@17161
   150
lemma diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
wenzelm@17161
   151
  by (simp add: union_def diff_def)
wenzelm@10249
   152
wenzelm@10249
   153
wenzelm@10249
   154
subsubsection {* Count of elements *}
wenzelm@10249
   155
wenzelm@17161
   156
lemma count_empty [simp]: "count {#} a = 0"
wenzelm@17161
   157
  by (simp add: count_def Mempty_def)
wenzelm@10249
   158
wenzelm@17161
   159
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
wenzelm@17161
   160
  by (simp add: count_def single_def)
wenzelm@10249
   161
wenzelm@17161
   162
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
wenzelm@17161
   163
  by (simp add: count_def union_def)
wenzelm@10249
   164
wenzelm@17161
   165
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
wenzelm@17161
   166
  by (simp add: count_def diff_def)
wenzelm@10249
   167
wenzelm@10249
   168
wenzelm@10249
   169
subsubsection {* Set of elements *}
wenzelm@10249
   170
wenzelm@17161
   171
lemma set_of_empty [simp]: "set_of {#} = {}"
wenzelm@17161
   172
  by (simp add: set_of_def)
wenzelm@10249
   173
wenzelm@17161
   174
lemma set_of_single [simp]: "set_of {#b#} = {b}"
wenzelm@17161
   175
  by (simp add: set_of_def)
wenzelm@10249
   176
wenzelm@17161
   177
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
wenzelm@17161
   178
  by (auto simp add: set_of_def)
wenzelm@10249
   179
wenzelm@17161
   180
lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
wenzelm@17161
   181
  by (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
wenzelm@10249
   182
wenzelm@17161
   183
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
wenzelm@17161
   184
  by (auto simp add: set_of_def)
wenzelm@10249
   185
wenzelm@10249
   186
wenzelm@10249
   187
subsubsection {* Size *}
wenzelm@10249
   188
wenzelm@17161
   189
lemma size_empty [simp]: "size {#} = 0"
wenzelm@17161
   190
  by (simp add: size_def)
wenzelm@10249
   191
wenzelm@17161
   192
lemma size_single [simp]: "size {#b#} = 1"
wenzelm@17161
   193
  by (simp add: size_def)
wenzelm@10249
   194
wenzelm@17161
   195
lemma finite_set_of [iff]: "finite (set_of M)"
wenzelm@17161
   196
  using Rep_multiset [of M]
wenzelm@17161
   197
  by (simp add: multiset_def set_of_def count_def)
wenzelm@10249
   198
wenzelm@17161
   199
lemma setsum_count_Int:
nipkow@11464
   200
    "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
wenzelm@18258
   201
  apply (induct rule: finite_induct)
wenzelm@17161
   202
   apply simp
wenzelm@10249
   203
  apply (simp add: Int_insert_left set_of_def)
wenzelm@10249
   204
  done
wenzelm@10249
   205
wenzelm@17161
   206
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
wenzelm@10249
   207
  apply (unfold size_def)
nipkow@11464
   208
  apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
wenzelm@10249
   209
   prefer 2
paulson@15072
   210
   apply (rule ext, simp)
nipkow@15402
   211
  apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
wenzelm@10249
   212
  apply (subst Int_commute)
wenzelm@10249
   213
  apply (simp (no_asm_simp) add: setsum_count_Int)
wenzelm@10249
   214
  done
wenzelm@10249
   215
wenzelm@17161
   216
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
paulson@15072
   217
  apply (unfold size_def Mempty_def count_def, auto)
wenzelm@10249
   218
  apply (simp add: set_of_def count_def expand_fun_eq)
wenzelm@10249
   219
  done
wenzelm@10249
   220
wenzelm@17161
   221
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
wenzelm@10249
   222
  apply (unfold size_def)
paulson@15072
   223
  apply (drule setsum_SucD, auto)
wenzelm@10249
   224
  done
wenzelm@10249
   225
wenzelm@10249
   226
subsubsection {* Equality of multisets *}
wenzelm@10249
   227
wenzelm@17161
   228
lemma multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
wenzelm@17161
   229
  by (simp add: count_def expand_fun_eq)
wenzelm@10249
   230
wenzelm@17161
   231
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
wenzelm@17161
   232
  by (simp add: single_def Mempty_def expand_fun_eq)
wenzelm@10249
   233
wenzelm@17161
   234
lemma single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
wenzelm@17161
   235
  by (auto simp add: single_def expand_fun_eq)
wenzelm@10249
   236
wenzelm@17161
   237
lemma union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
wenzelm@17161
   238
  by (auto simp add: union_def Mempty_def expand_fun_eq)
wenzelm@10249
   239
wenzelm@17161
   240
lemma empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
wenzelm@17161
   241
  by (auto simp add: union_def Mempty_def expand_fun_eq)
wenzelm@10249
   242
wenzelm@17161
   243
lemma union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
wenzelm@17161
   244
  by (simp add: union_def expand_fun_eq)
wenzelm@10249
   245
wenzelm@17161
   246
lemma union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
wenzelm@17161
   247
  by (simp add: union_def expand_fun_eq)
wenzelm@10249
   248
wenzelm@17161
   249
lemma union_is_single:
nipkow@11464
   250
    "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
paulson@15072
   251
  apply (simp add: Mempty_def single_def union_def add_is_1 expand_fun_eq)
wenzelm@10249
   252
  apply blast
wenzelm@10249
   253
  done
wenzelm@10249
   254
wenzelm@17161
   255
lemma single_is_union:
paulson@15072
   256
     "({#a#} = M + N) = ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
wenzelm@10249
   257
  apply (unfold Mempty_def single_def union_def)
nipkow@11464
   258
  apply (simp add: add_is_1 one_is_add expand_fun_eq)
wenzelm@10249
   259
  apply (blast dest: sym)
wenzelm@10249
   260
  done
wenzelm@10249
   261
wenzelm@17161
   262
lemma add_eq_conv_diff:
wenzelm@10249
   263
  "(M + {#a#} = N + {#b#}) =
paulson@15072
   264
   (M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
wenzelm@24035
   265
  using [[simproc del: neq]]
wenzelm@10249
   266
  apply (unfold single_def union_def diff_def)
wenzelm@10249
   267
  apply (simp (no_asm) add: expand_fun_eq)
paulson@15072
   268
  apply (rule conjI, force, safe, simp_all)
berghofe@13601
   269
  apply (simp add: eq_sym_conv)
wenzelm@10249
   270
  done
wenzelm@10249
   271
kleing@15869
   272
declare Rep_multiset_inject [symmetric, simp del]
kleing@15869
   273
nipkow@23611
   274
instance multiset :: (type) cancel_ab_semigroup_add
nipkow@23611
   275
proof
nipkow@23611
   276
  fix a b c :: "'a multiset"
nipkow@23611
   277
  show "a + b = a + c \<Longrightarrow> b = c" by simp
nipkow@23611
   278
qed
kleing@15869
   279
kleing@25610
   280
lemma insert_DiffM:
kleing@25610
   281
  "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
kleing@25610
   282
  by (clarsimp simp: multiset_eq_conv_count_eq)
kleing@25610
   283
kleing@25610
   284
lemma insert_DiffM2[simp]:
kleing@25610
   285
  "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
kleing@25610
   286
  by (clarsimp simp: multiset_eq_conv_count_eq)
kleing@25610
   287
kleing@25610
   288
lemma multi_union_self_other_eq: 
kleing@25610
   289
  "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
kleing@25610
   290
  by (induct A arbitrary: X Y, auto)
kleing@25610
   291
kleing@25610
   292
lemma multi_self_add_other_not_self[simp]: "(A = A + {#x#}) = False"
kleing@25610
   293
proof -
kleing@25610
   294
  {
kleing@25610
   295
    assume a: "A = A + {#x#}"
kleing@25610
   296
    have "A = A + {#}" by simp
kleing@25610
   297
    hence "A + {#} = A + {#x#}" using a by auto 
kleing@25610
   298
    hence "{#} = {#x#}"
kleing@25610
   299
      by - (drule multi_union_self_other_eq)
kleing@25610
   300
    hence False by auto
kleing@25610
   301
  }
kleing@25610
   302
  thus ?thesis by blast
kleing@25610
   303
qed
kleing@25610
   304
kleing@25610
   305
lemma insert_noteq_member: 
kleing@25610
   306
  assumes BC: "B + {#b#} = C + {#c#}"
kleing@25610
   307
   and bnotc: "b \<noteq> c"
kleing@25610
   308
  shows "c \<in># B"
kleing@25610
   309
proof -
kleing@25610
   310
  have "c \<in># C + {#c#}" by simp
kleing@25610
   311
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
kleing@25610
   312
  hence "c \<in># B + {#b#}" using BC by simp
kleing@25610
   313
  thus "c \<in># B" using nc by simp
kleing@25610
   314
qed
kleing@25610
   315
kleing@25610
   316
kleing@15869
   317
subsubsection {* Intersection *}
kleing@15869
   318
kleing@15869
   319
lemma multiset_inter_count:
wenzelm@17161
   320
    "count (A #\<inter> B) x = min (count A x) (count B x)"
wenzelm@17161
   321
  by (simp add: multiset_inter_def min_def)
kleing@15869
   322
kleing@15869
   323
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
wenzelm@17200
   324
  by (simp add: multiset_eq_conv_count_eq multiset_inter_count
haftmann@21214
   325
    min_max.inf_commute)
kleing@15869
   326
kleing@15869
   327
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
wenzelm@17200
   328
  by (simp add: multiset_eq_conv_count_eq multiset_inter_count
haftmann@21214
   329
    min_max.inf_assoc)
kleing@15869
   330
kleing@15869
   331
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
kleing@15869
   332
  by (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def)
kleing@15869
   333
wenzelm@17161
   334
lemmas multiset_inter_ac =
wenzelm@17161
   335
  multiset_inter_commute
wenzelm@17161
   336
  multiset_inter_assoc
wenzelm@17161
   337
  multiset_inter_left_commute
kleing@15869
   338
kleing@15869
   339
lemma multiset_union_diff_commute: "B #\<inter> C = {#} \<Longrightarrow> A + B - C = A - C + B"
wenzelm@17200
   340
  apply (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def
wenzelm@17161
   341
    split: split_if_asm)
kleing@15869
   342
  apply clarsimp
wenzelm@17161
   343
  apply (erule_tac x = a in allE)
kleing@15869
   344
  apply auto
kleing@15869
   345
  done
kleing@15869
   346
wenzelm@10249
   347
wenzelm@10249
   348
subsection {* Induction over multisets *}
wenzelm@10249
   349
wenzelm@10249
   350
lemma setsum_decr:
wenzelm@11701
   351
  "finite F ==> (0::nat) < f a ==>
paulson@15072
   352
    setsum (f (a := f a - 1)) F = (if a\<in>F then setsum f F - 1 else setsum f F)"
wenzelm@18258
   353
  apply (induct rule: finite_induct)
wenzelm@18258
   354
   apply auto
paulson@15072
   355
  apply (drule_tac a = a in mk_disjoint_insert, auto)
wenzelm@10249
   356
  done
wenzelm@10249
   357
wenzelm@10313
   358
lemma rep_multiset_induct_aux:
wenzelm@18730
   359
  assumes 1: "P (\<lambda>a. (0::nat))"
wenzelm@18730
   360
    and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))"
nipkow@25134
   361
  shows "\<forall>f. f \<in> multiset --> setsum f {x. f x \<noteq> 0} = n --> P f"
wenzelm@18730
   362
  apply (unfold multiset_def)
wenzelm@18730
   363
  apply (induct_tac n, simp, clarify)
wenzelm@18730
   364
   apply (subgoal_tac "f = (\<lambda>a.0)")
wenzelm@18730
   365
    apply simp
wenzelm@18730
   366
    apply (rule 1)
wenzelm@18730
   367
   apply (rule ext, force, clarify)
wenzelm@18730
   368
  apply (frule setsum_SucD, clarify)
wenzelm@18730
   369
  apply (rename_tac a)
nipkow@25162
   370
  apply (subgoal_tac "finite {x. (f (a := f a - 1)) x > 0}")
wenzelm@18730
   371
   prefer 2
wenzelm@18730
   372
   apply (rule finite_subset)
wenzelm@18730
   373
    prefer 2
wenzelm@18730
   374
    apply assumption
wenzelm@18730
   375
   apply simp
wenzelm@18730
   376
   apply blast
wenzelm@18730
   377
  apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
wenzelm@18730
   378
   prefer 2
wenzelm@18730
   379
   apply (rule ext)
wenzelm@18730
   380
   apply (simp (no_asm_simp))
wenzelm@18730
   381
   apply (erule ssubst, rule 2 [unfolded multiset_def], blast)
wenzelm@18730
   382
  apply (erule allE, erule impE, erule_tac [2] mp, blast)
wenzelm@18730
   383
  apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
nipkow@25134
   384
  apply (subgoal_tac "{x. x \<noteq> a --> f x \<noteq> 0} = {x. f x \<noteq> 0}")
wenzelm@18730
   385
   prefer 2
wenzelm@18730
   386
   apply blast
nipkow@25134
   387
  apply (subgoal_tac "{x. x \<noteq> a \<and> f x \<noteq> 0} = {x. f x \<noteq> 0} - {a}")
wenzelm@18730
   388
   prefer 2
wenzelm@18730
   389
   apply blast
wenzelm@18730
   390
  apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong)
wenzelm@18730
   391
  done
wenzelm@10249
   392
wenzelm@10313
   393
theorem rep_multiset_induct:
nipkow@11464
   394
  "f \<in> multiset ==> P (\<lambda>a. 0) ==>
wenzelm@11701
   395
    (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
wenzelm@17161
   396
  using rep_multiset_induct_aux by blast
wenzelm@10249
   397
wenzelm@18258
   398
theorem multiset_induct [case_names empty add, induct type: multiset]:
wenzelm@18258
   399
  assumes empty: "P {#}"
wenzelm@18258
   400
    and add: "!!M x. P M ==> P (M + {#x#})"
wenzelm@17161
   401
  shows "P M"
wenzelm@10249
   402
proof -
wenzelm@10249
   403
  note defns = union_def single_def Mempty_def
wenzelm@10249
   404
  show ?thesis
wenzelm@10249
   405
    apply (rule Rep_multiset_inverse [THEN subst])
wenzelm@10313
   406
    apply (rule Rep_multiset [THEN rep_multiset_induct])
wenzelm@18258
   407
     apply (rule empty [unfolded defns])
paulson@15072
   408
    apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))")
wenzelm@10249
   409
     prefer 2
wenzelm@10249
   410
     apply (simp add: expand_fun_eq)
wenzelm@10249
   411
    apply (erule ssubst)
wenzelm@17200
   412
    apply (erule Abs_multiset_inverse [THEN subst])
wenzelm@18258
   413
    apply (erule add [unfolded defns, simplified])
wenzelm@10249
   414
    done
wenzelm@10249
   415
qed
wenzelm@10249
   416
kleing@25610
   417
lemma empty_multiset_count:
kleing@25610
   418
  "(\<forall>x. count A x = 0) = (A = {#})"
kleing@25610
   419
  apply (rule iffI)
kleing@25610
   420
   apply (induct A, simp)
kleing@25610
   421
   apply (erule_tac x=x in allE)
kleing@25610
   422
   apply auto
kleing@25610
   423
  done
kleing@25610
   424
kleing@25610
   425
subsection {* Case splits *}
kleing@25610
   426
kleing@25610
   427
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
kleing@25610
   428
  by (induct M, auto)
kleing@25610
   429
kleing@25610
   430
lemma multiset_cases [cases type, case_names empty add]:
kleing@25610
   431
  assumes em:  "M = {#} \<Longrightarrow> P"
kleing@25610
   432
  assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P"
kleing@25610
   433
  shows "P"
kleing@25610
   434
proof (cases "M = {#}")
kleing@25610
   435
  assume "M = {#}" thus ?thesis using em by simp
kleing@25610
   436
next
kleing@25610
   437
  assume "M \<noteq> {#}"
kleing@25610
   438
  then obtain M' m where "M = M' + {#m#}" 
kleing@25610
   439
    by (blast dest: multi_nonempty_split)
kleing@25610
   440
  thus ?thesis using add by simp
kleing@25610
   441
qed
kleing@25610
   442
kleing@25610
   443
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
kleing@25610
   444
  apply (cases M, simp)
kleing@25610
   445
  apply (rule_tac x="M - {#x#}" in exI, simp)
kleing@25610
   446
  done
kleing@25610
   447
wenzelm@10249
   448
lemma MCollect_preserves_multiset:
nipkow@11464
   449
    "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
wenzelm@10249
   450
  apply (simp add: multiset_def)
paulson@15072
   451
  apply (rule finite_subset, auto)
wenzelm@10249
   452
  done
wenzelm@10249
   453
wenzelm@17161
   454
lemma count_MCollect [simp]:
wenzelm@10249
   455
    "count {# x:M. P x #} a = (if P a then count M a else 0)"
paulson@15072
   456
  by (simp add: count_def MCollect_def MCollect_preserves_multiset)
wenzelm@10249
   457
wenzelm@17161
   458
lemma set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
wenzelm@17161
   459
  by (auto simp add: set_of_def)
wenzelm@10249
   460
wenzelm@17161
   461
lemma multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
wenzelm@17161
   462
  by (subst multiset_eq_conv_count_eq, auto)
wenzelm@10249
   463
wenzelm@17161
   464
lemma add_eq_conv_ex:
wenzelm@17161
   465
  "(M + {#a#} = N + {#b#}) =
wenzelm@17161
   466
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
paulson@15072
   467
  by (auto simp add: add_eq_conv_diff)
wenzelm@10249
   468
kleing@15869
   469
declare multiset_typedef [simp del]
wenzelm@10249
   470
kleing@25610
   471
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
kleing@25610
   472
  apply (rule iffI)
kleing@25610
   473
   apply (case_tac "size S = 0")
kleing@25610
   474
    apply clarsimp
kleing@25610
   475
   apply (subst (asm) neq0_conv)
kleing@25610
   476
   apply auto
kleing@25610
   477
  done
kleing@25610
   478
kleing@25610
   479
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
kleing@25610
   480
  by (cases "B={#}", auto dest: multi_member_split)
wenzelm@17161
   481
wenzelm@10249
   482
subsection {* Multiset orderings *}
wenzelm@10249
   483
wenzelm@10249
   484
subsubsection {* Well-foundedness *}
wenzelm@10249
   485
wenzelm@19086
   486
definition
berghofe@23751
   487
  mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
wenzelm@19086
   488
  "mult1 r =
berghofe@23751
   489
    {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
berghofe@23751
   490
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
wenzelm@10249
   491
wenzelm@21404
   492
definition
berghofe@23751
   493
  mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
berghofe@23751
   494
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
   495
berghofe@23751
   496
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
wenzelm@10277
   497
  by (simp add: mult1_def)
wenzelm@10249
   498
berghofe@23751
   499
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
berghofe@23751
   500
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
berghofe@23751
   501
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@19582
   502
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
wenzelm@10249
   503
proof (unfold mult1_def)
berghofe@23751
   504
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
nipkow@11464
   505
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
berghofe@23751
   506
  let ?case1 = "?case1 {(N, M). ?R N M}"
wenzelm@10249
   507
berghofe@23751
   508
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
wenzelm@18258
   509
  then have "\<exists>a' M0' K.
nipkow@11464
   510
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
wenzelm@18258
   511
  then show "?case1 \<or> ?case2"
wenzelm@10249
   512
  proof (elim exE conjE)
wenzelm@10249
   513
    fix a' M0' K
wenzelm@10249
   514
    assume N: "N = M0' + K" and r: "?r K a'"
wenzelm@10249
   515
    assume "M0 + {#a#} = M0' + {#a'#}"
wenzelm@18258
   516
    then have "M0 = M0' \<and> a = a' \<or>
nipkow@11464
   517
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
wenzelm@10249
   518
      by (simp only: add_eq_conv_ex)
wenzelm@18258
   519
    then show ?thesis
wenzelm@10249
   520
    proof (elim disjE conjE exE)
wenzelm@10249
   521
      assume "M0 = M0'" "a = a'"
nipkow@11464
   522
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@18258
   523
      then have ?case2 .. then show ?thesis ..
wenzelm@10249
   524
    next
wenzelm@10249
   525
      fix K'
wenzelm@10249
   526
      assume "M0' = K' + {#a#}"
wenzelm@10249
   527
      with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
wenzelm@10249
   528
wenzelm@10249
   529
      assume "M0 = K' + {#a'#}"
wenzelm@10249
   530
      with r have "?R (K' + K) M0" by blast
wenzelm@18258
   531
      with n have ?case1 by simp then show ?thesis ..
wenzelm@10249
   532
    qed
wenzelm@10249
   533
  qed
wenzelm@10249
   534
qed
wenzelm@10249
   535
berghofe@23751
   536
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
wenzelm@10249
   537
proof
wenzelm@10249
   538
  let ?R = "mult1 r"
wenzelm@10249
   539
  let ?W = "acc ?R"
wenzelm@10249
   540
  {
wenzelm@10249
   541
    fix M M0 a
berghofe@23751
   542
    assume M0: "M0 \<in> ?W"
berghofe@23751
   543
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
   544
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
berghofe@23751
   545
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
   546
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
   547
      fix N
berghofe@23751
   548
      assume "(N, M0 + {#a#}) \<in> ?R"
berghofe@23751
   549
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
berghofe@23751
   550
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
wenzelm@10249
   551
        by (rule less_add)
berghofe@23751
   552
      then show "N \<in> ?W"
wenzelm@10249
   553
      proof (elim exE disjE conjE)
berghofe@23751
   554
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
berghofe@23751
   555
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
berghofe@23751
   556
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
berghofe@23751
   557
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
   558
      next
wenzelm@10249
   559
        fix K
wenzelm@10249
   560
        assume N: "N = M0 + K"
berghofe@23751
   561
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
berghofe@23751
   562
        then have "M0 + K \<in> ?W"
wenzelm@10249
   563
        proof (induct K)
wenzelm@18730
   564
          case empty
berghofe@23751
   565
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
   566
        next
wenzelm@18730
   567
          case (add K x)
berghofe@23751
   568
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
   569
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
   570
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
   571
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
berghofe@23751
   572
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
wenzelm@10249
   573
        qed
berghofe@23751
   574
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
   575
      qed
wenzelm@10249
   576
    qed
wenzelm@10249
   577
  } note tedious_reasoning = this
wenzelm@10249
   578
berghofe@23751
   579
  assume wf: "wf r"
wenzelm@10249
   580
  fix M
berghofe@23751
   581
  show "M \<in> ?W"
wenzelm@10249
   582
  proof (induct M)
berghofe@23751
   583
    show "{#} \<in> ?W"
wenzelm@10249
   584
    proof (rule accI)
berghofe@23751
   585
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
   586
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
   587
    qed
wenzelm@10249
   588
berghofe@23751
   589
    fix M a assume "M \<in> ?W"
berghofe@23751
   590
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
   591
    proof induct
wenzelm@10249
   592
      fix a
berghofe@23751
   593
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
   594
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
   595
      proof
berghofe@23751
   596
        fix M assume "M \<in> ?W"
berghofe@23751
   597
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
   598
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
   599
      qed
wenzelm@10249
   600
    qed
berghofe@23751
   601
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
wenzelm@10249
   602
  qed
wenzelm@10249
   603
qed
wenzelm@10249
   604
berghofe@23751
   605
theorem wf_mult1: "wf r ==> wf (mult1 r)"
wenzelm@23373
   606
  by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
   607
berghofe@23751
   608
theorem wf_mult: "wf r ==> wf (mult r)"
berghofe@23751
   609
  unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
   610
wenzelm@10249
   611
wenzelm@10249
   612
subsubsection {* Closure-free presentation *}
wenzelm@10249
   613
wenzelm@10249
   614
(*Badly needed: a linear arithmetic procedure for multisets*)
wenzelm@10249
   615
wenzelm@10249
   616
lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
wenzelm@23373
   617
  by (simp add: multiset_eq_conv_count_eq)
wenzelm@10249
   618
wenzelm@10249
   619
text {* One direction. *}
wenzelm@10249
   620
wenzelm@10249
   621
lemma mult_implies_one_step:
berghofe@23751
   622
  "trans r ==> (M, N) \<in> mult r ==>
nipkow@11464
   623
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
berghofe@23751
   624
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
wenzelm@10249
   625
  apply (unfold mult_def mult1_def set_of_def)
berghofe@23751
   626
  apply (erule converse_trancl_induct, clarify)
paulson@15072
   627
   apply (rule_tac x = M0 in exI, simp, clarify)
berghofe@23751
   628
  apply (case_tac "a :# K")
wenzelm@10249
   629
   apply (rule_tac x = I in exI)
wenzelm@10249
   630
   apply (simp (no_asm))
berghofe@23751
   631
   apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
wenzelm@10249
   632
   apply (simp (no_asm_simp) add: union_assoc [symmetric])
nipkow@11464
   633
   apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
wenzelm@10249
   634
   apply (simp add: diff_union_single_conv)
wenzelm@10249
   635
   apply (simp (no_asm_use) add: trans_def)
wenzelm@10249
   636
   apply blast
wenzelm@10249
   637
  apply (subgoal_tac "a :# I")
wenzelm@10249
   638
   apply (rule_tac x = "I - {#a#}" in exI)
wenzelm@10249
   639
   apply (rule_tac x = "J + {#a#}" in exI)
wenzelm@10249
   640
   apply (rule_tac x = "K + Ka" in exI)
wenzelm@10249
   641
   apply (rule conjI)
wenzelm@10249
   642
    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
wenzelm@10249
   643
   apply (rule conjI)
paulson@15072
   644
    apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
wenzelm@10249
   645
    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
wenzelm@10249
   646
   apply (simp (no_asm_use) add: trans_def)
wenzelm@10249
   647
   apply blast
wenzelm@10277
   648
  apply (subgoal_tac "a :# (M0 + {#a#})")
wenzelm@10249
   649
   apply simp
wenzelm@10249
   650
  apply (simp (no_asm))
wenzelm@10249
   651
  done
wenzelm@10249
   652
wenzelm@10249
   653
lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
wenzelm@23373
   654
  by (simp add: multiset_eq_conv_count_eq)
wenzelm@10249
   655
nipkow@11464
   656
lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
wenzelm@10249
   657
  apply (erule size_eq_Suc_imp_elem [THEN exE])
paulson@15072
   658
  apply (drule elem_imp_eq_diff_union, auto)
wenzelm@10249
   659
  done
wenzelm@10249
   660
wenzelm@10249
   661
lemma one_step_implies_mult_aux:
berghofe@23751
   662
  "trans r ==>
berghofe@23751
   663
    \<forall>I J K. (size J = n \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r))
berghofe@23751
   664
      --> (I + K, I + J) \<in> mult r"
paulson@15072
   665
  apply (induct_tac n, auto)
paulson@15072
   666
  apply (frule size_eq_Suc_imp_eq_union, clarify)
paulson@15072
   667
  apply (rename_tac "J'", simp)
paulson@15072
   668
  apply (erule notE, auto)
wenzelm@10249
   669
  apply (case_tac "J' = {#}")
wenzelm@10249
   670
   apply (simp add: mult_def)
berghofe@23751
   671
   apply (rule r_into_trancl)
paulson@15072
   672
   apply (simp add: mult1_def set_of_def, blast)
nipkow@11464
   673
  txt {* Now we know @{term "J' \<noteq> {#}"}. *}
berghofe@23751
   674
  apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@11464
   675
  apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
wenzelm@10249
   676
  apply (erule ssubst)
paulson@15072
   677
  apply (simp add: Ball_def, auto)
wenzelm@10249
   678
  apply (subgoal_tac
berghofe@23751
   679
    "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
berghofe@23751
   680
      (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
wenzelm@10249
   681
   prefer 2
wenzelm@10249
   682
   apply force
wenzelm@10249
   683
  apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
berghofe@23751
   684
  apply (erule trancl_trans)
berghofe@23751
   685
  apply (rule r_into_trancl)
wenzelm@10249
   686
  apply (simp add: mult1_def set_of_def)
wenzelm@10249
   687
  apply (rule_tac x = a in exI)
wenzelm@10249
   688
  apply (rule_tac x = "I + J'" in exI)
wenzelm@10249
   689
  apply (simp add: union_ac)
wenzelm@10249
   690
  done
wenzelm@10249
   691
wenzelm@17161
   692
lemma one_step_implies_mult:
berghofe@23751
   693
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
berghofe@23751
   694
    ==> (I + K, I + J) \<in> mult r"
wenzelm@23373
   695
  using one_step_implies_mult_aux by blast
wenzelm@10249
   696
wenzelm@10249
   697
wenzelm@10249
   698
subsubsection {* Partial-order properties *}
wenzelm@10249
   699
wenzelm@12338
   700
instance multiset :: (type) ord ..
wenzelm@10249
   701
wenzelm@10249
   702
defs (overloaded)
berghofe@23751
   703
  less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
nipkow@11464
   704
  le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
wenzelm@10249
   705
berghofe@23751
   706
lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
wenzelm@18730
   707
  unfolding trans_def by (blast intro: order_less_trans)
wenzelm@10249
   708
wenzelm@10249
   709
text {*
wenzelm@10249
   710
 \medskip Irreflexivity.
wenzelm@10249
   711
*}
wenzelm@10249
   712
wenzelm@10249
   713
lemma mult_irrefl_aux:
wenzelm@18258
   714
    "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) \<Longrightarrow> A = {}"
wenzelm@23373
   715
  by (induct rule: finite_induct) (auto intro: order_less_trans)
wenzelm@10249
   716
wenzelm@17161
   717
lemma mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
paulson@15072
   718
  apply (unfold less_multiset_def, auto)
paulson@15072
   719
  apply (drule trans_base_order [THEN mult_implies_one_step], auto)
wenzelm@10249
   720
  apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
wenzelm@10249
   721
  apply (simp add: set_of_eq_empty_iff)
wenzelm@10249
   722
  done
wenzelm@10249
   723
wenzelm@10249
   724
lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
wenzelm@23373
   725
  using insert mult_less_not_refl by fast
wenzelm@10249
   726
wenzelm@10249
   727
wenzelm@10249
   728
text {* Transitivity. *}
wenzelm@10249
   729
wenzelm@10249
   730
theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
berghofe@23751
   731
  unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
wenzelm@10249
   732
wenzelm@10249
   733
text {* Asymmetry. *}
wenzelm@10249
   734
nipkow@11464
   735
theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
wenzelm@10249
   736
  apply auto
wenzelm@10249
   737
  apply (rule mult_less_not_refl [THEN notE])
paulson@15072
   738
  apply (erule mult_less_trans, assumption)
wenzelm@10249
   739
  done
wenzelm@10249
   740
wenzelm@10249
   741
theorem mult_less_asym:
nipkow@11464
   742
    "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
paulson@15072
   743
  by (insert mult_less_not_sym, blast)
wenzelm@10249
   744
wenzelm@10249
   745
theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
wenzelm@18730
   746
  unfolding le_multiset_def by auto
wenzelm@10249
   747
wenzelm@10249
   748
text {* Anti-symmetry. *}
wenzelm@10249
   749
wenzelm@10249
   750
theorem mult_le_antisym:
wenzelm@10249
   751
    "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
wenzelm@18730
   752
  unfolding le_multiset_def by (blast dest: mult_less_not_sym)
wenzelm@10249
   753
wenzelm@10249
   754
text {* Transitivity. *}
wenzelm@10249
   755
wenzelm@10249
   756
theorem mult_le_trans:
wenzelm@10249
   757
    "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
wenzelm@18730
   758
  unfolding le_multiset_def by (blast intro: mult_less_trans)
wenzelm@10249
   759
wenzelm@11655
   760
theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
wenzelm@18730
   761
  unfolding le_multiset_def by auto
wenzelm@10249
   762
wenzelm@10277
   763
text {* Partial order. *}
wenzelm@10277
   764
wenzelm@10277
   765
instance multiset :: (order) order
wenzelm@10277
   766
  apply intro_classes
berghofe@23751
   767
  apply (rule mult_less_le)
berghofe@23751
   768
  apply (rule mult_le_refl)
berghofe@23751
   769
  apply (erule mult_le_trans, assumption)
berghofe@23751
   770
  apply (erule mult_le_antisym, assumption)
wenzelm@10277
   771
  done
wenzelm@10277
   772
wenzelm@10249
   773
wenzelm@10249
   774
subsubsection {* Monotonicity of multiset union *}
wenzelm@10249
   775
wenzelm@17161
   776
lemma mult1_union:
berghofe@23751
   777
    "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
paulson@15072
   778
  apply (unfold mult1_def, auto)
wenzelm@10249
   779
  apply (rule_tac x = a in exI)
wenzelm@10249
   780
  apply (rule_tac x = "C + M0" in exI)
wenzelm@10249
   781
  apply (simp add: union_assoc)
wenzelm@10249
   782
  done
wenzelm@10249
   783
wenzelm@10249
   784
lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
wenzelm@10249
   785
  apply (unfold less_multiset_def mult_def)
berghofe@23751
   786
  apply (erule trancl_induct)
berghofe@23751
   787
   apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
berghofe@23751
   788
  apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
wenzelm@10249
   789
  done
wenzelm@10249
   790
wenzelm@10249
   791
lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
wenzelm@10249
   792
  apply (subst union_commute [of B C])
wenzelm@10249
   793
  apply (subst union_commute [of D C])
wenzelm@10249
   794
  apply (erule union_less_mono2)
wenzelm@10249
   795
  done
wenzelm@10249
   796
wenzelm@17161
   797
lemma union_less_mono:
wenzelm@10249
   798
    "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
wenzelm@10249
   799
  apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
wenzelm@10249
   800
  done
wenzelm@10249
   801
wenzelm@17161
   802
lemma union_le_mono:
wenzelm@10249
   803
    "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
wenzelm@18730
   804
  unfolding le_multiset_def
wenzelm@18730
   805
  by (blast intro: union_less_mono union_less_mono1 union_less_mono2)
wenzelm@10249
   806
wenzelm@17161
   807
lemma empty_leI [iff]: "{#} <= (M::'a::order multiset)"
wenzelm@10249
   808
  apply (unfold le_multiset_def less_multiset_def)
wenzelm@10249
   809
  apply (case_tac "M = {#}")
wenzelm@10249
   810
   prefer 2
berghofe@23751
   811
   apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
wenzelm@10249
   812
    prefer 2
wenzelm@10249
   813
    apply (rule one_step_implies_mult)
berghofe@23751
   814
      apply (simp only: trans_def, auto)
wenzelm@10249
   815
  done
wenzelm@10249
   816
wenzelm@17161
   817
lemma union_upper1: "A <= A + (B::'a::order multiset)"
paulson@15072
   818
proof -
wenzelm@17200
   819
  have "A + {#} <= A + B" by (blast intro: union_le_mono)
wenzelm@18258
   820
  then show ?thesis by simp
paulson@15072
   821
qed
paulson@15072
   822
wenzelm@17161
   823
lemma union_upper2: "B <= A + (B::'a::order multiset)"
wenzelm@18258
   824
  by (subst union_commute) (rule union_upper1)
paulson@15072
   825
nipkow@23611
   826
instance multiset :: (order) pordered_ab_semigroup_add
nipkow@23611
   827
apply intro_classes
nipkow@23611
   828
apply(erule union_le_mono[OF mult_le_refl])
nipkow@23611
   829
done
paulson@15072
   830
wenzelm@17200
   831
subsection {* Link with lists *}
paulson@15072
   832
wenzelm@17200
   833
consts
paulson@15072
   834
  multiset_of :: "'a list \<Rightarrow> 'a multiset"
paulson@15072
   835
primrec
paulson@15072
   836
  "multiset_of [] = {#}"
paulson@15072
   837
  "multiset_of (a # x) = multiset_of x + {# a #}"
paulson@15072
   838
paulson@15072
   839
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
wenzelm@18258
   840
  by (induct x) auto
paulson@15072
   841
paulson@15072
   842
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
wenzelm@18258
   843
  by (induct x) auto
paulson@15072
   844
paulson@15072
   845
lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x"
wenzelm@18258
   846
  by (induct x) auto
kleing@15867
   847
kleing@15867
   848
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
kleing@15867
   849
  by (induct xs) auto
paulson@15072
   850
wenzelm@18258
   851
lemma multiset_of_append [simp]:
wenzelm@18258
   852
    "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
wenzelm@20503
   853
  by (induct xs arbitrary: ys) (auto simp: union_ac)
wenzelm@18730
   854
paulson@15072
   855
lemma surj_multiset_of: "surj multiset_of"
wenzelm@17200
   856
  apply (unfold surj_def, rule allI)
wenzelm@17200
   857
  apply (rule_tac M=y in multiset_induct, auto)
wenzelm@17200
   858
  apply (rule_tac x = "x # xa" in exI, auto)
wenzelm@10249
   859
  done
wenzelm@10249
   860
nipkow@25162
   861
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
wenzelm@18258
   862
  by (induct x) auto
paulson@15072
   863
wenzelm@17200
   864
lemma distinct_count_atmost_1:
paulson@15072
   865
   "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
wenzelm@18258
   866
   apply (induct x, simp, rule iffI, simp_all)
wenzelm@17200
   867
   apply (rule conjI)
wenzelm@17200
   868
   apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
paulson@15072
   869
   apply (erule_tac x=a in allE, simp, clarify)
wenzelm@17200
   870
   apply (erule_tac x=aa in allE, simp)
paulson@15072
   871
   done
paulson@15072
   872
wenzelm@17200
   873
lemma multiset_of_eq_setD:
kleing@15867
   874
  "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
kleing@15867
   875
  by (rule) (auto simp add:multiset_eq_conv_count_eq set_count_greater_0)
kleing@15867
   876
wenzelm@17200
   877
lemma set_eq_iff_multiset_of_eq_distinct:
wenzelm@17200
   878
  "\<lbrakk>distinct x; distinct y\<rbrakk>
paulson@15072
   879
   \<Longrightarrow> (set x = set y) = (multiset_of x = multiset_of y)"
wenzelm@17200
   880
  by (auto simp: multiset_eq_conv_count_eq distinct_count_atmost_1)
paulson@15072
   881
wenzelm@17200
   882
lemma set_eq_iff_multiset_of_remdups_eq:
paulson@15072
   883
   "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
wenzelm@17200
   884
  apply (rule iffI)
wenzelm@17200
   885
  apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
wenzelm@17200
   886
  apply (drule distinct_remdups[THEN distinct_remdups
wenzelm@17200
   887
                      [THEN set_eq_iff_multiset_of_eq_distinct[THEN iffD2]]])
paulson@15072
   888
  apply simp
wenzelm@10249
   889
  done
wenzelm@10249
   890
wenzelm@18258
   891
lemma multiset_of_compl_union [simp]:
nipkow@23281
   892
    "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
kleing@15630
   893
  by (induct xs) (auto simp: union_ac)
paulson@15072
   894
wenzelm@17200
   895
lemma count_filter:
nipkow@23281
   896
    "count (multiset_of xs) x = length [y \<leftarrow> xs. y = x]"
wenzelm@18258
   897
  by (induct xs) auto
kleing@15867
   898
kleing@15867
   899
paulson@15072
   900
subsection {* Pointwise ordering induced by count *}
paulson@15072
   901
wenzelm@19086
   902
definition
kleing@25610
   903
  mset_le :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<le>#" 50) where
kleing@25610
   904
  "(A \<le># B) = (\<forall>a. count A a \<le> count B a)"
nipkow@23611
   905
definition
kleing@25610
   906
  mset_less :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "<#" 50) where
kleing@25610
   907
  "(A <# B) = (A \<le># B \<and> A \<noteq> B)"
kleing@25610
   908
kleing@25610
   909
notation mset_le (infix "\<subseteq>#" 50)
kleing@25610
   910
notation mset_less (infix "\<subset>#" 50)
paulson@15072
   911
nipkow@23611
   912
lemma mset_le_refl[simp]: "A \<le># A"
wenzelm@18730
   913
  unfolding mset_le_def by auto
paulson@15072
   914
nipkow@23611
   915
lemma mset_le_trans: "\<lbrakk> A \<le># B; B \<le># C \<rbrakk> \<Longrightarrow> A \<le># C"
wenzelm@18730
   916
  unfolding mset_le_def by (fast intro: order_trans)
paulson@15072
   917
nipkow@23611
   918
lemma mset_le_antisym: "\<lbrakk> A \<le># B; B \<le># A \<rbrakk> \<Longrightarrow> A = B"
wenzelm@17200
   919
  apply (unfold mset_le_def)
wenzelm@17200
   920
  apply (rule multiset_eq_conv_count_eq[THEN iffD2])
paulson@15072
   921
  apply (blast intro: order_antisym)
paulson@15072
   922
  done
paulson@15072
   923
wenzelm@17200
   924
lemma mset_le_exists_conv:
nipkow@23611
   925
  "(A \<le># B) = (\<exists>C. B = A + C)"
nipkow@23611
   926
  apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
paulson@15072
   927
  apply (auto intro: multiset_eq_conv_count_eq [THEN iffD2])
paulson@15072
   928
  done
paulson@15072
   929
nipkow@23611
   930
lemma mset_le_mono_add_right_cancel[simp]: "(A + C \<le># B + C) = (A \<le># B)"
wenzelm@18730
   931
  unfolding mset_le_def by auto
paulson@15072
   932
nipkow@23611
   933
lemma mset_le_mono_add_left_cancel[simp]: "(C + A \<le># C + B) = (A \<le># B)"
wenzelm@18730
   934
  unfolding mset_le_def by auto
paulson@15072
   935
nipkow@23611
   936
lemma mset_le_mono_add: "\<lbrakk> A \<le># B; C \<le># D \<rbrakk> \<Longrightarrow> A + C \<le># B + D"
wenzelm@17200
   937
  apply (unfold mset_le_def)
wenzelm@17200
   938
  apply auto
paulson@15072
   939
  apply (erule_tac x=a in allE)+
paulson@15072
   940
  apply auto
paulson@15072
   941
  done
paulson@15072
   942
nipkow@23611
   943
lemma mset_le_add_left[simp]: "A \<le># A + B"
wenzelm@18730
   944
  unfolding mset_le_def by auto
paulson@15072
   945
nipkow@23611
   946
lemma mset_le_add_right[simp]: "B \<le># A + B"
wenzelm@18730
   947
  unfolding mset_le_def by auto
paulson@15072
   948
nipkow@23611
   949
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le># multiset_of xs"
nipkow@23611
   950
apply (induct xs)
nipkow@23611
   951
 apply auto
nipkow@23611
   952
apply (rule mset_le_trans)
nipkow@23611
   953
 apply auto
nipkow@23611
   954
done
nipkow@23611
   955
haftmann@25208
   956
interpretation mset_order:
haftmann@25208
   957
  order ["op \<le>#" "op <#"]
haftmann@25208
   958
  by (auto intro: order.intro mset_le_refl mset_le_antisym
haftmann@25208
   959
    mset_le_trans simp: mset_less_def)
nipkow@23611
   960
nipkow@23611
   961
interpretation mset_order_cancel_semigroup:
haftmann@25208
   962
  pordered_cancel_ab_semigroup_add ["op \<le>#" "op <#" "op +"]
haftmann@25208
   963
  by unfold_locales (erule mset_le_mono_add [OF mset_le_refl])
nipkow@23611
   964
nipkow@23611
   965
interpretation mset_order_semigroup_cancel:
haftmann@25208
   966
  pordered_ab_semigroup_add_imp_le ["op \<le>#" "op <#" "op +"]
haftmann@25208
   967
  by (unfold_locales) simp
paulson@15072
   968
kleing@25610
   969
kleing@25610
   970
lemma mset_lessD:
kleing@25610
   971
  "\<lbrakk> A \<subset># B ; x \<in># A \<rbrakk> \<Longrightarrow> x \<in># B"
kleing@25610
   972
  apply (clarsimp simp: mset_le_def mset_less_def)
kleing@25610
   973
  apply (erule_tac x=x in allE)
kleing@25610
   974
  apply auto
kleing@25610
   975
  done
kleing@25610
   976
kleing@25610
   977
lemma mset_leD:
kleing@25610
   978
  "\<lbrakk> A \<subseteq># B ; x \<in># A \<rbrakk> \<Longrightarrow> x \<in># B"
kleing@25610
   979
  apply (clarsimp simp: mset_le_def mset_less_def)
kleing@25610
   980
  apply (erule_tac x=x in allE)
kleing@25610
   981
  apply auto
kleing@25610
   982
  done
kleing@25610
   983
  
kleing@25610
   984
lemma mset_less_insertD:
kleing@25610
   985
  "(A + {#x#} \<subset># B) \<Longrightarrow> (x \<in># B \<and> A \<subset># B)"
kleing@25610
   986
  apply (rule conjI)
kleing@25610
   987
   apply (simp add: mset_lessD)
kleing@25610
   988
  apply (clarsimp simp: mset_le_def mset_less_def)
kleing@25610
   989
  apply safe
kleing@25610
   990
   apply (erule_tac x=a in allE)
kleing@25610
   991
   apply (auto split: split_if_asm)
kleing@25610
   992
  done
kleing@25610
   993
kleing@25610
   994
lemma mset_le_insertD:
kleing@25610
   995
  "(A + {#x#} \<subseteq># B) \<Longrightarrow> (x \<in># B \<and> A \<subseteq># B)"
kleing@25610
   996
  apply (rule conjI)
kleing@25610
   997
   apply (simp add: mset_leD)
kleing@25610
   998
  apply (force simp: mset_le_def mset_less_def split: split_if_asm)
kleing@25610
   999
  done
kleing@25610
  1000
kleing@25610
  1001
lemma mset_less_of_empty[simp]: "A \<subset># {#} = False" 
kleing@25610
  1002
  by (induct A, auto simp: mset_le_def mset_less_def)
kleing@25610
  1003
kleing@25610
  1004
lemma multi_psub_of_add_self[simp]: "A \<subset># A + {#x#}"
kleing@25610
  1005
  by (clarsimp simp: mset_le_def mset_less_def)
kleing@25610
  1006
kleing@25610
  1007
lemma multi_psub_self[simp]: "A \<subset># A = False"
kleing@25610
  1008
  by (clarsimp simp: mset_le_def mset_less_def)
kleing@25610
  1009
kleing@25610
  1010
lemma mset_less_add_bothsides:
kleing@25610
  1011
  "T + {#x#} \<subset># S + {#x#} \<Longrightarrow> T \<subset># S"
kleing@25610
  1012
  by (clarsimp simp: mset_le_def mset_less_def)
kleing@25610
  1013
kleing@25610
  1014
lemma mset_less_empty_nonempty: "({#} \<subset># S) = (S \<noteq> {#})"
kleing@25610
  1015
  by (auto simp: mset_le_def mset_less_def)
kleing@25610
  1016
kleing@25610
  1017
lemma mset_less_size: "A \<subset># B \<Longrightarrow> size A < size B"
kleing@25610
  1018
proof (induct A arbitrary: B)
kleing@25610
  1019
  case (empty M)
kleing@25610
  1020
  hence "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
kleing@25610
  1021
  then obtain M' x where "M = M' + {#x#}" 
kleing@25610
  1022
    by (blast dest: multi_nonempty_split)
kleing@25610
  1023
  thus ?case by simp
kleing@25610
  1024
next
kleing@25610
  1025
  case (add S x T)
kleing@25610
  1026
  have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact
kleing@25610
  1027
  have SxsubT: "S + {#x#} \<subset># T" by fact
kleing@25610
  1028
  hence "x \<in># T" and "S \<subset># T" by (auto dest: mset_less_insertD)
kleing@25610
  1029
  then obtain T' where T: "T = T' + {#x#}" 
kleing@25610
  1030
    by (blast dest: multi_member_split)
kleing@25610
  1031
  hence "S \<subset># T'" using SxsubT 
kleing@25610
  1032
    by (blast intro: mset_less_add_bothsides)
kleing@25610
  1033
  hence "size S < size T'" using IH by simp
kleing@25610
  1034
  thus ?case using T by simp
kleing@25610
  1035
qed
kleing@25610
  1036
kleing@25610
  1037
lemmas mset_less_trans = mset_order.less_eq_less.less_trans
kleing@25610
  1038
kleing@25610
  1039
lemma mset_less_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} \<subset># B"
kleing@25610
  1040
  by (auto simp: mset_le_def mset_less_def multi_drop_mem_not_eq)
kleing@25610
  1041
kleing@25610
  1042
subsection {* Strong induction and subset induction for multisets *}
kleing@25610
  1043
kleing@25610
  1044
subsubsection {* Well-foundedness of proper subset operator *}
kleing@25610
  1045
kleing@25610
  1046
definition
kleing@25610
  1047
  mset_less_rel  :: "('a multiset * 'a multiset) set" 
kleing@25610
  1048
  where
kleing@25610
  1049
  --{* proper multiset subset *}
kleing@25610
  1050
  "mset_less_rel \<equiv> {(A,B). A \<subset># B}"
kleing@25610
  1051
kleing@25610
  1052
lemma multiset_add_sub_el_shuffle: 
kleing@25610
  1053
  assumes cinB: "c \<in># B" and bnotc: "b \<noteq> c" 
kleing@25610
  1054
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
kleing@25610
  1055
proof -
kleing@25610
  1056
  from cinB obtain A where B: "B = A + {#c#}" 
kleing@25610
  1057
    by (blast dest: multi_member_split)
kleing@25610
  1058
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
kleing@25610
  1059
  hence "A + {#b#} = A + {#c#} + {#b#} - {#c#}" 
kleing@25610
  1060
    by (simp add: union_ac)
kleing@25610
  1061
  thus ?thesis using B by simp
kleing@25610
  1062
qed
kleing@25610
  1063
kleing@25610
  1064
lemma wf_mset_less_rel: "wf mset_less_rel"
kleing@25610
  1065
  apply (unfold mset_less_rel_def)
kleing@25610
  1066
  apply (rule wf_measure [THEN wf_subset, where f1=size])
kleing@25610
  1067
  apply (clarsimp simp: measure_def inv_image_def mset_less_size)
kleing@25610
  1068
  done
kleing@25610
  1069
kleing@25610
  1070
subsubsection {* The induction rules *}
kleing@25610
  1071
kleing@25610
  1072
lemma full_multiset_induct [case_names less]:
kleing@25610
  1073
  assumes ih: "\<And>B. \<forall>A. A \<subset># B \<longrightarrow> P A \<Longrightarrow> P B"
kleing@25610
  1074
  shows "P B"
kleing@25610
  1075
  apply (rule wf_mset_less_rel [THEN wf_induct])
kleing@25610
  1076
  apply (rule ih, auto simp: mset_less_rel_def)
kleing@25610
  1077
  done
kleing@25610
  1078
kleing@25610
  1079
lemma multi_subset_induct [consumes 2, case_names empty add]:
kleing@25610
  1080
  assumes "F \<subseteq># A"
kleing@25610
  1081
    and empty: "P {#}"
kleing@25610
  1082
    and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
kleing@25610
  1083
  shows "P F"
kleing@25610
  1084
proof -
kleing@25610
  1085
  from `F \<subseteq># A`
kleing@25610
  1086
  show ?thesis
kleing@25610
  1087
  proof (induct F)
kleing@25610
  1088
    show "P {#}" by fact
kleing@25610
  1089
  next
kleing@25610
  1090
    fix x F
kleing@25610
  1091
    assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "F + {#x#} \<subseteq># A"
kleing@25610
  1092
    show "P (F + {#x#})"
kleing@25610
  1093
    proof (rule insert)
kleing@25610
  1094
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
kleing@25610
  1095
      from i have "F \<subseteq># A" by (auto simp: mset_le_insertD)
kleing@25610
  1096
      with P show "P F" .
kleing@25610
  1097
    qed
kleing@25610
  1098
  qed
kleing@25610
  1099
qed 
kleing@25610
  1100
kleing@25610
  1101
subsection {* Multiset extensionality *}
kleing@25610
  1102
kleing@25610
  1103
lemma multi_count_eq: 
kleing@25610
  1104
  "(\<forall>x. count A x = count B x) = (A = B)"
kleing@25610
  1105
  apply (rule iffI)
kleing@25610
  1106
   prefer 2
kleing@25610
  1107
   apply clarsimp 
kleing@25610
  1108
  apply (induct A arbitrary: B rule: full_multiset_induct)
kleing@25610
  1109
  apply (rename_tac C)
kleing@25610
  1110
  apply (case_tac B rule: multiset_cases)
kleing@25610
  1111
   apply (simp add: empty_multiset_count)
kleing@25610
  1112
  apply simp
kleing@25610
  1113
  apply (case_tac "x \<in># C")
kleing@25610
  1114
   apply (force dest: multi_member_split)
kleing@25610
  1115
  apply (erule_tac x=x in allE)
kleing@25610
  1116
  apply simp
kleing@25610
  1117
  done
kleing@25610
  1118
kleing@25610
  1119
lemmas multi_count_ext = multi_count_eq [THEN iffD1, rule_format]
kleing@25610
  1120
kleing@25610
  1121
subsection {* The fold combinator *}
kleing@25610
  1122
kleing@25610
  1123
text {* The intended behaviour is
kleing@25610
  1124
@{text "foldM f z {#x\<^isub>1, ..., x\<^isub>n#} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
kleing@25610
  1125
if @{text f} is associative-commutative. 
kleing@25610
  1126
*}
kleing@25610
  1127
kleing@25610
  1128
(* the graph of foldM, z = the start element, f = folding function, 
kleing@25610
  1129
   A the multiset, y the result *)
kleing@25610
  1130
inductive 
kleing@25610
  1131
  foldMG :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b \<Rightarrow> bool" 
kleing@25610
  1132
  for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" 
kleing@25610
  1133
  and z :: 'b
kleing@25610
  1134
where
kleing@25610
  1135
  emptyI [intro]:  "foldMG f z {#} z"
kleing@25610
  1136
| insertI [intro]: "foldMG f z A y \<Longrightarrow> foldMG f z (A + {#x#}) (f x y)"
kleing@25610
  1137
kleing@25610
  1138
inductive_cases empty_foldMGE [elim!]: "foldMG f z {#} x"
kleing@25610
  1139
inductive_cases insert_foldMGE: "foldMG f z (A + {#}) y" 
kleing@25610
  1140
kleing@25610
  1141
definition
kleing@25610
  1142
  foldM :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
kleing@25610
  1143
where
kleing@25610
  1144
  "foldM f z A \<equiv> THE x. foldMG f z A x"
kleing@25610
  1145
kleing@25610
  1146
lemma Diff1_foldMG:
kleing@25610
  1147
  "\<lbrakk> foldMG f z (A - {#x#}) y; x \<in># A \<rbrakk> \<Longrightarrow> foldMG f z A (f x y)"
kleing@25610
  1148
  by (frule_tac x=x in foldMG.insertI, auto)
kleing@25610
  1149
kleing@25610
  1150
lemma foldMG_nonempty: "\<exists>x. foldMG f z A x"
kleing@25610
  1151
  apply (induct A)
kleing@25610
  1152
   apply blast
kleing@25610
  1153
  apply clarsimp
kleing@25610
  1154
  apply (drule_tac x=x in foldMG.insertI)
kleing@25610
  1155
  apply auto
kleing@25610
  1156
  done
kleing@25610
  1157
kleing@25610
  1158
lemma foldM_empty[simp]: "foldM f z {#} = z"
kleing@25610
  1159
  by (unfold foldM_def, blast)
kleing@25610
  1160
kleing@25610
  1161
locale left_commutative = 
kleing@25610
  1162
  fixes f :: "'a => 'b => 'b"    (infixl "\<cdot>" 70)
kleing@25610
  1163
  assumes left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
kleing@25610
  1164
kleing@25610
  1165
lemma (in left_commutative) foldMG_determ:
kleing@25610
  1166
  "\<lbrakk>foldMG f z A x; foldMG f z A y\<rbrakk> \<Longrightarrow> y = x"
kleing@25610
  1167
proof (induct arbitrary: x y z rule: full_multiset_induct)
kleing@25610
  1168
  case (less M x\<^isub>1 x\<^isub>2 Z)
kleing@25610
  1169
  have IH: "\<forall>A. A \<subset># M \<longrightarrow> 
kleing@25610
  1170
    (\<forall>x x' x''. foldMG op \<cdot> x'' A x \<longrightarrow> foldMG op \<cdot> x'' A x'
kleing@25610
  1171
               \<longrightarrow> x' = x)" by fact
kleing@25610
  1172
  have Mfoldx\<^isub>1: "foldMG f Z M x\<^isub>1" and Mfoldx\<^isub>2: "foldMG f Z M x\<^isub>2" by fact+
kleing@25610
  1173
  show ?case
kleing@25610
  1174
  proof (rule foldMG.cases [OF Mfoldx\<^isub>1])
kleing@25610
  1175
    assume "M = {#}" and "x\<^isub>1 = Z"
kleing@25610
  1176
    thus ?case using Mfoldx\<^isub>2 by auto 
kleing@25610
  1177
  next
kleing@25610
  1178
    fix B b u
kleing@25610
  1179
    assume "M = B + {#b#}" and "x\<^isub>1 = b \<cdot> u" and Bu: "foldMG op \<cdot> Z B u"
kleing@25610
  1180
    hence MBb: "M = B + {#b#}" and x\<^isub>1: "x\<^isub>1 = b \<cdot> u" by auto
kleing@25610
  1181
    show ?case
kleing@25610
  1182
    proof (rule foldMG.cases [OF Mfoldx\<^isub>2])
kleing@25610
  1183
      assume "M = {#}" "x\<^isub>2 = Z"
kleing@25610
  1184
      thus ?case using Mfoldx\<^isub>1 by auto
kleing@25610
  1185
    next
kleing@25610
  1186
      fix C c v
kleing@25610
  1187
      assume "M = C + {#c#}" and "x\<^isub>2 = c \<cdot> v" and Cv: "foldMG op \<cdot> Z C v"
kleing@25610
  1188
      hence MCc: "M = C + {#c#}" and x\<^isub>2: "x\<^isub>2 = c \<cdot> v" by auto
kleing@25610
  1189
      hence CsubM: "C \<subset># M" by simp
kleing@25610
  1190
      from MBb have BsubM: "B \<subset># M" by simp
kleing@25610
  1191
      show ?case
kleing@25610
  1192
      proof cases
kleing@25610
  1193
        assume "b=c"
kleing@25610
  1194
        then moreover have "B = C" using MBb MCc by auto
kleing@25610
  1195
        ultimately show ?thesis using Bu Cv x\<^isub>1 x\<^isub>2 CsubM IH by auto
kleing@25610
  1196
      next
kleing@25610
  1197
        assume diff: "b \<noteq> c"
kleing@25610
  1198
        let ?D = "B - {#c#}"
kleing@25610
  1199
        have cinB: "c \<in># B" and binC: "b \<in># C" using MBb MCc diff
kleing@25610
  1200
          by (auto intro: insert_noteq_member dest: sym)
kleing@25610
  1201
        have "B - {#c#} \<subset># B" using cinB by (rule mset_less_diff_self)
kleing@25610
  1202
        hence DsubM: "?D \<subset># M" using BsubM by (blast intro: mset_less_trans)
kleing@25610
  1203
        from MBb MCc have "B + {#b#} = C + {#c#}" by blast
kleing@25610
  1204
        hence [simp]: "B + {#b#} - {#c#} = C"
kleing@25610
  1205
          using MBb MCc binC cinB by auto
kleing@25610
  1206
        have B: "B = ?D + {#c#}" and C: "C = ?D + {#b#}"
kleing@25610
  1207
          using MBb MCc diff binC cinB
kleing@25610
  1208
          by (auto simp: multiset_add_sub_el_shuffle)
kleing@25610
  1209
        then obtain d where Dfoldd: "foldMG f Z ?D d"
kleing@25610
  1210
          using foldMG_nonempty by iprover
kleing@25610
  1211
        hence "foldMG f Z B (c \<cdot> d)" using cinB
kleing@25610
  1212
          by (rule Diff1_foldMG)
kleing@25610
  1213
        hence "c \<cdot> d = u" using IH BsubM Bu by blast
kleing@25610
  1214
        moreover 
kleing@25610
  1215
        have "foldMG f Z C (b \<cdot> d)" using binC cinB diff Dfoldd
kleing@25610
  1216
          by (auto simp: multiset_add_sub_el_shuffle 
kleing@25610
  1217
            dest: foldMG.insertI [where x=b])
kleing@25610
  1218
        hence "b \<cdot> d = v" using IH CsubM Cv by blast
kleing@25610
  1219
        ultimately show ?thesis using x\<^isub>1 x\<^isub>2
kleing@25610
  1220
          by (auto simp: left_commute)
kleing@25610
  1221
      qed
kleing@25610
  1222
    qed
kleing@25610
  1223
  qed
kleing@25610
  1224
qed
kleing@25610
  1225
        
kleing@25610
  1226
lemma (in left_commutative) foldM_insert_aux: "
kleing@25610
  1227
    (foldMG f z (A + {#x#}) v) =
kleing@25610
  1228
    (\<exists>y. foldMG f z A y \<and> v = f x y)"
kleing@25610
  1229
  apply (rule iffI)
kleing@25610
  1230
   prefer 2
kleing@25610
  1231
   apply blast
kleing@25610
  1232
  apply (rule_tac A=A and f=f in foldMG_nonempty [THEN exE, standard])
kleing@25610
  1233
  apply (blast intro: foldMG_determ)
kleing@25610
  1234
  done
kleing@25610
  1235
kleing@25610
  1236
lemma (in left_commutative) foldM_equality: "foldMG f z A y \<Longrightarrow> foldM f z A = y"
kleing@25610
  1237
  by (unfold foldM_def) (blast intro: foldMG_determ)
kleing@25610
  1238
kleing@25610
  1239
lemma (in left_commutative) foldM_insert[simp]:
kleing@25610
  1240
  "foldM f z (A + {#x#}) = f x (foldM f z A)"
kleing@25610
  1241
  apply (simp add: foldM_def foldM_insert_aux union_commute)  
kleing@25610
  1242
  apply (rule the_equality)
kleing@25610
  1243
  apply (auto cong add: conj_cong 
kleing@25610
  1244
              simp add: foldM_def [symmetric] foldM_equality foldMG_nonempty)
kleing@25610
  1245
  done
kleing@25610
  1246
kleing@25610
  1247
lemma (in left_commutative) foldM_insert_idem:
kleing@25610
  1248
  shows "foldM f z (A + {#a#}) = a \<cdot> foldM f z A"
kleing@25610
  1249
  apply (simp add: foldM_def foldM_insert_aux)
kleing@25610
  1250
  apply (rule the_equality)
kleing@25610
  1251
  apply (auto cong add: conj_cong 
kleing@25610
  1252
              simp add: foldM_def [symmetric] foldM_equality foldMG_nonempty)
kleing@25610
  1253
  done
kleing@25610
  1254
kleing@25610
  1255
lemma (in left_commutative) foldM_fusion:
kleing@25610
  1256
  includes left_commutative g
kleing@25610
  1257
  shows "\<lbrakk>\<And>x y. h (g x y) = f x (h y) \<rbrakk> \<Longrightarrow> h (foldM g w A) = foldM f (h w) A"
kleing@25610
  1258
  by (induct A, auto)
kleing@25610
  1259
kleing@25610
  1260
lemma (in left_commutative) foldM_commute:
kleing@25610
  1261
  "f x (foldM f z A) = foldM f (f x z) A"
kleing@25610
  1262
  by (induct A, auto simp: left_commute [of x])
kleing@25610
  1263
  
kleing@25610
  1264
lemma (in left_commutative) foldM_rec:
kleing@25610
  1265
  assumes a: "a \<in># A" 
kleing@25610
  1266
  shows "foldM f z A = f a (foldM f z (A - {#a#}))"
kleing@25610
  1267
proof -
kleing@25610
  1268
  from a obtain A' where "A = A' + {#a#}" by (blast dest: multi_member_split)
kleing@25610
  1269
  thus ?thesis by simp
kleing@25610
  1270
qed
kleing@25610
  1271
kleing@25610
  1272
wenzelm@10249
  1273
end