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