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