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