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