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