src/HOL/Library/Multiset.thy
author kuncar
Thu Mar 29 21:13:48 2012 +0200 (2012-03-29)
changeset 47198 cfd8ff62eab1
parent 47179 54b38de0620e
child 47308 9caab698dbe4
permissions -rw-r--r--
use qualified names for rsp and rep_eq theorems in quotient_def
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
haftmann@34943
     5
header {* (Finite) multisets *}
wenzelm@10249
     6
nipkow@15131
     7
theory Multiset
bulwahn@46237
     8
imports Main DAList
nipkow@15131
     9
begin
wenzelm@10249
    10
wenzelm@10249
    11
subsection {* The type of multisets *}
wenzelm@10249
    12
wenzelm@45694
    13
definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
wenzelm@45694
    14
wenzelm@45694
    15
typedef (open) 'a multiset = "multiset :: ('a => nat) set"
haftmann@34943
    16
  morphisms count Abs_multiset
wenzelm@45694
    17
  unfolding multiset_def
wenzelm@10249
    18
proof
wenzelm@45694
    19
  show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
wenzelm@10249
    20
qed
wenzelm@10249
    21
haftmann@34943
    22
lemmas multiset_typedef = Abs_multiset_inverse count_inverse count
wenzelm@19086
    23
haftmann@28708
    24
abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
kleing@25610
    25
  "a :# M == 0 < count M a"
kleing@25610
    26
wenzelm@26145
    27
notation (xsymbols)
wenzelm@26145
    28
  Melem (infix "\<in>#" 50)
wenzelm@10249
    29
nipkow@39302
    30
lemma multiset_eq_iff:
haftmann@34943
    31
  "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
nipkow@39302
    32
  by (simp only: count_inject [symmetric] fun_eq_iff)
haftmann@34943
    33
nipkow@39302
    34
lemma multiset_eqI:
haftmann@34943
    35
  "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
nipkow@39302
    36
  using multiset_eq_iff by auto
haftmann@34943
    37
haftmann@34943
    38
text {*
haftmann@34943
    39
 \medskip Preservation of the representing set @{term multiset}.
haftmann@34943
    40
*}
haftmann@34943
    41
haftmann@34943
    42
lemma const0_in_multiset:
haftmann@34943
    43
  "(\<lambda>a. 0) \<in> multiset"
haftmann@34943
    44
  by (simp add: multiset_def)
haftmann@34943
    45
haftmann@34943
    46
lemma only1_in_multiset:
haftmann@34943
    47
  "(\<lambda>b. if b = a then n else 0) \<in> multiset"
haftmann@34943
    48
  by (simp add: multiset_def)
haftmann@34943
    49
haftmann@34943
    50
lemma union_preserves_multiset:
haftmann@34943
    51
  "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
haftmann@34943
    52
  by (simp add: multiset_def)
haftmann@34943
    53
haftmann@34943
    54
lemma diff_preserves_multiset:
haftmann@34943
    55
  assumes "M \<in> multiset"
haftmann@34943
    56
  shows "(\<lambda>a. M a - N a) \<in> multiset"
haftmann@34943
    57
proof -
haftmann@34943
    58
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    59
    by auto
haftmann@34943
    60
  with assms show ?thesis
haftmann@34943
    61
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    62
qed
haftmann@34943
    63
haftmann@41069
    64
lemma filter_preserves_multiset:
haftmann@34943
    65
  assumes "M \<in> multiset"
haftmann@34943
    66
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
haftmann@34943
    67
proof -
haftmann@34943
    68
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    69
    by auto
haftmann@34943
    70
  with assms show ?thesis
haftmann@34943
    71
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    72
qed
haftmann@34943
    73
haftmann@34943
    74
lemmas in_multiset = const0_in_multiset only1_in_multiset
haftmann@41069
    75
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
haftmann@34943
    76
haftmann@34943
    77
haftmann@34943
    78
subsection {* Representing multisets *}
haftmann@34943
    79
haftmann@34943
    80
text {* Multiset enumeration *}
haftmann@34943
    81
haftmann@34943
    82
instantiation multiset :: (type) "{zero, plus}"
haftmann@25571
    83
begin
haftmann@25571
    84
haftmann@34943
    85
definition Mempty_def:
haftmann@34943
    86
  "0 = Abs_multiset (\<lambda>a. 0)"
haftmann@25571
    87
haftmann@34943
    88
abbreviation Mempty :: "'a multiset" ("{#}") where
haftmann@34943
    89
  "Mempty \<equiv> 0"
haftmann@25571
    90
haftmann@34943
    91
definition union_def:
haftmann@34943
    92
  "M + N = Abs_multiset (\<lambda>a. count M a + count N a)"
haftmann@25571
    93
haftmann@25571
    94
instance ..
haftmann@25571
    95
haftmann@25571
    96
end
wenzelm@10249
    97
haftmann@34943
    98
definition single :: "'a => 'a multiset" where
haftmann@34943
    99
  "single a = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
kleing@15869
   100
wenzelm@26145
   101
syntax
wenzelm@26176
   102
  "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
nipkow@25507
   103
translations
nipkow@25507
   104
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
   105
  "{#x#}" == "CONST single x"
nipkow@25507
   106
haftmann@34943
   107
lemma count_empty [simp]: "count {#} a = 0"
haftmann@34943
   108
  by (simp add: Mempty_def in_multiset multiset_typedef)
wenzelm@10249
   109
haftmann@34943
   110
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
haftmann@34943
   111
  by (simp add: single_def in_multiset multiset_typedef)
nipkow@29901
   112
wenzelm@10249
   113
haftmann@34943
   114
subsection {* Basic operations *}
wenzelm@10249
   115
wenzelm@10249
   116
subsubsection {* Union *}
wenzelm@10249
   117
haftmann@34943
   118
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
haftmann@34943
   119
  by (simp add: union_def in_multiset multiset_typedef)
wenzelm@10249
   120
wenzelm@46921
   121
instance multiset :: (type) cancel_comm_monoid_add
wenzelm@46921
   122
  by default (simp_all add: multiset_eq_iff)
wenzelm@10277
   123
wenzelm@10249
   124
wenzelm@10249
   125
subsubsection {* Difference *}
wenzelm@10249
   126
haftmann@34943
   127
instantiation multiset :: (type) minus
haftmann@34943
   128
begin
haftmann@34943
   129
haftmann@34943
   130
definition diff_def:
haftmann@34943
   131
  "M - N = Abs_multiset (\<lambda>a. count M a - count N a)"
haftmann@34943
   132
haftmann@34943
   133
instance ..
haftmann@34943
   134
haftmann@34943
   135
end
haftmann@34943
   136
haftmann@34943
   137
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
haftmann@34943
   138
  by (simp add: diff_def in_multiset multiset_typedef)
haftmann@34943
   139
wenzelm@17161
   140
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
nipkow@39302
   141
by(simp add: multiset_eq_iff)
nipkow@36903
   142
nipkow@36903
   143
lemma diff_cancel[simp]: "A - A = {#}"
nipkow@39302
   144
by (rule multiset_eqI) simp
wenzelm@10249
   145
nipkow@36903
   146
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
nipkow@39302
   147
by(simp add: multiset_eq_iff)
wenzelm@10249
   148
nipkow@36903
   149
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
nipkow@39302
   150
by(simp add: multiset_eq_iff)
haftmann@34943
   151
haftmann@34943
   152
lemma insert_DiffM:
haftmann@34943
   153
  "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
nipkow@39302
   154
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   155
haftmann@34943
   156
lemma insert_DiffM2 [simp]:
haftmann@34943
   157
  "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
nipkow@39302
   158
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   159
haftmann@34943
   160
lemma diff_right_commute:
haftmann@34943
   161
  "(M::'a multiset) - N - Q = M - Q - N"
nipkow@39302
   162
  by (auto simp add: multiset_eq_iff)
nipkow@36903
   163
nipkow@36903
   164
lemma diff_add:
nipkow@36903
   165
  "(M::'a multiset) - (N + Q) = M - N - Q"
nipkow@39302
   166
by (simp add: multiset_eq_iff)
haftmann@34943
   167
haftmann@34943
   168
lemma diff_union_swap:
haftmann@34943
   169
  "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
nipkow@39302
   170
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   171
haftmann@34943
   172
lemma diff_union_single_conv:
haftmann@34943
   173
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
nipkow@39302
   174
  by (simp add: multiset_eq_iff)
bulwahn@26143
   175
wenzelm@10249
   176
haftmann@34943
   177
subsubsection {* Equality of multisets *}
haftmann@34943
   178
haftmann@34943
   179
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
nipkow@39302
   180
  by (simp add: multiset_eq_iff)
haftmann@34943
   181
haftmann@34943
   182
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
nipkow@39302
   183
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   184
haftmann@34943
   185
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   186
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   187
haftmann@34943
   188
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   189
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   190
haftmann@34943
   191
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
nipkow@39302
   192
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   193
haftmann@34943
   194
lemma diff_single_trivial:
haftmann@34943
   195
  "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
nipkow@39302
   196
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   197
haftmann@34943
   198
lemma diff_single_eq_union:
haftmann@34943
   199
  "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
haftmann@34943
   200
  by auto
haftmann@34943
   201
haftmann@34943
   202
lemma union_single_eq_diff:
haftmann@34943
   203
  "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
haftmann@34943
   204
  by (auto dest: sym)
haftmann@34943
   205
haftmann@34943
   206
lemma union_single_eq_member:
haftmann@34943
   207
  "M + {#x#} = N \<Longrightarrow> x \<in># N"
haftmann@34943
   208
  by auto
haftmann@34943
   209
haftmann@34943
   210
lemma union_is_single:
wenzelm@46730
   211
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")
wenzelm@46730
   212
proof
haftmann@34943
   213
  assume ?rhs then show ?lhs by auto
haftmann@34943
   214
next
wenzelm@46730
   215
  assume ?lhs then show ?rhs
wenzelm@46730
   216
    by (simp add: multiset_eq_iff split:if_splits) (metis add_is_1)
haftmann@34943
   217
qed
haftmann@34943
   218
haftmann@34943
   219
lemma single_is_union:
haftmann@34943
   220
  "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
haftmann@34943
   221
  by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
haftmann@34943
   222
haftmann@34943
   223
lemma add_eq_conv_diff:
haftmann@34943
   224
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"  (is "?lhs = ?rhs")
nipkow@44890
   225
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
haftmann@34943
   226
proof
haftmann@34943
   227
  assume ?rhs then show ?lhs
haftmann@34943
   228
  by (auto simp add: add_assoc add_commute [of "{#b#}"])
haftmann@34943
   229
    (drule sym, simp add: add_assoc [symmetric])
haftmann@34943
   230
next
haftmann@34943
   231
  assume ?lhs
haftmann@34943
   232
  show ?rhs
haftmann@34943
   233
  proof (cases "a = b")
haftmann@34943
   234
    case True with `?lhs` show ?thesis by simp
haftmann@34943
   235
  next
haftmann@34943
   236
    case False
haftmann@34943
   237
    from `?lhs` have "a \<in># N + {#b#}" by (rule union_single_eq_member)
haftmann@34943
   238
    with False have "a \<in># N" by auto
haftmann@34943
   239
    moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
haftmann@34943
   240
    moreover note False
haftmann@34943
   241
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
haftmann@34943
   242
  qed
haftmann@34943
   243
qed
haftmann@34943
   244
haftmann@34943
   245
lemma insert_noteq_member: 
haftmann@34943
   246
  assumes BC: "B + {#b#} = C + {#c#}"
haftmann@34943
   247
   and bnotc: "b \<noteq> c"
haftmann@34943
   248
  shows "c \<in># B"
haftmann@34943
   249
proof -
haftmann@34943
   250
  have "c \<in># C + {#c#}" by simp
haftmann@34943
   251
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
haftmann@34943
   252
  then have "c \<in># B + {#b#}" using BC by simp
haftmann@34943
   253
  then show "c \<in># B" using nc by simp
haftmann@34943
   254
qed
haftmann@34943
   255
haftmann@34943
   256
lemma add_eq_conv_ex:
haftmann@34943
   257
  "(M + {#a#} = N + {#b#}) =
haftmann@34943
   258
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
haftmann@34943
   259
  by (auto simp add: add_eq_conv_diff)
haftmann@34943
   260
haftmann@34943
   261
haftmann@34943
   262
subsubsection {* Pointwise ordering induced by count *}
haftmann@34943
   263
haftmann@35268
   264
instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le
haftmann@35268
   265
begin
haftmann@35268
   266
haftmann@35268
   267
definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
haftmann@35268
   268
  mset_le_def: "A \<le> B \<longleftrightarrow> (\<forall>a. count A a \<le> count B a)"
haftmann@34943
   269
haftmann@35268
   270
definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
haftmann@35268
   271
  mset_less_def: "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B"
haftmann@34943
   272
wenzelm@46921
   273
instance
wenzelm@46921
   274
  by default (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym)
haftmann@35268
   275
haftmann@35268
   276
end
haftmann@34943
   277
haftmann@34943
   278
lemma mset_less_eqI:
haftmann@35268
   279
  "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le> B"
haftmann@34943
   280
  by (simp add: mset_le_def)
haftmann@34943
   281
haftmann@35268
   282
lemma mset_le_exists_conv:
haftmann@35268
   283
  "(A::'a multiset) \<le> B \<longleftrightarrow> (\<exists>C. B = A + C)"
haftmann@34943
   284
apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
nipkow@39302
   285
apply (auto intro: multiset_eq_iff [THEN iffD2])
haftmann@34943
   286
done
haftmann@34943
   287
haftmann@35268
   288
lemma mset_le_mono_add_right_cancel [simp]:
haftmann@35268
   289
  "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B"
haftmann@35268
   290
  by (fact add_le_cancel_right)
haftmann@34943
   291
haftmann@35268
   292
lemma mset_le_mono_add_left_cancel [simp]:
haftmann@35268
   293
  "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B"
haftmann@35268
   294
  by (fact add_le_cancel_left)
haftmann@35268
   295
haftmann@35268
   296
lemma mset_le_mono_add:
haftmann@35268
   297
  "(A::'a multiset) \<le> B \<Longrightarrow> C \<le> D \<Longrightarrow> A + C \<le> B + D"
haftmann@35268
   298
  by (fact add_mono)
haftmann@34943
   299
haftmann@35268
   300
lemma mset_le_add_left [simp]:
haftmann@35268
   301
  "(A::'a multiset) \<le> A + B"
haftmann@35268
   302
  unfolding mset_le_def by auto
haftmann@35268
   303
haftmann@35268
   304
lemma mset_le_add_right [simp]:
haftmann@35268
   305
  "B \<le> (A::'a multiset) + B"
haftmann@35268
   306
  unfolding mset_le_def by auto
haftmann@34943
   307
haftmann@35268
   308
lemma mset_le_single:
haftmann@35268
   309
  "a :# B \<Longrightarrow> {#a#} \<le> B"
haftmann@35268
   310
  by (simp add: mset_le_def)
haftmann@34943
   311
haftmann@35268
   312
lemma multiset_diff_union_assoc:
haftmann@35268
   313
  "C \<le> B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)"
nipkow@39302
   314
  by (simp add: multiset_eq_iff mset_le_def)
haftmann@34943
   315
haftmann@34943
   316
lemma mset_le_multiset_union_diff_commute:
nipkow@36867
   317
  "B \<le> A \<Longrightarrow> (A::'a multiset) - B + C = A + C - B"
nipkow@39302
   318
by (simp add: multiset_eq_iff mset_le_def)
haftmann@34943
   319
nipkow@39301
   320
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M"
nipkow@39301
   321
by(simp add: mset_le_def)
nipkow@39301
   322
haftmann@35268
   323
lemma mset_lessD: "A < B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
haftmann@34943
   324
apply (clarsimp simp: mset_le_def mset_less_def)
haftmann@34943
   325
apply (erule_tac x=x in allE)
haftmann@34943
   326
apply auto
haftmann@34943
   327
done
haftmann@34943
   328
haftmann@35268
   329
lemma mset_leD: "A \<le> B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
haftmann@34943
   330
apply (clarsimp simp: mset_le_def mset_less_def)
haftmann@34943
   331
apply (erule_tac x = x in allE)
haftmann@34943
   332
apply auto
haftmann@34943
   333
done
haftmann@34943
   334
  
haftmann@35268
   335
lemma mset_less_insertD: "(A + {#x#} < B) \<Longrightarrow> (x \<in># B \<and> A < B)"
haftmann@34943
   336
apply (rule conjI)
haftmann@34943
   337
 apply (simp add: mset_lessD)
haftmann@34943
   338
apply (clarsimp simp: mset_le_def mset_less_def)
haftmann@34943
   339
apply safe
haftmann@34943
   340
 apply (erule_tac x = a in allE)
haftmann@34943
   341
 apply (auto split: split_if_asm)
haftmann@34943
   342
done
haftmann@34943
   343
haftmann@35268
   344
lemma mset_le_insertD: "(A + {#x#} \<le> B) \<Longrightarrow> (x \<in># B \<and> A \<le> B)"
haftmann@34943
   345
apply (rule conjI)
haftmann@34943
   346
 apply (simp add: mset_leD)
haftmann@34943
   347
apply (force simp: mset_le_def mset_less_def split: split_if_asm)
haftmann@34943
   348
done
haftmann@34943
   349
haftmann@35268
   350
lemma mset_less_of_empty[simp]: "A < {#} \<longleftrightarrow> False"
nipkow@39302
   351
  by (auto simp add: mset_less_def mset_le_def multiset_eq_iff)
haftmann@34943
   352
haftmann@35268
   353
lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
haftmann@35268
   354
  by (auto simp: mset_le_def mset_less_def)
haftmann@34943
   355
haftmann@35268
   356
lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
haftmann@35268
   357
  by simp
haftmann@34943
   358
haftmann@34943
   359
lemma mset_less_add_bothsides:
haftmann@35268
   360
  "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
haftmann@35268
   361
  by (fact add_less_imp_less_right)
haftmann@35268
   362
haftmann@35268
   363
lemma mset_less_empty_nonempty:
haftmann@35268
   364
  "{#} < S \<longleftrightarrow> S \<noteq> {#}"
haftmann@35268
   365
  by (auto simp: mset_le_def mset_less_def)
haftmann@35268
   366
haftmann@35268
   367
lemma mset_less_diff_self:
haftmann@35268
   368
  "c \<in># B \<Longrightarrow> B - {#c#} < B"
nipkow@39302
   369
  by (auto simp: mset_le_def mset_less_def multiset_eq_iff)
haftmann@35268
   370
haftmann@35268
   371
haftmann@35268
   372
subsubsection {* Intersection *}
haftmann@35268
   373
haftmann@35268
   374
instantiation multiset :: (type) semilattice_inf
haftmann@35268
   375
begin
haftmann@35268
   376
haftmann@35268
   377
definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
haftmann@35268
   378
  multiset_inter_def: "inf_multiset A B = A - (A - B)"
haftmann@35268
   379
wenzelm@46921
   380
instance
wenzelm@46921
   381
proof -
haftmann@35268
   382
  have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith
wenzelm@46921
   383
  show "OFCLASS('a multiset, semilattice_inf_class)"
wenzelm@46921
   384
    by default (auto simp add: multiset_inter_def mset_le_def aux)
haftmann@35268
   385
qed
haftmann@35268
   386
haftmann@35268
   387
end
haftmann@35268
   388
haftmann@35268
   389
abbreviation multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
haftmann@35268
   390
  "multiset_inter \<equiv> inf"
haftmann@34943
   391
haftmann@41069
   392
lemma multiset_inter_count [simp]:
haftmann@35268
   393
  "count (A #\<inter> B) x = min (count A x) (count B x)"
haftmann@35268
   394
  by (simp add: multiset_inter_def multiset_typedef)
haftmann@35268
   395
haftmann@35268
   396
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
wenzelm@46730
   397
  by (rule multiset_eqI) auto
haftmann@34943
   398
haftmann@35268
   399
lemma multiset_union_diff_commute:
haftmann@35268
   400
  assumes "B #\<inter> C = {#}"
haftmann@35268
   401
  shows "A + B - C = A - C + B"
nipkow@39302
   402
proof (rule multiset_eqI)
haftmann@35268
   403
  fix x
haftmann@35268
   404
  from assms have "min (count B x) (count C x) = 0"
wenzelm@46730
   405
    by (auto simp add: multiset_eq_iff)
haftmann@35268
   406
  then have "count B x = 0 \<or> count C x = 0"
haftmann@35268
   407
    by auto
haftmann@35268
   408
  then show "count (A + B - C) x = count (A - C + B) x"
haftmann@35268
   409
    by auto
haftmann@35268
   410
qed
haftmann@35268
   411
haftmann@35268
   412
haftmann@41069
   413
subsubsection {* Filter (with comprehension syntax) *}
haftmann@41069
   414
haftmann@41069
   415
text {* Multiset comprehension *}
haftmann@41069
   416
haftmann@41069
   417
definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
haftmann@41069
   418
  "filter P M = Abs_multiset (\<lambda>x. if P x then count M x else 0)"
haftmann@35268
   419
haftmann@41069
   420
hide_const (open) filter
haftmann@35268
   421
haftmann@41069
   422
lemma count_filter [simp]:
haftmann@41069
   423
  "count (Multiset.filter P M) a = (if P a then count M a else 0)"
haftmann@41069
   424
  by (simp add: filter_def in_multiset multiset_typedef)
haftmann@41069
   425
haftmann@41069
   426
lemma filter_empty [simp]:
haftmann@41069
   427
  "Multiset.filter P {#} = {#}"
nipkow@39302
   428
  by (rule multiset_eqI) simp
haftmann@35268
   429
haftmann@41069
   430
lemma filter_single [simp]:
haftmann@41069
   431
  "Multiset.filter P {#x#} = (if P x then {#x#} else {#})"
haftmann@41069
   432
  by (rule multiset_eqI) simp
haftmann@41069
   433
haftmann@41069
   434
lemma filter_union [simp]:
haftmann@41069
   435
  "Multiset.filter P (M + N) = Multiset.filter P M + Multiset.filter P N"
nipkow@39302
   436
  by (rule multiset_eqI) simp
haftmann@35268
   437
haftmann@41069
   438
lemma filter_diff [simp]:
haftmann@41069
   439
  "Multiset.filter P (M - N) = Multiset.filter P M - Multiset.filter P N"
haftmann@41069
   440
  by (rule multiset_eqI) simp
haftmann@41069
   441
haftmann@41069
   442
lemma filter_inter [simp]:
haftmann@41069
   443
  "Multiset.filter P (M #\<inter> N) = Multiset.filter P M #\<inter> Multiset.filter P N"
nipkow@39302
   444
  by (rule multiset_eqI) simp
wenzelm@10249
   445
haftmann@41069
   446
syntax
haftmann@41069
   447
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
haftmann@41069
   448
syntax (xsymbol)
haftmann@41069
   449
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
haftmann@41069
   450
translations
haftmann@41069
   451
  "{#x \<in># M. P#}" == "CONST Multiset.filter (\<lambda>x. P) M"
haftmann@41069
   452
wenzelm@10249
   453
wenzelm@10249
   454
subsubsection {* Set of elements *}
wenzelm@10249
   455
haftmann@34943
   456
definition set_of :: "'a multiset => 'a set" where
haftmann@34943
   457
  "set_of M = {x. x :# M}"
haftmann@34943
   458
wenzelm@17161
   459
lemma set_of_empty [simp]: "set_of {#} = {}"
nipkow@26178
   460
by (simp add: set_of_def)
wenzelm@10249
   461
wenzelm@17161
   462
lemma set_of_single [simp]: "set_of {#b#} = {b}"
nipkow@26178
   463
by (simp add: set_of_def)
wenzelm@10249
   464
wenzelm@17161
   465
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
nipkow@26178
   466
by (auto simp add: set_of_def)
wenzelm@10249
   467
wenzelm@17161
   468
lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
nipkow@39302
   469
by (auto simp add: set_of_def multiset_eq_iff)
wenzelm@10249
   470
wenzelm@17161
   471
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
nipkow@26178
   472
by (auto simp add: set_of_def)
nipkow@26016
   473
haftmann@41069
   474
lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
nipkow@26178
   475
by (auto simp add: set_of_def)
wenzelm@10249
   476
haftmann@34943
   477
lemma finite_set_of [iff]: "finite (set_of M)"
haftmann@34943
   478
  using count [of M] by (simp add: multiset_def set_of_def)
haftmann@34943
   479
bulwahn@46756
   480
lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
bulwahn@46756
   481
  unfolding set_of_def[symmetric] by simp
wenzelm@10249
   482
wenzelm@10249
   483
subsubsection {* Size *}
wenzelm@10249
   484
haftmann@34943
   485
instantiation multiset :: (type) size
haftmann@34943
   486
begin
haftmann@34943
   487
haftmann@34943
   488
definition size_def:
haftmann@34943
   489
  "size M = setsum (count M) (set_of M)"
haftmann@34943
   490
haftmann@34943
   491
instance ..
haftmann@34943
   492
haftmann@34943
   493
end
haftmann@34943
   494
haftmann@28708
   495
lemma size_empty [simp]: "size {#} = 0"
nipkow@26178
   496
by (simp add: size_def)
wenzelm@10249
   497
haftmann@28708
   498
lemma size_single [simp]: "size {#b#} = 1"
nipkow@26178
   499
by (simp add: size_def)
wenzelm@10249
   500
wenzelm@17161
   501
lemma setsum_count_Int:
nipkow@26178
   502
  "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
nipkow@26178
   503
apply (induct rule: finite_induct)
nipkow@26178
   504
 apply simp
nipkow@26178
   505
apply (simp add: Int_insert_left set_of_def)
nipkow@26178
   506
done
wenzelm@10249
   507
haftmann@28708
   508
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
nipkow@26178
   509
apply (unfold size_def)
nipkow@26178
   510
apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
nipkow@26178
   511
 prefer 2
nipkow@26178
   512
 apply (rule ext, simp)
nipkow@26178
   513
apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
nipkow@26178
   514
apply (subst Int_commute)
nipkow@26178
   515
apply (simp (no_asm_simp) add: setsum_count_Int)
nipkow@26178
   516
done
wenzelm@10249
   517
wenzelm@17161
   518
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
nipkow@39302
   519
by (auto simp add: size_def multiset_eq_iff)
nipkow@26016
   520
nipkow@26016
   521
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
nipkow@26178
   522
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
wenzelm@10249
   523
wenzelm@17161
   524
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
nipkow@26178
   525
apply (unfold size_def)
nipkow@26178
   526
apply (drule setsum_SucD)
nipkow@26178
   527
apply auto
nipkow@26178
   528
done
wenzelm@10249
   529
haftmann@34943
   530
lemma size_eq_Suc_imp_eq_union:
haftmann@34943
   531
  assumes "size M = Suc n"
haftmann@34943
   532
  shows "\<exists>a N. M = N + {#a#}"
haftmann@34943
   533
proof -
haftmann@34943
   534
  from assms obtain a where "a \<in># M"
haftmann@34943
   535
    by (erule size_eq_Suc_imp_elem [THEN exE])
haftmann@34943
   536
  then have "M = M - {#a#} + {#a#}" by simp
haftmann@34943
   537
  then show ?thesis by blast
nipkow@23611
   538
qed
kleing@15869
   539
nipkow@26016
   540
nipkow@26016
   541
subsection {* Induction and case splits *}
wenzelm@10249
   542
wenzelm@10249
   543
lemma setsum_decr:
wenzelm@11701
   544
  "finite F ==> (0::nat) < f a ==>
paulson@15072
   545
    setsum (f (a := f a - 1)) F = (if a\<in>F then setsum f F - 1 else setsum f F)"
nipkow@26178
   546
apply (induct rule: finite_induct)
nipkow@26178
   547
 apply auto
nipkow@26178
   548
apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow@26178
   549
done
wenzelm@10249
   550
wenzelm@10313
   551
lemma rep_multiset_induct_aux:
nipkow@26178
   552
assumes 1: "P (\<lambda>a. (0::nat))"
nipkow@26178
   553
  and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))"
nipkow@26178
   554
shows "\<forall>f. f \<in> multiset --> setsum f {x. f x \<noteq> 0} = n --> P f"
nipkow@26178
   555
apply (unfold multiset_def)
nipkow@26178
   556
apply (induct_tac n, simp, clarify)
nipkow@26178
   557
 apply (subgoal_tac "f = (\<lambda>a.0)")
nipkow@26178
   558
  apply simp
nipkow@26178
   559
  apply (rule 1)
nipkow@26178
   560
 apply (rule ext, force, clarify)
nipkow@26178
   561
apply (frule setsum_SucD, clarify)
nipkow@26178
   562
apply (rename_tac a)
nipkow@26178
   563
apply (subgoal_tac "finite {x. (f (a := f a - 1)) x > 0}")
nipkow@26178
   564
 prefer 2
nipkow@26178
   565
 apply (rule finite_subset)
nipkow@26178
   566
  prefer 2
nipkow@26178
   567
  apply assumption
nipkow@26178
   568
 apply simp
nipkow@26178
   569
 apply blast
nipkow@26178
   570
apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
nipkow@26178
   571
 prefer 2
nipkow@26178
   572
 apply (rule ext)
nipkow@26178
   573
 apply (simp (no_asm_simp))
nipkow@26178
   574
 apply (erule ssubst, rule 2 [unfolded multiset_def], blast)
nipkow@26178
   575
apply (erule allE, erule impE, erule_tac [2] mp, blast)
nipkow@26178
   576
apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
nipkow@26178
   577
apply (subgoal_tac "{x. x \<noteq> a --> f x \<noteq> 0} = {x. f x \<noteq> 0}")
nipkow@26178
   578
 prefer 2
nipkow@26178
   579
 apply blast
nipkow@26178
   580
apply (subgoal_tac "{x. x \<noteq> a \<and> f x \<noteq> 0} = {x. f x \<noteq> 0} - {a}")
nipkow@26178
   581
 prefer 2
nipkow@26178
   582
 apply blast
nipkow@26178
   583
apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong)
nipkow@26178
   584
done
wenzelm@10249
   585
wenzelm@10313
   586
theorem rep_multiset_induct:
nipkow@11464
   587
  "f \<in> multiset ==> P (\<lambda>a. 0) ==>
wenzelm@11701
   588
    (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
nipkow@26178
   589
using rep_multiset_induct_aux by blast
wenzelm@10249
   590
wenzelm@18258
   591
theorem multiset_induct [case_names empty add, induct type: multiset]:
nipkow@26178
   592
assumes empty: "P {#}"
nipkow@26178
   593
  and add: "!!M x. P M ==> P (M + {#x#})"
nipkow@26178
   594
shows "P M"
wenzelm@10249
   595
proof -
wenzelm@10249
   596
  note defns = union_def single_def Mempty_def
haftmann@34943
   597
  note add' = add [unfolded defns, simplified]
haftmann@34943
   598
  have aux: "\<And>a::'a. count (Abs_multiset (\<lambda>b. if b = a then 1 else 0)) =
haftmann@34943
   599
    (\<lambda>b. if b = a then 1 else 0)" by (simp add: Abs_multiset_inverse in_multiset) 
wenzelm@10249
   600
  show ?thesis
haftmann@34943
   601
    apply (rule count_inverse [THEN subst])
haftmann@34943
   602
    apply (rule count [THEN rep_multiset_induct])
wenzelm@18258
   603
     apply (rule empty [unfolded defns])
paulson@15072
   604
    apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))")
wenzelm@10249
   605
     prefer 2
nipkow@39302
   606
     apply (simp add: fun_eq_iff)
wenzelm@10249
   607
    apply (erule ssubst)
wenzelm@17200
   608
    apply (erule Abs_multiset_inverse [THEN subst])
haftmann@34943
   609
    apply (drule add')
haftmann@34943
   610
    apply (simp add: aux)
wenzelm@10249
   611
    done
wenzelm@10249
   612
qed
wenzelm@10249
   613
kleing@25610
   614
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
nipkow@26178
   615
by (induct M) auto
kleing@25610
   616
kleing@25610
   617
lemma multiset_cases [cases type, case_names empty add]:
nipkow@26178
   618
assumes em:  "M = {#} \<Longrightarrow> P"
nipkow@26178
   619
assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P"
nipkow@26178
   620
shows "P"
kleing@25610
   621
proof (cases "M = {#}")
wenzelm@26145
   622
  assume "M = {#}" then show ?thesis using em by simp
kleing@25610
   623
next
kleing@25610
   624
  assume "M \<noteq> {#}"
kleing@25610
   625
  then obtain M' m where "M = M' + {#m#}" 
kleing@25610
   626
    by (blast dest: multi_nonempty_split)
wenzelm@26145
   627
  then show ?thesis using add by simp
kleing@25610
   628
qed
kleing@25610
   629
kleing@25610
   630
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
nipkow@26178
   631
apply (cases M)
nipkow@26178
   632
 apply simp
nipkow@26178
   633
apply (rule_tac x="M - {#x#}" in exI, simp)
nipkow@26178
   634
done
kleing@25610
   635
haftmann@34943
   636
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
haftmann@34943
   637
by (cases "B = {#}") (auto dest: multi_member_split)
haftmann@34943
   638
nipkow@26033
   639
lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}"
nipkow@39302
   640
apply (subst multiset_eq_iff)
nipkow@26178
   641
apply auto
nipkow@26178
   642
done
wenzelm@10249
   643
haftmann@35268
   644
lemma mset_less_size: "(A::'a multiset) < B \<Longrightarrow> size A < size B"
haftmann@34943
   645
proof (induct A arbitrary: B)
haftmann@34943
   646
  case (empty M)
haftmann@34943
   647
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
haftmann@34943
   648
  then obtain M' x where "M = M' + {#x#}" 
haftmann@34943
   649
    by (blast dest: multi_nonempty_split)
haftmann@34943
   650
  then show ?case by simp
haftmann@34943
   651
next
haftmann@34943
   652
  case (add S x T)
haftmann@35268
   653
  have IH: "\<And>B. S < B \<Longrightarrow> size S < size B" by fact
haftmann@35268
   654
  have SxsubT: "S + {#x#} < T" by fact
haftmann@35268
   655
  then have "x \<in># T" and "S < T" by (auto dest: mset_less_insertD)
haftmann@34943
   656
  then obtain T' where T: "T = T' + {#x#}" 
haftmann@34943
   657
    by (blast dest: multi_member_split)
haftmann@35268
   658
  then have "S < T'" using SxsubT 
haftmann@34943
   659
    by (blast intro: mset_less_add_bothsides)
haftmann@34943
   660
  then have "size S < size T'" using IH by simp
haftmann@34943
   661
  then show ?case using T by simp
haftmann@34943
   662
qed
haftmann@34943
   663
haftmann@34943
   664
haftmann@34943
   665
subsubsection {* Strong induction and subset induction for multisets *}
haftmann@34943
   666
haftmann@34943
   667
text {* Well-foundedness of proper subset operator: *}
haftmann@34943
   668
haftmann@34943
   669
text {* proper multiset subset *}
haftmann@34943
   670
haftmann@34943
   671
definition
haftmann@34943
   672
  mset_less_rel :: "('a multiset * 'a multiset) set" where
haftmann@35268
   673
  "mset_less_rel = {(A,B). A < B}"
wenzelm@10249
   674
haftmann@34943
   675
lemma multiset_add_sub_el_shuffle: 
haftmann@34943
   676
  assumes "c \<in># B" and "b \<noteq> c" 
haftmann@34943
   677
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
haftmann@34943
   678
proof -
haftmann@34943
   679
  from `c \<in># B` obtain A where B: "B = A + {#c#}" 
haftmann@34943
   680
    by (blast dest: multi_member_split)
haftmann@34943
   681
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
haftmann@34943
   682
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}" 
haftmann@34943
   683
    by (simp add: add_ac)
haftmann@34943
   684
  then show ?thesis using B by simp
haftmann@34943
   685
qed
haftmann@34943
   686
haftmann@34943
   687
lemma wf_mset_less_rel: "wf mset_less_rel"
haftmann@34943
   688
apply (unfold mset_less_rel_def)
haftmann@34943
   689
apply (rule wf_measure [THEN wf_subset, where f1=size])
haftmann@34943
   690
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
haftmann@34943
   691
done
haftmann@34943
   692
haftmann@34943
   693
text {* The induction rules: *}
haftmann@34943
   694
haftmann@34943
   695
lemma full_multiset_induct [case_names less]:
haftmann@35268
   696
assumes ih: "\<And>B. \<forall>(A::'a multiset). A < B \<longrightarrow> P A \<Longrightarrow> P B"
haftmann@34943
   697
shows "P B"
haftmann@34943
   698
apply (rule wf_mset_less_rel [THEN wf_induct])
haftmann@34943
   699
apply (rule ih, auto simp: mset_less_rel_def)
haftmann@34943
   700
done
haftmann@34943
   701
haftmann@34943
   702
lemma multi_subset_induct [consumes 2, case_names empty add]:
haftmann@35268
   703
assumes "F \<le> A"
haftmann@34943
   704
  and empty: "P {#}"
haftmann@34943
   705
  and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
haftmann@34943
   706
shows "P F"
haftmann@34943
   707
proof -
haftmann@35268
   708
  from `F \<le> A`
haftmann@34943
   709
  show ?thesis
haftmann@34943
   710
  proof (induct F)
haftmann@34943
   711
    show "P {#}" by fact
haftmann@34943
   712
  next
haftmann@34943
   713
    fix x F
haftmann@35268
   714
    assume P: "F \<le> A \<Longrightarrow> P F" and i: "F + {#x#} \<le> A"
haftmann@34943
   715
    show "P (F + {#x#})"
haftmann@34943
   716
    proof (rule insert)
haftmann@34943
   717
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
haftmann@35268
   718
      from i have "F \<le> A" by (auto dest: mset_le_insertD)
haftmann@34943
   719
      with P show "P F" .
haftmann@34943
   720
    qed
haftmann@34943
   721
  qed
haftmann@34943
   722
qed
wenzelm@26145
   723
wenzelm@17161
   724
haftmann@34943
   725
subsection {* Alternative representations *}
haftmann@34943
   726
haftmann@34943
   727
subsubsection {* Lists *}
haftmann@34943
   728
haftmann@34943
   729
primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where
haftmann@34943
   730
  "multiset_of [] = {#}" |
haftmann@34943
   731
  "multiset_of (a # x) = multiset_of x + {# a #}"
haftmann@34943
   732
haftmann@37107
   733
lemma in_multiset_in_set:
haftmann@37107
   734
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
haftmann@37107
   735
  by (induct xs) simp_all
haftmann@37107
   736
haftmann@37107
   737
lemma count_multiset_of:
haftmann@37107
   738
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@37107
   739
  by (induct xs) simp_all
haftmann@37107
   740
haftmann@34943
   741
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
haftmann@34943
   742
by (induct x) auto
haftmann@34943
   743
haftmann@34943
   744
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
haftmann@34943
   745
by (induct x) auto
haftmann@34943
   746
haftmann@40950
   747
lemma set_of_multiset_of[simp]: "set_of (multiset_of x) = set x"
haftmann@34943
   748
by (induct x) auto
haftmann@34943
   749
haftmann@34943
   750
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
haftmann@34943
   751
by (induct xs) auto
haftmann@34943
   752
haftmann@34943
   753
lemma multiset_of_append [simp]:
haftmann@34943
   754
  "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
haftmann@34943
   755
  by (induct xs arbitrary: ys) (auto simp: add_ac)
haftmann@34943
   756
haftmann@40303
   757
lemma multiset_of_filter:
haftmann@40303
   758
  "multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
haftmann@40303
   759
  by (induct xs) simp_all
haftmann@40303
   760
haftmann@40950
   761
lemma multiset_of_rev [simp]:
haftmann@40950
   762
  "multiset_of (rev xs) = multiset_of xs"
haftmann@40950
   763
  by (induct xs) simp_all
haftmann@40950
   764
haftmann@34943
   765
lemma surj_multiset_of: "surj multiset_of"
haftmann@34943
   766
apply (unfold surj_def)
haftmann@34943
   767
apply (rule allI)
haftmann@34943
   768
apply (rule_tac M = y in multiset_induct)
haftmann@34943
   769
 apply auto
haftmann@34943
   770
apply (rule_tac x = "x # xa" in exI)
haftmann@34943
   771
apply auto
haftmann@34943
   772
done
haftmann@34943
   773
haftmann@34943
   774
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
haftmann@34943
   775
by (induct x) auto
haftmann@34943
   776
haftmann@34943
   777
lemma distinct_count_atmost_1:
haftmann@34943
   778
  "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
haftmann@34943
   779
apply (induct x, simp, rule iffI, simp_all)
haftmann@34943
   780
apply (rule conjI)
haftmann@34943
   781
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
haftmann@34943
   782
apply (erule_tac x = a in allE, simp, clarify)
haftmann@34943
   783
apply (erule_tac x = aa in allE, simp)
haftmann@34943
   784
done
haftmann@34943
   785
haftmann@34943
   786
lemma multiset_of_eq_setD:
haftmann@34943
   787
  "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
nipkow@39302
   788
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
haftmann@34943
   789
haftmann@34943
   790
lemma set_eq_iff_multiset_of_eq_distinct:
haftmann@34943
   791
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
haftmann@34943
   792
    (set x = set y) = (multiset_of x = multiset_of y)"
nipkow@39302
   793
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
haftmann@34943
   794
haftmann@34943
   795
lemma set_eq_iff_multiset_of_remdups_eq:
haftmann@34943
   796
   "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
haftmann@34943
   797
apply (rule iffI)
haftmann@34943
   798
apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
haftmann@34943
   799
apply (drule distinct_remdups [THEN distinct_remdups
haftmann@34943
   800
      [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
haftmann@34943
   801
apply simp
haftmann@34943
   802
done
haftmann@34943
   803
haftmann@34943
   804
lemma multiset_of_compl_union [simp]:
haftmann@34943
   805
  "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
haftmann@34943
   806
  by (induct xs) (auto simp: add_ac)
haftmann@34943
   807
haftmann@41069
   808
lemma count_multiset_of_length_filter:
haftmann@39533
   809
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@39533
   810
  by (induct xs) auto
haftmann@34943
   811
haftmann@34943
   812
lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls"
haftmann@34943
   813
apply (induct ls arbitrary: i)
haftmann@34943
   814
 apply simp
haftmann@34943
   815
apply (case_tac i)
haftmann@34943
   816
 apply auto
haftmann@34943
   817
done
haftmann@34943
   818
nipkow@36903
   819
lemma multiset_of_remove1[simp]:
nipkow@36903
   820
  "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
nipkow@39302
   821
by (induct xs) (auto simp add: multiset_eq_iff)
haftmann@34943
   822
haftmann@34943
   823
lemma multiset_of_eq_length:
haftmann@37107
   824
  assumes "multiset_of xs = multiset_of ys"
haftmann@37107
   825
  shows "length xs = length ys"
wenzelm@46921
   826
using assms
wenzelm@46921
   827
proof (induct xs arbitrary: ys)
haftmann@37107
   828
  case Nil then show ?case by simp
haftmann@37107
   829
next
haftmann@37107
   830
  case (Cons x xs)
haftmann@37107
   831
  then have "x \<in># multiset_of ys" by (simp add: union_single_eq_member)
haftmann@37107
   832
  then have "x \<in> set ys" by (simp add: in_multiset_in_set)
haftmann@37107
   833
  from Cons.prems [symmetric] have "multiset_of xs = multiset_of (remove1 x ys)"
haftmann@37107
   834
    by simp
haftmann@37107
   835
  with Cons.hyps have "length xs = length (remove1 x ys)" .
haftmann@37107
   836
  with `x \<in> set ys` show ?case
haftmann@37107
   837
    by (auto simp add: length_remove1 dest: length_pos_if_in_set)
haftmann@34943
   838
qed
haftmann@34943
   839
haftmann@39533
   840
lemma multiset_of_eq_length_filter:
haftmann@39533
   841
  assumes "multiset_of xs = multiset_of ys"
haftmann@39533
   842
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
haftmann@39533
   843
proof (cases "z \<in># multiset_of xs")
haftmann@39533
   844
  case False
haftmann@39533
   845
  moreover have "\<not> z \<in># multiset_of ys" using assms False by simp
haftmann@41069
   846
  ultimately show ?thesis by (simp add: count_multiset_of_length_filter)
haftmann@39533
   847
next
haftmann@39533
   848
  case True
haftmann@39533
   849
  moreover have "z \<in># multiset_of ys" using assms True by simp
wenzelm@46921
   850
  show ?thesis using assms
wenzelm@46921
   851
  proof (induct xs arbitrary: ys)
haftmann@39533
   852
    case Nil then show ?case by simp
haftmann@39533
   853
  next
haftmann@39533
   854
    case (Cons x xs)
haftmann@39533
   855
    from `multiset_of (x # xs) = multiset_of ys` [symmetric]
haftmann@39533
   856
      have *: "multiset_of xs = multiset_of (remove1 x ys)"
haftmann@39533
   857
      and "x \<in> set ys"
haftmann@39533
   858
      by (auto simp add: mem_set_multiset_eq)
haftmann@39533
   859
    from * have "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) (remove1 x ys))" by (rule Cons.hyps)
haftmann@39533
   860
    moreover from `x \<in> set ys` have "length (filter (\<lambda>y. x = y) ys) > 0" by (simp add: filter_empty_conv)
haftmann@39533
   861
    ultimately show ?case using `x \<in> set ys`
haftmann@39533
   862
      by (simp add: filter_remove1) (auto simp add: length_remove1)
haftmann@39533
   863
  qed
haftmann@39533
   864
qed
haftmann@39533
   865
haftmann@45989
   866
lemma fold_multiset_equiv:
haftmann@45989
   867
  assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
haftmann@45989
   868
    and equiv: "multiset_of xs = multiset_of ys"
haftmann@45989
   869
  shows "fold f xs = fold f ys"
wenzelm@46921
   870
using f equiv [symmetric]
wenzelm@46921
   871
proof (induct xs arbitrary: ys)
haftmann@45989
   872
  case Nil then show ?case by simp
haftmann@45989
   873
next
haftmann@45989
   874
  case (Cons x xs)
haftmann@45989
   875
  then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD)
haftmann@45989
   876
  have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" 
haftmann@45989
   877
    by (rule Cons.prems(1)) (simp_all add: *)
haftmann@45989
   878
  moreover from * have "x \<in> set ys" by simp
haftmann@45989
   879
  ultimately have "fold f ys = fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
haftmann@45989
   880
  moreover from Cons.prems have "fold f xs = fold f (remove1 x ys)" by (auto intro: Cons.hyps)
haftmann@45989
   881
  ultimately show ?case by simp
haftmann@45989
   882
qed
haftmann@45989
   883
haftmann@39533
   884
context linorder
haftmann@39533
   885
begin
haftmann@39533
   886
haftmann@40210
   887
lemma multiset_of_insort [simp]:
haftmann@39533
   888
  "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
haftmann@37107
   889
  by (induct xs) (simp_all add: ac_simps)
haftmann@39533
   890
 
haftmann@40210
   891
lemma multiset_of_sort [simp]:
haftmann@39533
   892
  "multiset_of (sort_key k xs) = multiset_of xs"
haftmann@37107
   893
  by (induct xs) (simp_all add: ac_simps)
haftmann@37107
   894
haftmann@34943
   895
text {*
haftmann@34943
   896
  This lemma shows which properties suffice to show that a function
haftmann@34943
   897
  @{text "f"} with @{text "f xs = ys"} behaves like sort.
haftmann@34943
   898
*}
haftmann@37074
   899
haftmann@39533
   900
lemma properties_for_sort_key:
haftmann@39533
   901
  assumes "multiset_of ys = multiset_of xs"
haftmann@40305
   902
  and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs"
haftmann@39533
   903
  and "sorted (map f ys)"
haftmann@39533
   904
  shows "sort_key f xs = ys"
wenzelm@46921
   905
using assms
wenzelm@46921
   906
proof (induct xs arbitrary: ys)
haftmann@34943
   907
  case Nil then show ?case by simp
haftmann@34943
   908
next
haftmann@34943
   909
  case (Cons x xs)
haftmann@39533
   910
  from Cons.prems(2) have
haftmann@40305
   911
    "\<forall>k \<in> set ys. filter (\<lambda>x. f k = f x) (remove1 x ys) = filter (\<lambda>x. f k = f x) xs"
haftmann@39533
   912
    by (simp add: filter_remove1)
haftmann@39533
   913
  with Cons.prems have "sort_key f xs = remove1 x ys"
haftmann@39533
   914
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
haftmann@39533
   915
  moreover from Cons.prems have "x \<in> set ys"
haftmann@39533
   916
    by (auto simp add: mem_set_multiset_eq intro!: ccontr)
haftmann@39533
   917
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
haftmann@34943
   918
qed
haftmann@34943
   919
haftmann@39533
   920
lemma properties_for_sort:
haftmann@39533
   921
  assumes multiset: "multiset_of ys = multiset_of xs"
haftmann@39533
   922
  and "sorted ys"
haftmann@39533
   923
  shows "sort xs = ys"
haftmann@39533
   924
proof (rule properties_for_sort_key)
haftmann@39533
   925
  from multiset show "multiset_of ys = multiset_of xs" .
haftmann@39533
   926
  from `sorted ys` show "sorted (map (\<lambda>x. x) ys)" by simp
haftmann@39533
   927
  from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)"
haftmann@39533
   928
    by (rule multiset_of_eq_length_filter)
haftmann@39533
   929
  then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k"
haftmann@39533
   930
    by simp
haftmann@40305
   931
  then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs"
haftmann@39533
   932
    by (simp add: replicate_length_filter)
haftmann@39533
   933
qed
haftmann@39533
   934
haftmann@40303
   935
lemma sort_key_by_quicksort:
haftmann@40303
   936
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
haftmann@40303
   937
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
haftmann@40303
   938
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
haftmann@40303
   939
proof (rule properties_for_sort_key)
haftmann@40303
   940
  show "multiset_of ?rhs = multiset_of ?lhs"
haftmann@40303
   941
    by (rule multiset_eqI) (auto simp add: multiset_of_filter)
haftmann@40303
   942
next
haftmann@40303
   943
  show "sorted (map f ?rhs)"
haftmann@40303
   944
    by (auto simp add: sorted_append intro: sorted_map_same)
haftmann@40303
   945
next
haftmann@40305
   946
  fix l
haftmann@40305
   947
  assume "l \<in> set ?rhs"
haftmann@40346
   948
  let ?pivot = "f (xs ! (length xs div 2))"
haftmann@40346
   949
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
haftmann@40306
   950
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
haftmann@40305
   951
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
haftmann@40346
   952
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
haftmann@40346
   953
  have "\<And>x P. P (f x) ?pivot \<and> f l = f x \<longleftrightarrow> P (f l) ?pivot \<and> f l = f x" by auto
haftmann@40346
   954
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
haftmann@40346
   955
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
haftmann@40346
   956
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
haftmann@40306
   957
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
haftmann@40305
   958
  proof (cases "f l" ?pivot rule: linorder_cases)
wenzelm@46730
   959
    case less
wenzelm@46730
   960
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
wenzelm@46730
   961
    with less show ?thesis
haftmann@40346
   962
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40305
   963
  next
haftmann@40306
   964
    case equal then show ?thesis
haftmann@40346
   965
      by (simp add: * less_le)
haftmann@40305
   966
  next
wenzelm@46730
   967
    case greater
wenzelm@46730
   968
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
wenzelm@46730
   969
    with greater show ?thesis
haftmann@40346
   970
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40306
   971
  qed
haftmann@40303
   972
qed
haftmann@40303
   973
haftmann@40303
   974
lemma sort_by_quicksort:
haftmann@40303
   975
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
haftmann@40303
   976
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
haftmann@40303
   977
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
haftmann@40303
   978
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
haftmann@40303
   979
haftmann@40347
   980
text {* A stable parametrized quicksort *}
haftmann@40347
   981
haftmann@40347
   982
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
haftmann@40347
   983
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
haftmann@40347
   984
haftmann@40347
   985
lemma part_code [code]:
haftmann@40347
   986
  "part f pivot [] = ([], [], [])"
haftmann@40347
   987
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
haftmann@40347
   988
     if x' < pivot then (x # lts, eqs, gts)
haftmann@40347
   989
     else if x' > pivot then (lts, eqs, x # gts)
haftmann@40347
   990
     else (lts, x # eqs, gts))"
haftmann@40347
   991
  by (auto simp add: part_def Let_def split_def)
haftmann@40347
   992
haftmann@40347
   993
lemma sort_key_by_quicksort_code [code]:
haftmann@40347
   994
  "sort_key f xs = (case xs of [] \<Rightarrow> []
haftmann@40347
   995
    | [x] \<Rightarrow> xs
haftmann@40347
   996
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
haftmann@40347
   997
    | _ \<Rightarrow> (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
   998
       in sort_key f lts @ eqs @ sort_key f gts))"
haftmann@40347
   999
proof (cases xs)
haftmann@40347
  1000
  case Nil then show ?thesis by simp
haftmann@40347
  1001
next
wenzelm@46921
  1002
  case (Cons _ ys) note hyps = Cons show ?thesis
wenzelm@46921
  1003
  proof (cases ys)
haftmann@40347
  1004
    case Nil with hyps show ?thesis by simp
haftmann@40347
  1005
  next
wenzelm@46921
  1006
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
wenzelm@46921
  1007
    proof (cases zs)
haftmann@40347
  1008
      case Nil with hyps show ?thesis by auto
haftmann@40347
  1009
    next
haftmann@40347
  1010
      case Cons 
haftmann@40347
  1011
      from sort_key_by_quicksort [of f xs]
haftmann@40347
  1012
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1013
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1014
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
haftmann@40347
  1015
      with hyps Cons show ?thesis by (simp only: list.cases)
haftmann@40347
  1016
    qed
haftmann@40347
  1017
  qed
haftmann@40347
  1018
qed
haftmann@40347
  1019
haftmann@39533
  1020
end
haftmann@39533
  1021
haftmann@40347
  1022
hide_const (open) part
haftmann@40347
  1023
haftmann@35268
  1024
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs"
haftmann@35268
  1025
  by (induct xs) (auto intro: order_trans)
haftmann@34943
  1026
haftmann@34943
  1027
lemma multiset_of_update:
haftmann@34943
  1028
  "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
haftmann@34943
  1029
proof (induct ls arbitrary: i)
haftmann@34943
  1030
  case Nil then show ?case by simp
haftmann@34943
  1031
next
haftmann@34943
  1032
  case (Cons x xs)
haftmann@34943
  1033
  show ?case
haftmann@34943
  1034
  proof (cases i)
haftmann@34943
  1035
    case 0 then show ?thesis by simp
haftmann@34943
  1036
  next
haftmann@34943
  1037
    case (Suc i')
haftmann@34943
  1038
    with Cons show ?thesis
haftmann@34943
  1039
      apply simp
haftmann@34943
  1040
      apply (subst add_assoc)
haftmann@34943
  1041
      apply (subst add_commute [of "{#v#}" "{#x#}"])
haftmann@34943
  1042
      apply (subst add_assoc [symmetric])
haftmann@34943
  1043
      apply simp
haftmann@34943
  1044
      apply (rule mset_le_multiset_union_diff_commute)
haftmann@34943
  1045
      apply (simp add: mset_le_single nth_mem_multiset_of)
haftmann@34943
  1046
      done
haftmann@34943
  1047
  qed
haftmann@34943
  1048
qed
haftmann@34943
  1049
haftmann@34943
  1050
lemma multiset_of_swap:
haftmann@34943
  1051
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
haftmann@34943
  1052
    multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
haftmann@34943
  1053
  by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)
haftmann@34943
  1054
haftmann@34943
  1055
bulwahn@46168
  1056
subsubsection {* Association lists -- including code generation *}
bulwahn@46168
  1057
bulwahn@46168
  1058
text {* Preliminaries *}
bulwahn@46168
  1059
bulwahn@46168
  1060
text {* Raw operations on lists *}
bulwahn@46168
  1061
bulwahn@46168
  1062
definition join_raw :: "('key \<Rightarrow> 'val \<times> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bulwahn@46168
  1063
where
bulwahn@46168
  1064
  "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (%v'. f k (v', v))) ys xs"
bulwahn@46168
  1065
bulwahn@46168
  1066
lemma join_raw_Nil [simp]:
bulwahn@46168
  1067
  "join_raw f xs [] = xs"
bulwahn@46168
  1068
by (simp add: join_raw_def)
bulwahn@46168
  1069
bulwahn@46168
  1070
lemma join_raw_Cons [simp]:
bulwahn@46168
  1071
  "join_raw f xs ((k, v) # ys) = map_default k v (%v'. f k (v', v)) (join_raw f xs ys)"
bulwahn@46168
  1072
by (simp add: join_raw_def)
bulwahn@46168
  1073
bulwahn@46168
  1074
lemma map_of_join_raw:
bulwahn@46168
  1075
  assumes "distinct (map fst ys)"
bulwahn@46168
  1076
  shows "map_of (join_raw f xs ys) x = (case map_of xs x of None => map_of ys x | Some v => (case map_of ys x of None => Some v | Some v' => Some (f x (v, v'))))"
bulwahn@46168
  1077
using assms
bulwahn@46168
  1078
apply (induct ys)
bulwahn@46168
  1079
apply (auto simp add: map_of_map_default split: option.split)
bulwahn@46168
  1080
apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI)
bulwahn@46168
  1081
by (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2))
bulwahn@46168
  1082
bulwahn@46168
  1083
lemma distinct_join_raw:
bulwahn@46168
  1084
  assumes "distinct (map fst xs)"
bulwahn@46168
  1085
  shows "distinct (map fst (join_raw f xs ys))"
bulwahn@46168
  1086
using assms
bulwahn@46168
  1087
proof (induct ys)
bulwahn@46168
  1088
  case (Cons y ys)
bulwahn@46168
  1089
  thus ?case by (cases y) (simp add: distinct_map_default)
bulwahn@46168
  1090
qed auto
bulwahn@46168
  1091
bulwahn@46168
  1092
definition
bulwahn@46238
  1093
  "subtract_entries_raw xs ys = foldr (%(k, v). AList.map_entry k (%v'. v' - v)) ys xs"
bulwahn@46168
  1094
bulwahn@46168
  1095
lemma map_of_subtract_entries_raw:
bulwahn@46168
  1096
  "distinct (map fst ys) ==> map_of (subtract_entries_raw xs ys) x = (case map_of xs x of None => None | Some v => (case map_of ys x of None => Some v | Some v' => Some (v - v')))"
bulwahn@46168
  1097
unfolding subtract_entries_raw_def
bulwahn@46168
  1098
apply (induct ys)
bulwahn@46168
  1099
apply auto
bulwahn@46168
  1100
apply (simp split: option.split)
bulwahn@46168
  1101
apply (simp add: map_of_map_entry)
bulwahn@46168
  1102
apply (auto split: option.split)
bulwahn@46168
  1103
apply (metis map_of_eq_None_iff option.simps(3) option.simps(4))
bulwahn@46168
  1104
by (metis map_of_eq_None_iff option.simps(4) option.simps(5))
bulwahn@46168
  1105
bulwahn@46168
  1106
lemma distinct_subtract_entries_raw:
bulwahn@46168
  1107
  assumes "distinct (map fst xs)"
bulwahn@46168
  1108
  shows "distinct (map fst (subtract_entries_raw xs ys))"
bulwahn@46168
  1109
using assms
bulwahn@46168
  1110
unfolding subtract_entries_raw_def by (induct ys) (auto simp add: distinct_map_entry)
bulwahn@46168
  1111
bulwahn@47179
  1112
text {* Operations on alists with distinct keys *}
bulwahn@46168
  1113
bulwahn@47179
  1114
quotient_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist" 
bulwahn@46168
  1115
where
bulwahn@47179
  1116
  "join" is "join_raw :: ('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list"
bulwahn@47179
  1117
by (simp add: distinct_join_raw)
bulwahn@46168
  1118
bulwahn@47179
  1119
quotient_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
bulwahn@47179
  1120
where
bulwahn@47179
  1121
  "subtract_entries" is "subtract_entries_raw :: ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('a \<times> 'b) list" 
bulwahn@47179
  1122
by (simp add: distinct_subtract_entries_raw)
bulwahn@46168
  1123
bulwahn@46168
  1124
text {* Implementing multisets by means of association lists *}
haftmann@34943
  1125
haftmann@34943
  1126
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" where
haftmann@34943
  1127
  "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
haftmann@34943
  1128
haftmann@34943
  1129
lemma count_of_multiset:
haftmann@34943
  1130
  "count_of xs \<in> multiset"
haftmann@34943
  1131
proof -
haftmann@34943
  1132
  let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)}"
haftmann@34943
  1133
  have "?A \<subseteq> dom (map_of xs)"
haftmann@34943
  1134
  proof
haftmann@34943
  1135
    fix x
haftmann@34943
  1136
    assume "x \<in> ?A"
haftmann@34943
  1137
    then have "0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)" by simp
haftmann@34943
  1138
    then have "map_of xs x \<noteq> None" by (cases "map_of xs x") auto
haftmann@34943
  1139
    then show "x \<in> dom (map_of xs)" by auto
haftmann@34943
  1140
  qed
haftmann@34943
  1141
  with finite_dom_map_of [of xs] have "finite ?A"
haftmann@34943
  1142
    by (auto intro: finite_subset)
haftmann@34943
  1143
  then show ?thesis
nipkow@39302
  1144
    by (simp add: count_of_def fun_eq_iff multiset_def)
haftmann@34943
  1145
qed
haftmann@34943
  1146
haftmann@34943
  1147
lemma count_simps [simp]:
haftmann@34943
  1148
  "count_of [] = (\<lambda>_. 0)"
haftmann@34943
  1149
  "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
nipkow@39302
  1150
  by (simp_all add: count_of_def fun_eq_iff)
haftmann@34943
  1151
haftmann@34943
  1152
lemma count_of_empty:
haftmann@34943
  1153
  "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
haftmann@34943
  1154
  by (induct xs) (simp_all add: count_of_def)
haftmann@34943
  1155
haftmann@34943
  1156
lemma count_of_filter:
bulwahn@46168
  1157
  "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
haftmann@34943
  1158
  by (induct xs) auto
haftmann@34943
  1159
bulwahn@46168
  1160
lemma count_of_map_default [simp]:
bulwahn@46168
  1161
  "count_of (map_default x b (%x. x + b) xs) y = (if x = y then count_of xs x + b else count_of xs y)"
bulwahn@46168
  1162
unfolding count_of_def by (simp add: map_of_map_default split: option.split)
bulwahn@46168
  1163
bulwahn@46168
  1164
lemma count_of_join_raw:
bulwahn@46168
  1165
  "distinct (map fst ys) ==> count_of xs x + count_of ys x = count_of (join_raw (%x (x, y). x + y) xs ys) x"
bulwahn@46168
  1166
unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
bulwahn@46168
  1167
bulwahn@46168
  1168
lemma count_of_subtract_entries_raw:
bulwahn@46168
  1169
  "distinct (map fst ys) ==> count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x"
bulwahn@46168
  1170
unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
bulwahn@46168
  1171
bulwahn@46168
  1172
text {* Code equations for multiset operations *}
bulwahn@46168
  1173
bulwahn@46168
  1174
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset" where
bulwahn@46237
  1175
  "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
haftmann@34943
  1176
haftmann@34943
  1177
code_datatype Bag
haftmann@34943
  1178
haftmann@34943
  1179
lemma count_Bag [simp, code]:
bulwahn@46237
  1180
  "count (Bag xs) = count_of (DAList.impl_of xs)"
haftmann@34943
  1181
  by (simp add: Bag_def count_of_multiset Abs_multiset_inverse)
haftmann@34943
  1182
haftmann@34943
  1183
lemma Mempty_Bag [code]:
bulwahn@46394
  1184
  "{#} = Bag (DAList.empty)"
bulwahn@46394
  1185
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
bulwahn@47143
  1186
haftmann@34943
  1187
lemma single_Bag [code]:
bulwahn@46394
  1188
  "{#x#} = Bag (DAList.update x 1 DAList.empty)"
kuncar@47198
  1189
  by (simp add: multiset_eq_iff alist.Alist_inverse update.rep_eq empty.rep_eq)
bulwahn@46168
  1190
bulwahn@46168
  1191
lemma union_Bag [code]:
bulwahn@46168
  1192
  "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)"
bulwahn@46168
  1193
by (rule multiset_eqI) (simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def)
bulwahn@46168
  1194
bulwahn@46168
  1195
lemma minus_Bag [code]:
bulwahn@46168
  1196
  "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
bulwahn@46168
  1197
by (rule multiset_eqI)
bulwahn@46168
  1198
  (simp add: count_of_subtract_entries_raw alist.Alist_inverse distinct_subtract_entries_raw subtract_entries_def)
haftmann@34943
  1199
haftmann@41069
  1200
lemma filter_Bag [code]:
bulwahn@46237
  1201
  "Multiset.filter P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
kuncar@47198
  1202
by (rule multiset_eqI) (simp add: count_of_filter filter.rep_eq)
haftmann@34943
  1203
haftmann@34943
  1204
lemma mset_less_eq_Bag [code]:
bulwahn@46237
  1205
  "Bag xs \<le> A \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). count_of (DAList.impl_of xs) x \<le> count A x)"
haftmann@34943
  1206
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@34943
  1207
proof
haftmann@34943
  1208
  assume ?lhs then show ?rhs
wenzelm@46730
  1209
    by (auto simp add: mset_le_def)
haftmann@34943
  1210
next
haftmann@34943
  1211
  assume ?rhs
haftmann@34943
  1212
  show ?lhs
haftmann@34943
  1213
  proof (rule mset_less_eqI)
haftmann@34943
  1214
    fix x
bulwahn@46237
  1215
    from `?rhs` have "count_of (DAList.impl_of xs) x \<le> count A x"
bulwahn@46237
  1216
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
haftmann@34943
  1217
    then show "count (Bag xs) x \<le> count A x"
wenzelm@46730
  1218
      by (simp add: mset_le_def)
haftmann@34943
  1219
  qed
haftmann@34943
  1220
qed
haftmann@34943
  1221
haftmann@38857
  1222
instantiation multiset :: (equal) equal
haftmann@34943
  1223
begin
haftmann@34943
  1224
haftmann@34943
  1225
definition
bulwahn@45866
  1226
  [code]: "HOL.equal A B \<longleftrightarrow> (A::'a multiset) \<le> B \<and> B \<le> A"
haftmann@34943
  1227
wenzelm@46921
  1228
instance
wenzelm@46921
  1229
  by default (simp add: equal_multiset_def eq_iff)
haftmann@34943
  1230
haftmann@34943
  1231
end
haftmann@34943
  1232
bulwahn@46168
  1233
text {* Quickcheck generators *}
haftmann@38857
  1234
haftmann@34943
  1235
definition (in term_syntax)
bulwahn@46168
  1236
  bagify :: "('a\<Colon>typerep, nat) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)
haftmann@34943
  1237
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
haftmann@34943
  1238
  [code_unfold]: "bagify xs = Code_Evaluation.valtermify Bag {\<cdot>} xs"
haftmann@34943
  1239
haftmann@37751
  1240
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1241
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@34943
  1242
haftmann@34943
  1243
instantiation multiset :: (random) random
haftmann@34943
  1244
begin
haftmann@34943
  1245
haftmann@34943
  1246
definition
haftmann@37751
  1247
  "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (bagify xs))"
haftmann@34943
  1248
haftmann@34943
  1249
instance ..
haftmann@34943
  1250
haftmann@34943
  1251
end
haftmann@34943
  1252
haftmann@37751
  1253
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1254
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@34943
  1255
bulwahn@46168
  1256
instantiation multiset :: (exhaustive) exhaustive
bulwahn@46168
  1257
begin
bulwahn@46168
  1258
bulwahn@46168
  1259
definition exhaustive_multiset :: "('a multiset => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@46168
  1260
where
bulwahn@46168
  1261
  "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (%xs. f (Bag xs)) i"
bulwahn@46168
  1262
bulwahn@46168
  1263
instance ..
bulwahn@46168
  1264
bulwahn@46168
  1265
end
bulwahn@46168
  1266
bulwahn@46168
  1267
instantiation multiset :: (full_exhaustive) full_exhaustive
bulwahn@46168
  1268
begin
bulwahn@46168
  1269
bulwahn@46168
  1270
definition full_exhaustive_multiset :: "('a multiset * (unit => term) => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@46168
  1271
where
bulwahn@46168
  1272
  "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (%xs. f (bagify xs)) i"
bulwahn@46168
  1273
bulwahn@46168
  1274
instance ..
bulwahn@46168
  1275
bulwahn@46168
  1276
end
bulwahn@46168
  1277
wenzelm@36176
  1278
hide_const (open) bagify
haftmann@34943
  1279
haftmann@34943
  1280
haftmann@34943
  1281
subsection {* The multiset order *}
wenzelm@10249
  1282
wenzelm@10249
  1283
subsubsection {* Well-foundedness *}
wenzelm@10249
  1284
haftmann@28708
  1285
definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1286
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
berghofe@23751
  1287
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
wenzelm@10249
  1288
haftmann@28708
  1289
definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1290
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
  1291
berghofe@23751
  1292
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
nipkow@26178
  1293
by (simp add: mult1_def)
wenzelm@10249
  1294
berghofe@23751
  1295
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
berghofe@23751
  1296
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
berghofe@23751
  1297
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@19582
  1298
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
wenzelm@10249
  1299
proof (unfold mult1_def)
berghofe@23751
  1300
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
nipkow@11464
  1301
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
berghofe@23751
  1302
  let ?case1 = "?case1 {(N, M). ?R N M}"
wenzelm@10249
  1303
berghofe@23751
  1304
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
wenzelm@18258
  1305
  then have "\<exists>a' M0' K.
nipkow@11464
  1306
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
wenzelm@18258
  1307
  then show "?case1 \<or> ?case2"
wenzelm@10249
  1308
  proof (elim exE conjE)
wenzelm@10249
  1309
    fix a' M0' K
wenzelm@10249
  1310
    assume N: "N = M0' + K" and r: "?r K a'"
wenzelm@10249
  1311
    assume "M0 + {#a#} = M0' + {#a'#}"
wenzelm@18258
  1312
    then have "M0 = M0' \<and> a = a' \<or>
nipkow@11464
  1313
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
wenzelm@10249
  1314
      by (simp only: add_eq_conv_ex)
wenzelm@18258
  1315
    then show ?thesis
wenzelm@10249
  1316
    proof (elim disjE conjE exE)
wenzelm@10249
  1317
      assume "M0 = M0'" "a = a'"
nipkow@11464
  1318
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@18258
  1319
      then have ?case2 .. then show ?thesis ..
wenzelm@10249
  1320
    next
wenzelm@10249
  1321
      fix K'
wenzelm@10249
  1322
      assume "M0' = K' + {#a#}"
haftmann@34943
  1323
      with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
wenzelm@10249
  1324
wenzelm@10249
  1325
      assume "M0 = K' + {#a'#}"
wenzelm@10249
  1326
      with r have "?R (K' + K) M0" by blast
wenzelm@18258
  1327
      with n have ?case1 by simp then show ?thesis ..
wenzelm@10249
  1328
    qed
wenzelm@10249
  1329
  qed
wenzelm@10249
  1330
qed
wenzelm@10249
  1331
berghofe@23751
  1332
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
wenzelm@10249
  1333
proof
wenzelm@10249
  1334
  let ?R = "mult1 r"
wenzelm@10249
  1335
  let ?W = "acc ?R"
wenzelm@10249
  1336
  {
wenzelm@10249
  1337
    fix M M0 a
berghofe@23751
  1338
    assume M0: "M0 \<in> ?W"
berghofe@23751
  1339
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1340
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
berghofe@23751
  1341
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
  1342
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
  1343
      fix N
berghofe@23751
  1344
      assume "(N, M0 + {#a#}) \<in> ?R"
berghofe@23751
  1345
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
berghofe@23751
  1346
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
wenzelm@10249
  1347
        by (rule less_add)
berghofe@23751
  1348
      then show "N \<in> ?W"
wenzelm@10249
  1349
      proof (elim exE disjE conjE)
berghofe@23751
  1350
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
berghofe@23751
  1351
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
berghofe@23751
  1352
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
berghofe@23751
  1353
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
  1354
      next
wenzelm@10249
  1355
        fix K
wenzelm@10249
  1356
        assume N: "N = M0 + K"
berghofe@23751
  1357
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
berghofe@23751
  1358
        then have "M0 + K \<in> ?W"
wenzelm@10249
  1359
        proof (induct K)
wenzelm@18730
  1360
          case empty
berghofe@23751
  1361
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
  1362
        next
wenzelm@18730
  1363
          case (add K x)
berghofe@23751
  1364
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
  1365
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
  1366
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
  1367
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
haftmann@34943
  1368
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
wenzelm@10249
  1369
        qed
berghofe@23751
  1370
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
  1371
      qed
wenzelm@10249
  1372
    qed
wenzelm@10249
  1373
  } note tedious_reasoning = this
wenzelm@10249
  1374
berghofe@23751
  1375
  assume wf: "wf r"
wenzelm@10249
  1376
  fix M
berghofe@23751
  1377
  show "M \<in> ?W"
wenzelm@10249
  1378
  proof (induct M)
berghofe@23751
  1379
    show "{#} \<in> ?W"
wenzelm@10249
  1380
    proof (rule accI)
berghofe@23751
  1381
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
  1382
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
  1383
    qed
wenzelm@10249
  1384
berghofe@23751
  1385
    fix M a assume "M \<in> ?W"
berghofe@23751
  1386
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1387
    proof induct
wenzelm@10249
  1388
      fix a
berghofe@23751
  1389
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1390
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1391
      proof
berghofe@23751
  1392
        fix M assume "M \<in> ?W"
berghofe@23751
  1393
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
  1394
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
  1395
      qed
wenzelm@10249
  1396
    qed
berghofe@23751
  1397
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
wenzelm@10249
  1398
  qed
wenzelm@10249
  1399
qed
wenzelm@10249
  1400
berghofe@23751
  1401
theorem wf_mult1: "wf r ==> wf (mult1 r)"
nipkow@26178
  1402
by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
  1403
berghofe@23751
  1404
theorem wf_mult: "wf r ==> wf (mult r)"
nipkow@26178
  1405
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
  1406
wenzelm@10249
  1407
wenzelm@10249
  1408
subsubsection {* Closure-free presentation *}
wenzelm@10249
  1409
wenzelm@10249
  1410
text {* One direction. *}
wenzelm@10249
  1411
wenzelm@10249
  1412
lemma mult_implies_one_step:
berghofe@23751
  1413
  "trans r ==> (M, N) \<in> mult r ==>
nipkow@11464
  1414
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
berghofe@23751
  1415
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
nipkow@26178
  1416
apply (unfold mult_def mult1_def set_of_def)
nipkow@26178
  1417
apply (erule converse_trancl_induct, clarify)
nipkow@26178
  1418
 apply (rule_tac x = M0 in exI, simp, clarify)
nipkow@26178
  1419
apply (case_tac "a :# K")
nipkow@26178
  1420
 apply (rule_tac x = I in exI)
nipkow@26178
  1421
 apply (simp (no_asm))
nipkow@26178
  1422
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
haftmann@34943
  1423
 apply (simp (no_asm_simp) add: add_assoc [symmetric])
nipkow@26178
  1424
 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
nipkow@26178
  1425
 apply (simp add: diff_union_single_conv)
nipkow@26178
  1426
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1427
 apply blast
nipkow@26178
  1428
apply (subgoal_tac "a :# I")
nipkow@26178
  1429
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow@26178
  1430
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow@26178
  1431
 apply (rule_tac x = "K + Ka" in exI)
nipkow@26178
  1432
 apply (rule conjI)
nipkow@39302
  1433
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1434
 apply (rule conjI)
nipkow@26178
  1435
  apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
nipkow@39302
  1436
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1437
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1438
 apply blast
nipkow@26178
  1439
apply (subgoal_tac "a :# (M0 + {#a#})")
nipkow@26178
  1440
 apply simp
nipkow@26178
  1441
apply (simp (no_asm))
nipkow@26178
  1442
done
wenzelm@10249
  1443
wenzelm@10249
  1444
lemma one_step_implies_mult_aux:
berghofe@23751
  1445
  "trans r ==>
berghofe@23751
  1446
    \<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
  1447
      --> (I + K, I + J) \<in> mult r"
nipkow@26178
  1448
apply (induct_tac n, auto)
nipkow@26178
  1449
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow@26178
  1450
apply (rename_tac "J'", simp)
nipkow@26178
  1451
apply (erule notE, auto)
nipkow@26178
  1452
apply (case_tac "J' = {#}")
nipkow@26178
  1453
 apply (simp add: mult_def)
nipkow@26178
  1454
 apply (rule r_into_trancl)
nipkow@26178
  1455
 apply (simp add: mult1_def set_of_def, blast)
nipkow@26178
  1456
txt {* Now we know @{term "J' \<noteq> {#}"}. *}
nipkow@26178
  1457
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@26178
  1458
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
nipkow@26178
  1459
apply (erule ssubst)
nipkow@26178
  1460
apply (simp add: Ball_def, auto)
nipkow@26178
  1461
apply (subgoal_tac
nipkow@26178
  1462
  "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
nipkow@26178
  1463
    (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow@26178
  1464
 prefer 2
nipkow@26178
  1465
 apply force
haftmann@34943
  1466
apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
nipkow@26178
  1467
apply (erule trancl_trans)
nipkow@26178
  1468
apply (rule r_into_trancl)
nipkow@26178
  1469
apply (simp add: mult1_def set_of_def)
nipkow@26178
  1470
apply (rule_tac x = a in exI)
nipkow@26178
  1471
apply (rule_tac x = "I + J'" in exI)
haftmann@34943
  1472
apply (simp add: add_ac)
nipkow@26178
  1473
done
wenzelm@10249
  1474
wenzelm@17161
  1475
lemma one_step_implies_mult:
berghofe@23751
  1476
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
berghofe@23751
  1477
    ==> (I + K, I + J) \<in> mult r"
nipkow@26178
  1478
using one_step_implies_mult_aux by blast
wenzelm@10249
  1479
wenzelm@10249
  1480
wenzelm@10249
  1481
subsubsection {* Partial-order properties *}
wenzelm@10249
  1482
haftmann@35273
  1483
definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
haftmann@35273
  1484
  "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
wenzelm@10249
  1485
haftmann@35273
  1486
definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
haftmann@35273
  1487
  "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
haftmann@35273
  1488
haftmann@35308
  1489
notation (xsymbols) less_multiset (infix "\<subset>#" 50)
haftmann@35308
  1490
notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
wenzelm@10249
  1491
haftmann@35268
  1492
interpretation multiset_order: order le_multiset less_multiset
haftmann@35268
  1493
proof -
haftmann@35268
  1494
  have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
haftmann@35268
  1495
  proof
haftmann@35268
  1496
    fix M :: "'a multiset"
haftmann@35268
  1497
    assume "M \<subset># M"
haftmann@35268
  1498
    then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
haftmann@35268
  1499
    have "trans {(x'::'a, x). x' < x}"
haftmann@35268
  1500
      by (rule transI) simp
haftmann@35268
  1501
    moreover note MM
haftmann@35268
  1502
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
haftmann@35268
  1503
      \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})"
haftmann@35268
  1504
      by (rule mult_implies_one_step)
haftmann@35268
  1505
    then obtain I J K where "M = I + J" and "M = I + K"
haftmann@35268
  1506
      and "J \<noteq> {#}" and "(\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})" by blast
haftmann@35268
  1507
    then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_of K. \<exists>j\<in>set_of K. k < j" by auto
haftmann@35268
  1508
    have "finite (set_of K)" by simp
haftmann@35268
  1509
    moreover note aux2
haftmann@35268
  1510
    ultimately have "set_of K = {}"
haftmann@35268
  1511
      by (induct rule: finite_induct) (auto intro: order_less_trans)
haftmann@35268
  1512
    with aux1 show False by simp
haftmann@35268
  1513
  qed
haftmann@35268
  1514
  have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N"
haftmann@35268
  1515
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
wenzelm@46921
  1516
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
wenzelm@46921
  1517
    by default (auto simp add: le_multiset_def irrefl dest: trans)
haftmann@35268
  1518
qed
wenzelm@10249
  1519
wenzelm@46730
  1520
lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R"
wenzelm@46730
  1521
  by simp
haftmann@26567
  1522
wenzelm@10249
  1523
wenzelm@10249
  1524
subsubsection {* Monotonicity of multiset union *}
wenzelm@10249
  1525
wenzelm@46730
  1526
lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
nipkow@26178
  1527
apply (unfold mult1_def)
nipkow@26178
  1528
apply auto
nipkow@26178
  1529
apply (rule_tac x = a in exI)
nipkow@26178
  1530
apply (rule_tac x = "C + M0" in exI)
haftmann@34943
  1531
apply (simp add: add_assoc)
nipkow@26178
  1532
done
wenzelm@10249
  1533
haftmann@35268
  1534
lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
nipkow@26178
  1535
apply (unfold less_multiset_def mult_def)
nipkow@26178
  1536
apply (erule trancl_induct)
noschinl@40249
  1537
 apply (blast intro: mult1_union)
noschinl@40249
  1538
apply (blast intro: mult1_union trancl_trans)
nipkow@26178
  1539
done
wenzelm@10249
  1540
haftmann@35268
  1541
lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order multiset)"
haftmann@34943
  1542
apply (subst add_commute [of B C])
haftmann@34943
  1543
apply (subst add_commute [of D C])
nipkow@26178
  1544
apply (erule union_less_mono2)
nipkow@26178
  1545
done
wenzelm@10249
  1546
wenzelm@17161
  1547
lemma union_less_mono:
haftmann@35268
  1548
  "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)"
haftmann@35268
  1549
  by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
wenzelm@10249
  1550
haftmann@35268
  1551
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
haftmann@35268
  1552
proof
haftmann@35268
  1553
qed (auto simp add: le_multiset_def intro: union_less_mono2)
wenzelm@26145
  1554
paulson@15072
  1555
kleing@25610
  1556
subsection {* The fold combinator *}
kleing@25610
  1557
wenzelm@26145
  1558
text {*
wenzelm@26145
  1559
  The intended behaviour is
wenzelm@26145
  1560
  @{text "fold_mset f z {#x\<^isub>1, ..., x\<^isub>n#} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
wenzelm@26145
  1561
  if @{text f} is associative-commutative. 
kleing@25610
  1562
*}
kleing@25610
  1563
wenzelm@26145
  1564
text {*
wenzelm@26145
  1565
  The graph of @{text "fold_mset"}, @{text "z"}: the start element,
wenzelm@26145
  1566
  @{text "f"}: folding function, @{text "A"}: the multiset, @{text
wenzelm@26145
  1567
  "y"}: the result.
wenzelm@26145
  1568
*}
kleing@25610
  1569
inductive 
kleing@25759
  1570
  fold_msetG :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b \<Rightarrow> bool" 
kleing@25610
  1571
  for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" 
kleing@25610
  1572
  and z :: 'b
kleing@25610
  1573
where
kleing@25759
  1574
  emptyI [intro]:  "fold_msetG f z {#} z"
kleing@25759
  1575
| insertI [intro]: "fold_msetG f z A y \<Longrightarrow> fold_msetG f z (A + {#x#}) (f x y)"
kleing@25610
  1576
kleing@25759
  1577
inductive_cases empty_fold_msetGE [elim!]: "fold_msetG f z {#} x"
kleing@25759
  1578
inductive_cases insert_fold_msetGE: "fold_msetG f z (A + {#}) y" 
kleing@25610
  1579
kleing@25610
  1580
definition
wenzelm@26145
  1581
  fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b" where
wenzelm@26145
  1582
  "fold_mset f z A = (THE x. fold_msetG f z A x)"
kleing@25610
  1583
kleing@25759
  1584
lemma Diff1_fold_msetG:
wenzelm@26145
  1585
  "fold_msetG f z (A - {#x#}) y \<Longrightarrow> x \<in># A \<Longrightarrow> fold_msetG f z A (f x y)"
nipkow@26178
  1586
apply (frule_tac x = x in fold_msetG.insertI)
nipkow@26178
  1587
apply auto
nipkow@26178
  1588
done
kleing@25610
  1589
kleing@25759
  1590
lemma fold_msetG_nonempty: "\<exists>x. fold_msetG f z A x"
nipkow@26178
  1591
apply (induct A)
nipkow@26178
  1592
 apply blast
nipkow@26178
  1593
apply clarsimp
nipkow@26178
  1594
apply (drule_tac x = x in fold_msetG.insertI)
nipkow@26178
  1595
apply auto
nipkow@26178
  1596
done
kleing@25610
  1597
kleing@25759
  1598
lemma fold_mset_empty[simp]: "fold_mset f z {#} = z"
nipkow@26178
  1599
unfolding fold_mset_def by blast
kleing@25610
  1600
haftmann@42871
  1601
context comp_fun_commute
wenzelm@26145
  1602
begin
kleing@25610
  1603
wenzelm@26145
  1604
lemma fold_msetG_determ:
wenzelm@26145
  1605
  "fold_msetG f z A x \<Longrightarrow> fold_msetG f z A y \<Longrightarrow> y = x"
kleing@25610
  1606
proof (induct arbitrary: x y z rule: full_multiset_induct)
kleing@25610
  1607
  case (less M x\<^isub>1 x\<^isub>2 Z)
haftmann@35268
  1608
  have IH: "\<forall>A. A < M \<longrightarrow> 
kleing@25759
  1609
    (\<forall>x x' x''. fold_msetG f x'' A x \<longrightarrow> fold_msetG f x'' A x'
kleing@25610
  1610
               \<longrightarrow> x' = x)" by fact
kleing@25759
  1611
  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
  1612
  show ?case
kleing@25759
  1613
  proof (rule fold_msetG.cases [OF Mfoldx\<^isub>1])
kleing@25610
  1614
    assume "M = {#}" and "x\<^isub>1 = Z"
wenzelm@26145
  1615
    then show ?case using Mfoldx\<^isub>2 by auto 
kleing@25610
  1616
  next
kleing@25610
  1617
    fix B b u
kleing@25759
  1618
    assume "M = B + {#b#}" and "x\<^isub>1 = f b u" and Bu: "fold_msetG f Z B u"
wenzelm@26145
  1619
    then have MBb: "M = B + {#b#}" and x\<^isub>1: "x\<^isub>1 = f b u" by auto
kleing@25610
  1620
    show ?case
kleing@25759
  1621
    proof (rule fold_msetG.cases [OF Mfoldx\<^isub>2])
kleing@25610
  1622
      assume "M = {#}" "x\<^isub>2 = Z"
wenzelm@26145
  1623
      then show ?case using Mfoldx\<^isub>1 by auto
kleing@25610
  1624
    next
kleing@25610
  1625
      fix C c v
kleing@25759
  1626
      assume "M = C + {#c#}" and "x\<^isub>2 = f c v" and Cv: "fold_msetG f Z C v"
wenzelm@26145
  1627
      then have MCc: "M = C + {#c#}" and x\<^isub>2: "x\<^isub>2 = f c v" by auto
haftmann@35268
  1628
      then have CsubM: "C < M" by simp
haftmann@35268
  1629
      from MBb have BsubM: "B < M" by simp
kleing@25610
  1630
      show ?case
kleing@25610
  1631
      proof cases
wenzelm@46730
  1632
        assume *: "b = c"
wenzelm@46730
  1633
        then have "B = C" using MBb MCc by auto
wenzelm@46730
  1634
        with * show ?thesis using Bu Cv x\<^isub>1 x\<^isub>2 CsubM IH by auto
kleing@25610
  1635
      next
kleing@25610
  1636
        assume diff: "b \<noteq> c"
kleing@25610
  1637
        let ?D = "B - {#c#}"
kleing@25610
  1638
        have cinB: "c \<in># B" and binC: "b \<in># C" using MBb MCc diff
kleing@25610
  1639
          by (auto intro: insert_noteq_member dest: sym)
haftmann@35268
  1640
        have "B - {#c#} < B" using cinB by (rule mset_less_diff_self)
haftmann@35268
  1641
        then have DsubM: "?D < M" using BsubM by (blast intro: order_less_trans)
kleing@25610
  1642
        from MBb MCc have "B + {#b#} = C + {#c#}" by blast
wenzelm@26145
  1643
        then have [simp]: "B + {#b#} - {#c#} = C"
kleing@25610
  1644
          using MBb MCc binC cinB by auto
kleing@25610
  1645
        have B: "B = ?D + {#c#}" and C: "C = ?D + {#b#}"
kleing@25610
  1646
          using MBb MCc diff binC cinB
kleing@25610
  1647
          by (auto simp: multiset_add_sub_el_shuffle)
kleing@25759
  1648
        then obtain d where Dfoldd: "fold_msetG f Z ?D d"
kleing@25759
  1649
          using fold_msetG_nonempty by iprover
wenzelm@26145
  1650
        then have "fold_msetG f Z B (f c d)" using cinB
kleing@25759
  1651
          by (rule Diff1_fold_msetG)
wenzelm@26145
  1652
        then have "f c d = u" using IH BsubM Bu by blast
kleing@25610
  1653
        moreover 
kleing@25759
  1654
        have "fold_msetG f Z C (f b d)" using binC cinB diff Dfoldd
kleing@25610
  1655
          by (auto simp: multiset_add_sub_el_shuffle 
kleing@25759
  1656
            dest: fold_msetG.insertI [where x=b])
wenzelm@26145
  1657
        then have "f b d = v" using IH CsubM Cv by blast
kleing@25610
  1658
        ultimately show ?thesis using x\<^isub>1 x\<^isub>2
haftmann@34943
  1659
          by (auto simp: fun_left_comm)
kleing@25610
  1660
      qed
kleing@25610
  1661
    qed
kleing@25610
  1662
  qed
kleing@25610
  1663
qed
kleing@25610
  1664
        
wenzelm@26145
  1665
lemma fold_mset_insert_aux:
wenzelm@26145
  1666
  "(fold_msetG f z (A + {#x#}) v) =
kleing@25759
  1667
    (\<exists>y. fold_msetG f z A y \<and> v = f x y)"
nipkow@26178
  1668
apply (rule iffI)
nipkow@26178
  1669
 prefer 2
nipkow@26178
  1670
 apply blast
wenzelm@45608
  1671
apply (rule_tac A1=A and f1=f in fold_msetG_nonempty [THEN exE])
nipkow@26178
  1672
apply (blast intro: fold_msetG_determ)
nipkow@26178
  1673
done
kleing@25610
  1674
wenzelm@26145
  1675
lemma fold_mset_equality: "fold_msetG f z A y \<Longrightarrow> fold_mset f z A = y"
nipkow@26178
  1676
unfolding fold_mset_def by (blast intro: fold_msetG_determ)
kleing@25610
  1677
wenzelm@26145
  1678
lemma fold_mset_insert:
nipkow@26178
  1679
  "fold_mset f z (A + {#x#}) = f x (fold_mset f z A)"
nipkow@26178
  1680
apply (simp add: fold_mset_def fold_mset_insert_aux)
nipkow@26178
  1681
apply (rule the_equality)
nipkow@26178
  1682
 apply (auto cong add: conj_cong 
wenzelm@26145
  1683
     simp add: fold_mset_def [symmetric] fold_mset_equality fold_msetG_nonempty)
nipkow@26178
  1684
done
kleing@25610
  1685
wenzelm@26145
  1686
lemma fold_mset_commute: "f x (fold_mset f z A) = fold_mset f (f x z) A"
haftmann@34943
  1687
by (induct A) (auto simp: fold_mset_insert fun_left_comm [of x])
nipkow@26178
  1688
wenzelm@26145
  1689
lemma fold_mset_single [simp]: "fold_mset f z {#x#} = f x z"
nipkow@26178
  1690
using fold_mset_insert [of z "{#}"] by simp
kleing@25610
  1691
wenzelm@26145
  1692
lemma fold_mset_union [simp]:
wenzelm@26145
  1693
  "fold_mset f z (A+B) = fold_mset f (fold_mset f z A) B"
kleing@25759
  1694
proof (induct A)
wenzelm@26145
  1695
  case empty then show ?case by simp
kleing@25759
  1696
next
wenzelm@26145
  1697
  case (add A x)
haftmann@34943
  1698
  have "A + {#x#} + B = (A+B) + {#x#}" by (simp add: add_ac)
wenzelm@26145
  1699
  then have "fold_mset f z (A + {#x#} + B) = f x (fold_mset f z (A + B))" 
wenzelm@26145
  1700
    by (simp add: fold_mset_insert)
wenzelm@26145
  1701
  also have "\<dots> = fold_mset f (fold_mset f z (A + {#x#})) B"
wenzelm@26145
  1702
    by (simp add: fold_mset_commute[of x,symmetric] add fold_mset_insert)
wenzelm@26145
  1703
  finally show ?case .
kleing@25759
  1704
qed
kleing@25759
  1705
wenzelm@26145
  1706
lemma fold_mset_fusion:
haftmann@42871
  1707
  assumes "comp_fun_commute g"
ballarin@27611
  1708
  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
  1709
proof -
haftmann@42871
  1710
  interpret comp_fun_commute g by (fact assms)
ballarin@27611
  1711
  show "PROP ?P" by (induct A) auto
ballarin@27611
  1712
qed
kleing@25610
  1713
wenzelm@26145
  1714
lemma fold_mset_rec:
wenzelm@26145
  1715
  assumes "a \<in># A" 
kleing@25759
  1716
  shows "fold_mset f z A = f a (fold_mset f z (A - {#a#}))"
kleing@25610
  1717
proof -
wenzelm@26145
  1718
  from assms obtain A' where "A = A' + {#a#}"
wenzelm@26145
  1719
    by (blast dest: multi_member_split)
wenzelm@26145
  1720
  then show ?thesis by simp
kleing@25610
  1721
qed
kleing@25610
  1722
wenzelm@26145
  1723
end
wenzelm@26145
  1724
wenzelm@26145
  1725
text {*
wenzelm@26145
  1726
  A note on code generation: When defining some function containing a
wenzelm@26145
  1727
  subterm @{term"fold_mset F"}, code generation is not automatic. When
wenzelm@26145
  1728
  interpreting locale @{text left_commutative} with @{text F}, the
wenzelm@26145
  1729
  would be code thms for @{const fold_mset} become thms like
wenzelm@26145
  1730
  @{term"fold_mset F z {#} = z"} where @{text F} is not a pattern but
wenzelm@26145
  1731
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
wenzelm@26145
  1732
  constant with its own code thms needs to be introduced for @{text
wenzelm@26145
  1733
  F}. See the image operator below.
wenzelm@26145
  1734
*}
wenzelm@26145
  1735
nipkow@26016
  1736
nipkow@26016
  1737
subsection {* Image *}
nipkow@26016
  1738
haftmann@34943
  1739
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
haftmann@34943
  1740
  "image_mset f = fold_mset (op + o single o f) {#}"
nipkow@26016
  1741
wenzelm@44339
  1742
interpretation image_fun_commute: comp_fun_commute "op + o single o f" for f
haftmann@42809
  1743
proof qed (simp add: add_ac fun_eq_iff)
nipkow@26016
  1744
haftmann@28708
  1745
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
nipkow@26178
  1746
by (simp add: image_mset_def)
nipkow@26016
  1747
haftmann@28708
  1748
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
nipkow@26178
  1749
by (simp add: image_mset_def)
nipkow@26016
  1750
nipkow@26016
  1751
lemma image_mset_insert:
nipkow@26016
  1752
  "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
nipkow@26178
  1753
by (simp add: image_mset_def add_ac)
nipkow@26016
  1754
haftmann@28708
  1755
lemma image_mset_union [simp]:
nipkow@26016
  1756
  "image_mset f (M+N) = image_mset f M + image_mset f N"
nipkow@26178
  1757
apply (induct N)
nipkow@26178
  1758
 apply simp
haftmann@34943
  1759
apply (simp add: add_assoc [symmetric] image_mset_insert)
nipkow@26178
  1760
done
nipkow@26016
  1761
wenzelm@26145
  1762
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
nipkow@26178
  1763
by (induct M) simp_all
nipkow@26016
  1764
wenzelm@26145
  1765
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
nipkow@26178
  1766
by (cases M) auto
nipkow@26016
  1767
wenzelm@26145
  1768
syntax
wenzelm@35352
  1769
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
wenzelm@26145
  1770
      ("({#_/. _ :# _#})")
wenzelm@26145
  1771
translations
wenzelm@26145
  1772
  "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
nipkow@26016
  1773
wenzelm@26145
  1774
syntax
wenzelm@35352
  1775
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
wenzelm@26145
  1776
      ("({#_/ | _ :# _./ _#})")
nipkow@26016
  1777
translations
nipkow@26033
  1778
  "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
nipkow@26016
  1779
wenzelm@26145
  1780
text {*
wenzelm@26145
  1781
  This allows to write not just filters like @{term "{#x:#M. x<c#}"}
wenzelm@26145
  1782
  but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
wenzelm@26145
  1783
  "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
wenzelm@26145
  1784
  @{term "{#x+x|x:#M. x<c#}"}.
wenzelm@26145
  1785
*}
nipkow@26016
  1786
wenzelm@46730
  1787
enriched_type image_mset: image_mset
wenzelm@46730
  1788
proof -
wenzelm@46921
  1789
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
haftmann@41372
  1790
  proof
haftmann@41372
  1791
    fix A
haftmann@41372
  1792
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
haftmann@41372
  1793
      by (induct A) simp_all
haftmann@41372
  1794
  qed
haftmann@41372
  1795
  show "image_mset id = id"
haftmann@41372
  1796
  proof
haftmann@41372
  1797
    fix A
haftmann@41372
  1798
    show "image_mset id A = id A"
haftmann@41372
  1799
      by (induct A) simp_all
haftmann@41372
  1800
  qed
haftmann@40606
  1801
qed
haftmann@40606
  1802
krauss@29125
  1803
krauss@29125
  1804
subsection {* Termination proofs with multiset orders *}
krauss@29125
  1805
krauss@29125
  1806
lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
krauss@29125
  1807
  and multi_member_this: "x \<in># {# x #} + XS"
krauss@29125
  1808
  and multi_member_last: "x \<in># {# x #}"
krauss@29125
  1809
  by auto
krauss@29125
  1810
krauss@29125
  1811
definition "ms_strict = mult pair_less"
haftmann@37765
  1812
definition "ms_weak = ms_strict \<union> Id"
krauss@29125
  1813
krauss@29125
  1814
lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
krauss@29125
  1815
unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
krauss@29125
  1816
by (auto intro: wf_mult1 wf_trancl simp: mult_def)
krauss@29125
  1817
krauss@29125
  1818
lemma smsI:
krauss@29125
  1819
  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
krauss@29125
  1820
  unfolding ms_strict_def
krauss@29125
  1821
by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
krauss@29125
  1822
krauss@29125
  1823
lemma wmsI:
krauss@29125
  1824
  "(set_of A, set_of B) \<in> max_strict \<or> A = {#} \<and> B = {#}
krauss@29125
  1825
  \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
krauss@29125
  1826
unfolding ms_weak_def ms_strict_def
krauss@29125
  1827
by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
krauss@29125
  1828
krauss@29125
  1829
inductive pw_leq
krauss@29125
  1830
where
krauss@29125
  1831
  pw_leq_empty: "pw_leq {#} {#}"
krauss@29125
  1832
| pw_leq_step:  "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
krauss@29125
  1833
krauss@29125
  1834
lemma pw_leq_lstep:
krauss@29125
  1835
  "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
krauss@29125
  1836
by (drule pw_leq_step) (rule pw_leq_empty, simp)
krauss@29125
  1837
krauss@29125
  1838
lemma pw_leq_split:
krauss@29125
  1839
  assumes "pw_leq X Y"
krauss@29125
  1840
  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
  1841
  using assms
krauss@29125
  1842
proof (induct)
krauss@29125
  1843
  case pw_leq_empty thus ?case by auto
krauss@29125
  1844
next
krauss@29125
  1845
  case (pw_leq_step x y X Y)
krauss@29125
  1846
  then obtain A B Z where
krauss@29125
  1847
    [simp]: "X = A + Z" "Y = B + Z" 
krauss@29125
  1848
      and 1[simp]: "(set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#})" 
krauss@29125
  1849
    by auto
krauss@29125
  1850
  from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less" 
krauss@29125
  1851
    unfolding pair_leq_def by auto
krauss@29125
  1852
  thus ?case
krauss@29125
  1853
  proof
krauss@29125
  1854
    assume [simp]: "x = y"
krauss@29125
  1855
    have
krauss@29125
  1856
      "{#x#} + X = A + ({#y#}+Z) 
krauss@29125
  1857
      \<and> {#y#} + Y = B + ({#y#}+Z)
krauss@29125
  1858
      \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
krauss@29125
  1859
      by (auto simp: add_ac)
krauss@29125
  1860
    thus ?case by (intro exI)
krauss@29125
  1861
  next
krauss@29125
  1862
    assume A: "(x, y) \<in> pair_less"
krauss@29125
  1863
    let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
krauss@29125
  1864
    have "{#x#} + X = ?A' + Z"
krauss@29125
  1865
      "{#y#} + Y = ?B' + Z"
krauss@29125
  1866
      by (auto simp add: add_ac)
krauss@29125
  1867
    moreover have 
krauss@29125
  1868
      "(set_of ?A', set_of ?B') \<in> max_strict"
krauss@29125
  1869
      using 1 A unfolding max_strict_def 
krauss@29125
  1870
      by (auto elim!: max_ext.cases)
krauss@29125
  1871
    ultimately show ?thesis by blast
krauss@29125
  1872
  qed
krauss@29125
  1873
qed
krauss@29125
  1874
krauss@29125
  1875
lemma 
krauss@29125
  1876
  assumes pwleq: "pw_leq Z Z'"
krauss@29125
  1877
  shows ms_strictI: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
krauss@29125
  1878
  and   ms_weakI1:  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
krauss@29125
  1879
  and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
krauss@29125
  1880
proof -
krauss@29125
  1881
  from pw_leq_split[OF pwleq] 
krauss@29125
  1882
  obtain A' B' Z''
krauss@29125
  1883
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
krauss@29125
  1884
    and mx_or_empty: "(set_of A', set_of B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
krauss@29125
  1885
    by blast
krauss@29125
  1886
  {
krauss@29125
  1887
    assume max: "(set_of A, set_of B) \<in> max_strict"
krauss@29125
  1888
    from mx_or_empty
krauss@29125
  1889
    have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
krauss@29125
  1890
    proof
krauss@29125
  1891
      assume max': "(set_of A', set_of B') \<in> max_strict"
krauss@29125
  1892
      with max have "(set_of (A + A'), set_of (B + B')) \<in> max_strict"
krauss@29125
  1893
        by (auto simp: max_strict_def intro: max_ext_additive)
krauss@29125
  1894
      thus ?thesis by (rule smsI) 
krauss@29125
  1895
    next
krauss@29125
  1896
      assume [simp]: "A' = {#} \<and> B' = {#}"
krauss@29125
  1897
      show ?thesis by (rule smsI) (auto intro: max)
krauss@29125
  1898
    qed
krauss@29125
  1899
    thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add:add_ac)
krauss@29125
  1900
    thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
krauss@29125
  1901
  }
krauss@29125
  1902
  from mx_or_empty
krauss@29125
  1903
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
krauss@29125
  1904
  thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:add_ac)
krauss@29125
  1905
qed
krauss@29125
  1906
nipkow@39301
  1907
lemma empty_neutral: "{#} + x = x" "x + {#} = x"
krauss@29125
  1908
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
krauss@29125
  1909
and nonempty_single: "{# x #} \<noteq> {#}"
krauss@29125
  1910
by auto
krauss@29125
  1911
krauss@29125
  1912
setup {*
krauss@29125
  1913
let
wenzelm@35402
  1914
  fun msetT T = Type (@{type_name multiset}, [T]);
krauss@29125
  1915
wenzelm@35402
  1916
  fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
krauss@29125
  1917
    | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
krauss@29125
  1918
    | mk_mset T (x :: xs) =
krauss@29125
  1919
          Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
krauss@29125
  1920
                mk_mset T [x] $ mk_mset T xs
krauss@29125
  1921
krauss@29125
  1922
  fun mset_member_tac m i =
krauss@29125
  1923
      (if m <= 0 then
krauss@29125
  1924
           rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
krauss@29125
  1925
       else
krauss@29125
  1926
           rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
krauss@29125
  1927
krauss@29125
  1928
  val mset_nonempty_tac =
krauss@29125
  1929
      rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
krauss@29125
  1930
krauss@29125
  1931
  val regroup_munion_conv =
wenzelm@35402
  1932
      Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
nipkow@39301
  1933
        (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
krauss@29125
  1934
krauss@29125
  1935
  fun unfold_pwleq_tac i =
krauss@29125
  1936
    (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
krauss@29125
  1937
      ORELSE (rtac @{thm pw_leq_lstep} i)
krauss@29125
  1938
      ORELSE (rtac @{thm pw_leq_empty} i)
krauss@29125
  1939
krauss@29125
  1940
  val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
krauss@29125
  1941
                      @{thm Un_insert_left}, @{thm Un_empty_left}]
krauss@29125
  1942
in
krauss@29125
  1943
  ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset 
krauss@29125
  1944
  {
krauss@29125
  1945
    msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
krauss@29125
  1946
    mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
krauss@29125
  1947
    mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
wenzelm@30595
  1948
    smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
wenzelm@30595
  1949
    reduction_pair= @{thm ms_reduction_pair}
krauss@29125
  1950
  })
wenzelm@10249
  1951
end
krauss@29125
  1952
*}
krauss@29125
  1953
haftmann@34943
  1954
haftmann@34943
  1955
subsection {* Legacy theorem bindings *}
haftmann@34943
  1956
nipkow@39302
  1957
lemmas multi_count_eq = multiset_eq_iff [symmetric]
haftmann@34943
  1958
haftmann@34943
  1959
lemma union_commute: "M + N = N + (M::'a multiset)"
haftmann@34943
  1960
  by (fact add_commute)
haftmann@34943
  1961
haftmann@34943
  1962
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
haftmann@34943
  1963
  by (fact add_assoc)
haftmann@34943
  1964
haftmann@34943
  1965
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
haftmann@34943
  1966
  by (fact add_left_commute)
haftmann@34943
  1967
haftmann@34943
  1968
lemmas union_ac = union_assoc union_commute union_lcomm
haftmann@34943
  1969
haftmann@34943
  1970
lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1971
  by (fact add_right_cancel)
haftmann@34943
  1972
haftmann@34943
  1973
lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1974
  by (fact add_left_cancel)
haftmann@34943
  1975
haftmann@34943
  1976
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
haftmann@34943
  1977
  by (fact add_imp_eq)
haftmann@34943
  1978
haftmann@35268
  1979
lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N"
haftmann@35268
  1980
  by (fact order_less_trans)
haftmann@35268
  1981
haftmann@35268
  1982
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
haftmann@35268
  1983
  by (fact inf.commute)
haftmann@35268
  1984
haftmann@35268
  1985
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
haftmann@35268
  1986
  by (fact inf.assoc [symmetric])
haftmann@35268
  1987
haftmann@35268
  1988
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
haftmann@35268
  1989
  by (fact inf.left_commute)
haftmann@35268
  1990
haftmann@35268
  1991
lemmas multiset_inter_ac =
haftmann@35268
  1992
  multiset_inter_commute
haftmann@35268
  1993
  multiset_inter_assoc
haftmann@35268
  1994
  multiset_inter_left_commute
haftmann@35268
  1995
haftmann@35268
  1996
lemma mult_less_not_refl:
haftmann@35268
  1997
  "\<not> M \<subset># (M::'a::order multiset)"
haftmann@35268
  1998
  by (fact multiset_order.less_irrefl)
haftmann@35268
  1999
haftmann@35268
  2000
lemma mult_less_trans:
haftmann@35268
  2001
  "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)"
haftmann@35268
  2002
  by (fact multiset_order.less_trans)
haftmann@35268
  2003
    
haftmann@35268
  2004
lemma mult_less_not_sym:
haftmann@35268
  2005
  "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
haftmann@35268
  2006
  by (fact multiset_order.less_not_sym)
haftmann@35268
  2007
haftmann@35268
  2008
lemma mult_less_asym:
haftmann@35268
  2009
  "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P"
haftmann@35268
  2010
  by (fact multiset_order.less_asym)
haftmann@34943
  2011
blanchet@35712
  2012
ML {*
blanchet@35712
  2013
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
blanchet@35712
  2014
                      (Const _ $ t') =
blanchet@35712
  2015
    let
blanchet@35712
  2016
      val (maybe_opt, ps) =
blanchet@35712
  2017
        Nitpick_Model.dest_plain_fun t' ||> op ~~
blanchet@35712
  2018
        ||> map (apsnd (snd o HOLogic.dest_number))
blanchet@35712
  2019
      fun elems_for t =
blanchet@35712
  2020
        case AList.lookup (op =) ps t of
blanchet@35712
  2021
          SOME n => replicate n t
blanchet@35712
  2022
        | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]
blanchet@35712
  2023
    in
blanchet@35712
  2024
      case maps elems_for (all_values elem_T) @
blanchet@37261
  2025
           (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
blanchet@37261
  2026
            else []) of
blanchet@35712
  2027
        [] => Const (@{const_name zero_class.zero}, T)
blanchet@35712
  2028
      | ts => foldl1 (fn (t1, t2) =>
blanchet@35712
  2029
                         Const (@{const_name plus_class.plus}, T --> T --> T)
blanchet@35712
  2030
                         $ t1 $ t2)
blanchet@35712
  2031
                     (map (curry (op $) (Const (@{const_name single},
blanchet@35712
  2032
                                                elem_T --> T))) ts)
blanchet@35712
  2033
    end
blanchet@35712
  2034
  | multiset_postproc _ _ _ _ t = t
blanchet@35712
  2035
*}
blanchet@35712
  2036
blanchet@38287
  2037
declaration {*
blanchet@38287
  2038
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
blanchet@38242
  2039
    multiset_postproc
blanchet@35712
  2040
*}
blanchet@35712
  2041
blanchet@37169
  2042
end