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