src/HOL/Library/Multiset.thy
author haftmann
Thu Oct 11 11:56:42 2012 +0200 (2012-10-11)
changeset 49822 0cfc1651be25
parent 49717 56494eedf493
child 49823 1c146fa7701e
permissions -rw-r--r--
simplified construction of fold combinator on multisets;
more coherent name for fold combinator on multisets
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
haftmann@49822
   660
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
huffman@48023
   661
where
haftmann@49822
   662
  "fold f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_of M)"
huffman@48023
   663
haftmann@49822
   664
lemma fold_mset_empty [simp]:
haftmann@49822
   665
  "fold f s {#} = s"
haftmann@49822
   666
  by (simp add: fold_def)
huffman@48023
   667
huffman@48023
   668
context comp_fun_commute
huffman@48023
   669
begin
huffman@48023
   670
haftmann@49822
   671
lemma fold_mset_insert:
haftmann@49822
   672
  "fold f s (M + {#x#}) = f x (fold f s M)"
haftmann@49822
   673
proof -
haftmann@49822
   674
  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
haftmann@49822
   675
    by (fact comp_fun_commute_funpow)
haftmann@49822
   676
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
haftmann@49822
   677
    by (fact comp_fun_commute_funpow)
haftmann@49822
   678
  show ?thesis
haftmann@49822
   679
  proof (cases "x \<in> set_of M")
haftmann@49822
   680
    case False
haftmann@49822
   681
    then have *: "count (M + {#x#}) x = 1" by simp
haftmann@49822
   682
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_of M) =
haftmann@49822
   683
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_of M)"
haftmann@49822
   684
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
   685
    with False * show ?thesis
haftmann@49822
   686
      by (simp add: fold_def del: count_union)
huffman@48023
   687
  next
haftmann@49822
   688
    case True
haftmann@49822
   689
    def N \<equiv> "set_of M - {x}"
haftmann@49822
   690
    from N_def True have *: "set_of M = insert x N" "x \<notin> N" "finite N" by auto
haftmann@49822
   691
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
haftmann@49822
   692
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
haftmann@49822
   693
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
   694
    with * show ?thesis by (simp add: fold_def del: count_union) simp
huffman@48023
   695
  qed
huffman@48023
   696
qed
huffman@48023
   697
haftmann@49822
   698
corollary fold_mset_single [simp]:
haftmann@49822
   699
  "fold f s {#x#} = f x s"
haftmann@49822
   700
proof -
haftmann@49822
   701
  have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
haftmann@49822
   702
  then show ?thesis by simp
haftmann@49822
   703
qed
huffman@48023
   704
haftmann@49822
   705
lemma fold_mset_fun_comm:
haftmann@49822
   706
  "f x (fold f s M) = fold f (f x s) M"
haftmann@49822
   707
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
huffman@48023
   708
huffman@48023
   709
lemma fold_mset_union [simp]:
haftmann@49822
   710
  "fold f s (M + N) = fold f (fold f s M) N"
haftmann@49822
   711
proof (induct M)
huffman@48023
   712
  case empty then show ?case by simp
huffman@48023
   713
next
haftmann@49822
   714
  case (add M x)
haftmann@49822
   715
  have "M + {#x#} + N = (M + N) + {#x#}"
haftmann@49822
   716
    by (simp add: add_ac)
haftmann@49822
   717
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_comm)
huffman@48023
   718
qed
huffman@48023
   719
huffman@48023
   720
lemma fold_mset_fusion:
huffman@48023
   721
  assumes "comp_fun_commute g"
haftmann@49822
   722
  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold g w A) = fold f (h w) A" (is "PROP ?P")
huffman@48023
   723
proof -
huffman@48023
   724
  interpret comp_fun_commute g by (fact assms)
huffman@48023
   725
  show "PROP ?P" by (induct A) auto
huffman@48023
   726
qed
huffman@48023
   727
huffman@48023
   728
end
huffman@48023
   729
huffman@48023
   730
text {*
huffman@48023
   731
  A note on code generation: When defining some function containing a
haftmann@49822
   732
  subterm @{term "fold F"}, code generation is not automatic. When
huffman@48023
   733
  interpreting locale @{text left_commutative} with @{text F}, the
haftmann@49822
   734
  would be code thms for @{const fold} become thms like
haftmann@49822
   735
  @{term "fold F z {#} = z"} where @{text F} is not a pattern but
huffman@48023
   736
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
huffman@48023
   737
  constant with its own code thms needs to be introduced for @{text
huffman@48023
   738
  F}. See the image operator below.
huffman@48023
   739
*}
huffman@48023
   740
huffman@48023
   741
huffman@48023
   742
subsection {* Image *}
huffman@48023
   743
huffman@48023
   744
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
haftmann@49822
   745
  "image_mset f = fold (plus o single o f) {#}"
huffman@48023
   746
huffman@48023
   747
interpretation image_fun_commute: comp_fun_commute "op + o single o f" for f
huffman@48023
   748
proof qed (simp add: add_ac fun_eq_iff)
huffman@48023
   749
huffman@48023
   750
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
huffman@48023
   751
by (simp add: image_mset_def)
huffman@48023
   752
huffman@48023
   753
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
huffman@48023
   754
by (simp add: image_mset_def)
huffman@48023
   755
huffman@48023
   756
lemma image_mset_insert:
huffman@48023
   757
  "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
huffman@48023
   758
by (simp add: image_mset_def add_ac)
huffman@48023
   759
huffman@48023
   760
lemma image_mset_union [simp]:
huffman@48023
   761
  "image_mset f (M+N) = image_mset f M + image_mset f N"
huffman@48023
   762
apply (induct N)
huffman@48023
   763
 apply simp
huffman@48023
   764
apply (simp add: add_assoc [symmetric] image_mset_insert)
huffman@48023
   765
done
huffman@48023
   766
huffman@48040
   767
lemma set_of_image_mset [simp]: "set_of (image_mset f M) = image f (set_of M)"
huffman@48040
   768
by (induct M) simp_all
huffman@48040
   769
huffman@48023
   770
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
huffman@48023
   771
by (induct M) simp_all
huffman@48023
   772
huffman@48023
   773
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
huffman@48023
   774
by (cases M) auto
huffman@48023
   775
huffman@48023
   776
syntax
huffman@48023
   777
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
huffman@48023
   778
      ("({#_/. _ :# _#})")
huffman@48023
   779
translations
huffman@48023
   780
  "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
huffman@48023
   781
huffman@48023
   782
syntax
huffman@48023
   783
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
huffman@48023
   784
      ("({#_/ | _ :# _./ _#})")
huffman@48023
   785
translations
huffman@48023
   786
  "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
huffman@48023
   787
huffman@48023
   788
text {*
huffman@48023
   789
  This allows to write not just filters like @{term "{#x:#M. x<c#}"}
huffman@48023
   790
  but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
huffman@48023
   791
  "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
huffman@48023
   792
  @{term "{#x+x|x:#M. x<c#}"}.
huffman@48023
   793
*}
huffman@48023
   794
huffman@48023
   795
enriched_type image_mset: image_mset
huffman@48023
   796
proof -
huffman@48023
   797
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
huffman@48023
   798
  proof
huffman@48023
   799
    fix A
huffman@48023
   800
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
huffman@48023
   801
      by (induct A) simp_all
huffman@48023
   802
  qed
huffman@48023
   803
  show "image_mset id = id"
huffman@48023
   804
  proof
huffman@48023
   805
    fix A
huffman@48023
   806
    show "image_mset id A = id A"
huffman@48023
   807
      by (induct A) simp_all
huffman@48023
   808
  qed
huffman@48023
   809
qed
huffman@48023
   810
haftmann@49717
   811
declare image_mset.identity [simp]
haftmann@49717
   812
huffman@48023
   813
haftmann@34943
   814
subsection {* Alternative representations *}
haftmann@34943
   815
haftmann@34943
   816
subsubsection {* Lists *}
haftmann@34943
   817
haftmann@34943
   818
primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where
haftmann@34943
   819
  "multiset_of [] = {#}" |
haftmann@34943
   820
  "multiset_of (a # x) = multiset_of x + {# a #}"
haftmann@34943
   821
haftmann@37107
   822
lemma in_multiset_in_set:
haftmann@37107
   823
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
haftmann@37107
   824
  by (induct xs) simp_all
haftmann@37107
   825
haftmann@37107
   826
lemma count_multiset_of:
haftmann@37107
   827
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@37107
   828
  by (induct xs) simp_all
haftmann@37107
   829
haftmann@34943
   830
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
haftmann@34943
   831
by (induct x) auto
haftmann@34943
   832
haftmann@34943
   833
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
haftmann@34943
   834
by (induct x) auto
haftmann@34943
   835
haftmann@40950
   836
lemma set_of_multiset_of[simp]: "set_of (multiset_of x) = set x"
haftmann@34943
   837
by (induct x) auto
haftmann@34943
   838
haftmann@34943
   839
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
haftmann@34943
   840
by (induct xs) auto
haftmann@34943
   841
huffman@48012
   842
lemma size_multiset_of [simp]: "size (multiset_of xs) = length xs"
huffman@48012
   843
  by (induct xs) simp_all
huffman@48012
   844
haftmann@34943
   845
lemma multiset_of_append [simp]:
haftmann@34943
   846
  "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
haftmann@34943
   847
  by (induct xs arbitrary: ys) (auto simp: add_ac)
haftmann@34943
   848
haftmann@40303
   849
lemma multiset_of_filter:
haftmann@40303
   850
  "multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
haftmann@40303
   851
  by (induct xs) simp_all
haftmann@40303
   852
haftmann@40950
   853
lemma multiset_of_rev [simp]:
haftmann@40950
   854
  "multiset_of (rev xs) = multiset_of xs"
haftmann@40950
   855
  by (induct xs) simp_all
haftmann@40950
   856
haftmann@34943
   857
lemma surj_multiset_of: "surj multiset_of"
haftmann@34943
   858
apply (unfold surj_def)
haftmann@34943
   859
apply (rule allI)
haftmann@34943
   860
apply (rule_tac M = y in multiset_induct)
haftmann@34943
   861
 apply auto
haftmann@34943
   862
apply (rule_tac x = "x # xa" in exI)
haftmann@34943
   863
apply auto
haftmann@34943
   864
done
haftmann@34943
   865
haftmann@34943
   866
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
haftmann@34943
   867
by (induct x) auto
haftmann@34943
   868
haftmann@34943
   869
lemma distinct_count_atmost_1:
haftmann@34943
   870
  "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
haftmann@34943
   871
apply (induct x, simp, rule iffI, simp_all)
haftmann@34943
   872
apply (rule conjI)
haftmann@34943
   873
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
haftmann@34943
   874
apply (erule_tac x = a in allE, simp, clarify)
haftmann@34943
   875
apply (erule_tac x = aa in allE, simp)
haftmann@34943
   876
done
haftmann@34943
   877
haftmann@34943
   878
lemma multiset_of_eq_setD:
haftmann@34943
   879
  "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
nipkow@39302
   880
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
haftmann@34943
   881
haftmann@34943
   882
lemma set_eq_iff_multiset_of_eq_distinct:
haftmann@34943
   883
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
haftmann@34943
   884
    (set x = set y) = (multiset_of x = multiset_of y)"
nipkow@39302
   885
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
haftmann@34943
   886
haftmann@34943
   887
lemma set_eq_iff_multiset_of_remdups_eq:
haftmann@34943
   888
   "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
haftmann@34943
   889
apply (rule iffI)
haftmann@34943
   890
apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
haftmann@34943
   891
apply (drule distinct_remdups [THEN distinct_remdups
haftmann@34943
   892
      [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
haftmann@34943
   893
apply simp
haftmann@34943
   894
done
haftmann@34943
   895
haftmann@34943
   896
lemma multiset_of_compl_union [simp]:
haftmann@34943
   897
  "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
haftmann@34943
   898
  by (induct xs) (auto simp: add_ac)
haftmann@34943
   899
haftmann@41069
   900
lemma count_multiset_of_length_filter:
haftmann@39533
   901
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@39533
   902
  by (induct xs) auto
haftmann@34943
   903
haftmann@34943
   904
lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls"
haftmann@34943
   905
apply (induct ls arbitrary: i)
haftmann@34943
   906
 apply simp
haftmann@34943
   907
apply (case_tac i)
haftmann@34943
   908
 apply auto
haftmann@34943
   909
done
haftmann@34943
   910
nipkow@36903
   911
lemma multiset_of_remove1[simp]:
nipkow@36903
   912
  "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
nipkow@39302
   913
by (induct xs) (auto simp add: multiset_eq_iff)
haftmann@34943
   914
haftmann@34943
   915
lemma multiset_of_eq_length:
haftmann@37107
   916
  assumes "multiset_of xs = multiset_of ys"
haftmann@37107
   917
  shows "length xs = length ys"
huffman@48012
   918
  using assms by (metis size_multiset_of)
haftmann@34943
   919
haftmann@39533
   920
lemma multiset_of_eq_length_filter:
haftmann@39533
   921
  assumes "multiset_of xs = multiset_of ys"
haftmann@39533
   922
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
huffman@48012
   923
  using assms by (metis count_multiset_of)
haftmann@39533
   924
haftmann@45989
   925
lemma fold_multiset_equiv:
haftmann@45989
   926
  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
   927
    and equiv: "multiset_of xs = multiset_of ys"
haftmann@49822
   928
  shows "List.fold f xs = List.fold f ys"
wenzelm@46921
   929
using f equiv [symmetric]
wenzelm@46921
   930
proof (induct xs arbitrary: ys)
haftmann@45989
   931
  case Nil then show ?case by simp
haftmann@45989
   932
next
haftmann@45989
   933
  case (Cons x xs)
haftmann@45989
   934
  then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD)
haftmann@45989
   935
  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
   936
    by (rule Cons.prems(1)) (simp_all add: *)
haftmann@45989
   937
  moreover from * have "x \<in> set ys" by simp
haftmann@49822
   938
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
haftmann@49822
   939
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
haftmann@45989
   940
  ultimately show ?case by simp
haftmann@45989
   941
qed
haftmann@45989
   942
haftmann@39533
   943
context linorder
haftmann@39533
   944
begin
haftmann@39533
   945
haftmann@40210
   946
lemma multiset_of_insort [simp]:
haftmann@39533
   947
  "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
haftmann@37107
   948
  by (induct xs) (simp_all add: ac_simps)
haftmann@39533
   949
 
haftmann@40210
   950
lemma multiset_of_sort [simp]:
haftmann@39533
   951
  "multiset_of (sort_key k xs) = multiset_of xs"
haftmann@37107
   952
  by (induct xs) (simp_all add: ac_simps)
haftmann@37107
   953
haftmann@34943
   954
text {*
haftmann@34943
   955
  This lemma shows which properties suffice to show that a function
haftmann@34943
   956
  @{text "f"} with @{text "f xs = ys"} behaves like sort.
haftmann@34943
   957
*}
haftmann@37074
   958
haftmann@39533
   959
lemma properties_for_sort_key:
haftmann@39533
   960
  assumes "multiset_of ys = multiset_of xs"
haftmann@40305
   961
  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
   962
  and "sorted (map f ys)"
haftmann@39533
   963
  shows "sort_key f xs = ys"
wenzelm@46921
   964
using assms
wenzelm@46921
   965
proof (induct xs arbitrary: ys)
haftmann@34943
   966
  case Nil then show ?case by simp
haftmann@34943
   967
next
haftmann@34943
   968
  case (Cons x xs)
haftmann@39533
   969
  from Cons.prems(2) have
haftmann@40305
   970
    "\<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
   971
    by (simp add: filter_remove1)
haftmann@39533
   972
  with Cons.prems have "sort_key f xs = remove1 x ys"
haftmann@39533
   973
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
haftmann@39533
   974
  moreover from Cons.prems have "x \<in> set ys"
haftmann@39533
   975
    by (auto simp add: mem_set_multiset_eq intro!: ccontr)
haftmann@39533
   976
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
haftmann@34943
   977
qed
haftmann@34943
   978
haftmann@39533
   979
lemma properties_for_sort:
haftmann@39533
   980
  assumes multiset: "multiset_of ys = multiset_of xs"
haftmann@39533
   981
  and "sorted ys"
haftmann@39533
   982
  shows "sort xs = ys"
haftmann@39533
   983
proof (rule properties_for_sort_key)
haftmann@39533
   984
  from multiset show "multiset_of ys = multiset_of xs" .
haftmann@39533
   985
  from `sorted ys` show "sorted (map (\<lambda>x. x) ys)" by simp
haftmann@39533
   986
  from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)"
haftmann@39533
   987
    by (rule multiset_of_eq_length_filter)
haftmann@39533
   988
  then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k"
haftmann@39533
   989
    by simp
haftmann@40305
   990
  then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs"
haftmann@39533
   991
    by (simp add: replicate_length_filter)
haftmann@39533
   992
qed
haftmann@39533
   993
haftmann@40303
   994
lemma sort_key_by_quicksort:
haftmann@40303
   995
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
haftmann@40303
   996
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
haftmann@40303
   997
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
haftmann@40303
   998
proof (rule properties_for_sort_key)
haftmann@40303
   999
  show "multiset_of ?rhs = multiset_of ?lhs"
haftmann@40303
  1000
    by (rule multiset_eqI) (auto simp add: multiset_of_filter)
haftmann@40303
  1001
next
haftmann@40303
  1002
  show "sorted (map f ?rhs)"
haftmann@40303
  1003
    by (auto simp add: sorted_append intro: sorted_map_same)
haftmann@40303
  1004
next
haftmann@40305
  1005
  fix l
haftmann@40305
  1006
  assume "l \<in> set ?rhs"
haftmann@40346
  1007
  let ?pivot = "f (xs ! (length xs div 2))"
haftmann@40346
  1008
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
haftmann@40306
  1009
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
haftmann@40305
  1010
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
haftmann@40346
  1011
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
haftmann@40346
  1012
  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
  1013
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
haftmann@40346
  1014
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
haftmann@40346
  1015
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
haftmann@40306
  1016
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
haftmann@40305
  1017
  proof (cases "f l" ?pivot rule: linorder_cases)
wenzelm@46730
  1018
    case less
wenzelm@46730
  1019
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
wenzelm@46730
  1020
    with less show ?thesis
haftmann@40346
  1021
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40305
  1022
  next
haftmann@40306
  1023
    case equal then show ?thesis
haftmann@40346
  1024
      by (simp add: * less_le)
haftmann@40305
  1025
  next
wenzelm@46730
  1026
    case greater
wenzelm@46730
  1027
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
wenzelm@46730
  1028
    with greater show ?thesis
haftmann@40346
  1029
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40306
  1030
  qed
haftmann@40303
  1031
qed
haftmann@40303
  1032
haftmann@40303
  1033
lemma sort_by_quicksort:
haftmann@40303
  1034
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
haftmann@40303
  1035
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
haftmann@40303
  1036
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
haftmann@40303
  1037
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
haftmann@40303
  1038
haftmann@40347
  1039
text {* A stable parametrized quicksort *}
haftmann@40347
  1040
haftmann@40347
  1041
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
haftmann@40347
  1042
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
haftmann@40347
  1043
haftmann@40347
  1044
lemma part_code [code]:
haftmann@40347
  1045
  "part f pivot [] = ([], [], [])"
haftmann@40347
  1046
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
haftmann@40347
  1047
     if x' < pivot then (x # lts, eqs, gts)
haftmann@40347
  1048
     else if x' > pivot then (lts, eqs, x # gts)
haftmann@40347
  1049
     else (lts, x # eqs, gts))"
haftmann@40347
  1050
  by (auto simp add: part_def Let_def split_def)
haftmann@40347
  1051
haftmann@40347
  1052
lemma sort_key_by_quicksort_code [code]:
haftmann@40347
  1053
  "sort_key f xs = (case xs of [] \<Rightarrow> []
haftmann@40347
  1054
    | [x] \<Rightarrow> xs
haftmann@40347
  1055
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
haftmann@40347
  1056
    | _ \<Rightarrow> (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1057
       in sort_key f lts @ eqs @ sort_key f gts))"
haftmann@40347
  1058
proof (cases xs)
haftmann@40347
  1059
  case Nil then show ?thesis by simp
haftmann@40347
  1060
next
wenzelm@46921
  1061
  case (Cons _ ys) note hyps = Cons show ?thesis
wenzelm@46921
  1062
  proof (cases ys)
haftmann@40347
  1063
    case Nil with hyps show ?thesis by simp
haftmann@40347
  1064
  next
wenzelm@46921
  1065
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
wenzelm@46921
  1066
    proof (cases zs)
haftmann@40347
  1067
      case Nil with hyps show ?thesis by auto
haftmann@40347
  1068
    next
haftmann@40347
  1069
      case Cons 
haftmann@40347
  1070
      from sort_key_by_quicksort [of f xs]
haftmann@40347
  1071
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1072
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1073
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
haftmann@40347
  1074
      with hyps Cons show ?thesis by (simp only: list.cases)
haftmann@40347
  1075
    qed
haftmann@40347
  1076
  qed
haftmann@40347
  1077
qed
haftmann@40347
  1078
haftmann@39533
  1079
end
haftmann@39533
  1080
haftmann@40347
  1081
hide_const (open) part
haftmann@40347
  1082
haftmann@35268
  1083
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs"
haftmann@35268
  1084
  by (induct xs) (auto intro: order_trans)
haftmann@34943
  1085
haftmann@34943
  1086
lemma multiset_of_update:
haftmann@34943
  1087
  "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
haftmann@34943
  1088
proof (induct ls arbitrary: i)
haftmann@34943
  1089
  case Nil then show ?case by simp
haftmann@34943
  1090
next
haftmann@34943
  1091
  case (Cons x xs)
haftmann@34943
  1092
  show ?case
haftmann@34943
  1093
  proof (cases i)
haftmann@34943
  1094
    case 0 then show ?thesis by simp
haftmann@34943
  1095
  next
haftmann@34943
  1096
    case (Suc i')
haftmann@34943
  1097
    with Cons show ?thesis
haftmann@34943
  1098
      apply simp
haftmann@34943
  1099
      apply (subst add_assoc)
haftmann@34943
  1100
      apply (subst add_commute [of "{#v#}" "{#x#}"])
haftmann@34943
  1101
      apply (subst add_assoc [symmetric])
haftmann@34943
  1102
      apply simp
haftmann@34943
  1103
      apply (rule mset_le_multiset_union_diff_commute)
haftmann@34943
  1104
      apply (simp add: mset_le_single nth_mem_multiset_of)
haftmann@34943
  1105
      done
haftmann@34943
  1106
  qed
haftmann@34943
  1107
qed
haftmann@34943
  1108
haftmann@34943
  1109
lemma multiset_of_swap:
haftmann@34943
  1110
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
haftmann@34943
  1111
    multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
haftmann@34943
  1112
  by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)
haftmann@34943
  1113
haftmann@34943
  1114
bulwahn@46168
  1115
subsubsection {* Association lists -- including code generation *}
bulwahn@46168
  1116
bulwahn@46168
  1117
text {* Preliminaries *}
bulwahn@46168
  1118
bulwahn@46168
  1119
text {* Raw operations on lists *}
bulwahn@46168
  1120
bulwahn@46168
  1121
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
  1122
where
bulwahn@46168
  1123
  "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (%v'. f k (v', v))) ys xs"
bulwahn@46168
  1124
bulwahn@46168
  1125
lemma join_raw_Nil [simp]:
bulwahn@46168
  1126
  "join_raw f xs [] = xs"
bulwahn@46168
  1127
by (simp add: join_raw_def)
bulwahn@46168
  1128
bulwahn@46168
  1129
lemma join_raw_Cons [simp]:
bulwahn@46168
  1130
  "join_raw f xs ((k, v) # ys) = map_default k v (%v'. f k (v', v)) (join_raw f xs ys)"
bulwahn@46168
  1131
by (simp add: join_raw_def)
bulwahn@46168
  1132
bulwahn@46168
  1133
lemma map_of_join_raw:
bulwahn@46168
  1134
  assumes "distinct (map fst ys)"
bulwahn@47429
  1135
  shows "map_of (join_raw f xs ys) x = (case map_of xs x of None => map_of ys x | Some v =>
bulwahn@47429
  1136
    (case map_of ys x of None => Some v | Some v' => Some (f x (v, v'))))"
bulwahn@46168
  1137
using assms
bulwahn@46168
  1138
apply (induct ys)
bulwahn@46168
  1139
apply (auto simp add: map_of_map_default split: option.split)
bulwahn@46168
  1140
apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI)
bulwahn@46168
  1141
by (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2))
bulwahn@46168
  1142
bulwahn@46168
  1143
lemma distinct_join_raw:
bulwahn@46168
  1144
  assumes "distinct (map fst xs)"
bulwahn@46168
  1145
  shows "distinct (map fst (join_raw f xs ys))"
bulwahn@46168
  1146
using assms
bulwahn@46168
  1147
proof (induct ys)
bulwahn@46168
  1148
  case (Cons y ys)
bulwahn@46168
  1149
  thus ?case by (cases y) (simp add: distinct_map_default)
bulwahn@46168
  1150
qed auto
bulwahn@46168
  1151
bulwahn@46168
  1152
definition
bulwahn@46238
  1153
  "subtract_entries_raw xs ys = foldr (%(k, v). AList.map_entry k (%v'. v' - v)) ys xs"
bulwahn@46168
  1154
bulwahn@46168
  1155
lemma map_of_subtract_entries_raw:
bulwahn@47429
  1156
  assumes "distinct (map fst ys)"
bulwahn@47429
  1157
  shows "map_of (subtract_entries_raw xs ys) x = (case map_of xs x of None => None | Some v =>
bulwahn@47429
  1158
    (case map_of ys x of None => Some v | Some v' => Some (v - v')))"
bulwahn@47429
  1159
using assms unfolding subtract_entries_raw_def
bulwahn@46168
  1160
apply (induct ys)
bulwahn@46168
  1161
apply auto
bulwahn@46168
  1162
apply (simp split: option.split)
bulwahn@46168
  1163
apply (simp add: map_of_map_entry)
bulwahn@46168
  1164
apply (auto split: option.split)
bulwahn@46168
  1165
apply (metis map_of_eq_None_iff option.simps(3) option.simps(4))
bulwahn@46168
  1166
by (metis map_of_eq_None_iff option.simps(4) option.simps(5))
bulwahn@46168
  1167
bulwahn@46168
  1168
lemma distinct_subtract_entries_raw:
bulwahn@46168
  1169
  assumes "distinct (map fst xs)"
bulwahn@46168
  1170
  shows "distinct (map fst (subtract_entries_raw xs ys))"
bulwahn@46168
  1171
using assms
bulwahn@46168
  1172
unfolding subtract_entries_raw_def by (induct ys) (auto simp add: distinct_map_entry)
bulwahn@46168
  1173
bulwahn@47179
  1174
text {* Operations on alists with distinct keys *}
bulwahn@46168
  1175
kuncar@47308
  1176
lift_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist" 
kuncar@47308
  1177
is join_raw
bulwahn@47179
  1178
by (simp add: distinct_join_raw)
bulwahn@46168
  1179
kuncar@47308
  1180
lift_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
kuncar@47308
  1181
is subtract_entries_raw 
bulwahn@47179
  1182
by (simp add: distinct_subtract_entries_raw)
bulwahn@46168
  1183
bulwahn@46168
  1184
text {* Implementing multisets by means of association lists *}
haftmann@34943
  1185
haftmann@34943
  1186
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" where
haftmann@34943
  1187
  "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
haftmann@34943
  1188
haftmann@34943
  1189
lemma count_of_multiset:
haftmann@34943
  1190
  "count_of xs \<in> multiset"
haftmann@34943
  1191
proof -
haftmann@34943
  1192
  let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)}"
haftmann@34943
  1193
  have "?A \<subseteq> dom (map_of xs)"
haftmann@34943
  1194
  proof
haftmann@34943
  1195
    fix x
haftmann@34943
  1196
    assume "x \<in> ?A"
haftmann@34943
  1197
    then have "0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)" by simp
haftmann@34943
  1198
    then have "map_of xs x \<noteq> None" by (cases "map_of xs x") auto
haftmann@34943
  1199
    then show "x \<in> dom (map_of xs)" by auto
haftmann@34943
  1200
  qed
haftmann@34943
  1201
  with finite_dom_map_of [of xs] have "finite ?A"
haftmann@34943
  1202
    by (auto intro: finite_subset)
haftmann@34943
  1203
  then show ?thesis
nipkow@39302
  1204
    by (simp add: count_of_def fun_eq_iff multiset_def)
haftmann@34943
  1205
qed
haftmann@34943
  1206
haftmann@34943
  1207
lemma count_simps [simp]:
haftmann@34943
  1208
  "count_of [] = (\<lambda>_. 0)"
haftmann@34943
  1209
  "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
nipkow@39302
  1210
  by (simp_all add: count_of_def fun_eq_iff)
haftmann@34943
  1211
haftmann@34943
  1212
lemma count_of_empty:
haftmann@34943
  1213
  "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
haftmann@34943
  1214
  by (induct xs) (simp_all add: count_of_def)
haftmann@34943
  1215
haftmann@34943
  1216
lemma count_of_filter:
bulwahn@46168
  1217
  "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
haftmann@34943
  1218
  by (induct xs) auto
haftmann@34943
  1219
bulwahn@46168
  1220
lemma count_of_map_default [simp]:
bulwahn@46168
  1221
  "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
  1222
unfolding count_of_def by (simp add: map_of_map_default split: option.split)
bulwahn@46168
  1223
bulwahn@46168
  1224
lemma count_of_join_raw:
bulwahn@46168
  1225
  "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
  1226
unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
bulwahn@46168
  1227
bulwahn@46168
  1228
lemma count_of_subtract_entries_raw:
bulwahn@46168
  1229
  "distinct (map fst ys) ==> count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x"
bulwahn@46168
  1230
unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
bulwahn@46168
  1231
bulwahn@46168
  1232
text {* Code equations for multiset operations *}
bulwahn@46168
  1233
bulwahn@46168
  1234
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset" where
bulwahn@46237
  1235
  "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
haftmann@34943
  1236
haftmann@34943
  1237
code_datatype Bag
haftmann@34943
  1238
haftmann@34943
  1239
lemma count_Bag [simp, code]:
bulwahn@46237
  1240
  "count (Bag xs) = count_of (DAList.impl_of xs)"
haftmann@34943
  1241
  by (simp add: Bag_def count_of_multiset Abs_multiset_inverse)
haftmann@34943
  1242
haftmann@34943
  1243
lemma Mempty_Bag [code]:
bulwahn@46394
  1244
  "{#} = Bag (DAList.empty)"
bulwahn@46394
  1245
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
bulwahn@47143
  1246
haftmann@34943
  1247
lemma single_Bag [code]:
bulwahn@46394
  1248
  "{#x#} = Bag (DAList.update x 1 DAList.empty)"
kuncar@47198
  1249
  by (simp add: multiset_eq_iff alist.Alist_inverse update.rep_eq empty.rep_eq)
bulwahn@46168
  1250
bulwahn@46168
  1251
lemma union_Bag [code]:
bulwahn@46168
  1252
  "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)"
bulwahn@46168
  1253
by (rule multiset_eqI) (simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def)
bulwahn@46168
  1254
bulwahn@46168
  1255
lemma minus_Bag [code]:
bulwahn@46168
  1256
  "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
bulwahn@46168
  1257
by (rule multiset_eqI)
bulwahn@46168
  1258
  (simp add: count_of_subtract_entries_raw alist.Alist_inverse distinct_subtract_entries_raw subtract_entries_def)
haftmann@34943
  1259
haftmann@41069
  1260
lemma filter_Bag [code]:
bulwahn@46237
  1261
  "Multiset.filter P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
bulwahn@47429
  1262
by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq)
haftmann@34943
  1263
haftmann@34943
  1264
lemma mset_less_eq_Bag [code]:
bulwahn@46237
  1265
  "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
  1266
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@34943
  1267
proof
haftmann@34943
  1268
  assume ?lhs then show ?rhs
wenzelm@46730
  1269
    by (auto simp add: mset_le_def)
haftmann@34943
  1270
next
haftmann@34943
  1271
  assume ?rhs
haftmann@34943
  1272
  show ?lhs
haftmann@34943
  1273
  proof (rule mset_less_eqI)
haftmann@34943
  1274
    fix x
bulwahn@46237
  1275
    from `?rhs` have "count_of (DAList.impl_of xs) x \<le> count A x"
bulwahn@46237
  1276
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
haftmann@34943
  1277
    then show "count (Bag xs) x \<le> count A x"
wenzelm@46730
  1278
      by (simp add: mset_le_def)
haftmann@34943
  1279
  qed
haftmann@34943
  1280
qed
haftmann@34943
  1281
haftmann@38857
  1282
instantiation multiset :: (equal) equal
haftmann@34943
  1283
begin
haftmann@34943
  1284
haftmann@34943
  1285
definition
bulwahn@45866
  1286
  [code]: "HOL.equal A B \<longleftrightarrow> (A::'a multiset) \<le> B \<and> B \<le> A"
haftmann@34943
  1287
wenzelm@46921
  1288
instance
wenzelm@46921
  1289
  by default (simp add: equal_multiset_def eq_iff)
haftmann@34943
  1290
haftmann@34943
  1291
end
haftmann@34943
  1292
bulwahn@46168
  1293
text {* Quickcheck generators *}
haftmann@38857
  1294
haftmann@34943
  1295
definition (in term_syntax)
bulwahn@46168
  1296
  bagify :: "('a\<Colon>typerep, nat) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)
haftmann@34943
  1297
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
haftmann@34943
  1298
  [code_unfold]: "bagify xs = Code_Evaluation.valtermify Bag {\<cdot>} xs"
haftmann@34943
  1299
haftmann@37751
  1300
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1301
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@34943
  1302
haftmann@34943
  1303
instantiation multiset :: (random) random
haftmann@34943
  1304
begin
haftmann@34943
  1305
haftmann@34943
  1306
definition
haftmann@37751
  1307
  "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (bagify xs))"
haftmann@34943
  1308
haftmann@34943
  1309
instance ..
haftmann@34943
  1310
haftmann@34943
  1311
end
haftmann@34943
  1312
haftmann@37751
  1313
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
  1314
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@34943
  1315
bulwahn@46168
  1316
instantiation multiset :: (exhaustive) exhaustive
bulwahn@46168
  1317
begin
bulwahn@46168
  1318
bulwahn@46168
  1319
definition exhaustive_multiset :: "('a multiset => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@46168
  1320
where
bulwahn@46168
  1321
  "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (%xs. f (Bag xs)) i"
bulwahn@46168
  1322
bulwahn@46168
  1323
instance ..
bulwahn@46168
  1324
bulwahn@46168
  1325
end
bulwahn@46168
  1326
bulwahn@46168
  1327
instantiation multiset :: (full_exhaustive) full_exhaustive
bulwahn@46168
  1328
begin
bulwahn@46168
  1329
bulwahn@46168
  1330
definition full_exhaustive_multiset :: "('a multiset * (unit => term) => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@46168
  1331
where
bulwahn@46168
  1332
  "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (%xs. f (bagify xs)) i"
bulwahn@46168
  1333
bulwahn@46168
  1334
instance ..
bulwahn@46168
  1335
bulwahn@46168
  1336
end
bulwahn@46168
  1337
wenzelm@36176
  1338
hide_const (open) bagify
haftmann@34943
  1339
haftmann@34943
  1340
haftmann@34943
  1341
subsection {* The multiset order *}
wenzelm@10249
  1342
wenzelm@10249
  1343
subsubsection {* Well-foundedness *}
wenzelm@10249
  1344
haftmann@28708
  1345
definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1346
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
berghofe@23751
  1347
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
wenzelm@10249
  1348
haftmann@28708
  1349
definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1350
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
  1351
berghofe@23751
  1352
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
nipkow@26178
  1353
by (simp add: mult1_def)
wenzelm@10249
  1354
berghofe@23751
  1355
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
berghofe@23751
  1356
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
berghofe@23751
  1357
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@19582
  1358
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
wenzelm@10249
  1359
proof (unfold mult1_def)
berghofe@23751
  1360
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
nipkow@11464
  1361
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
berghofe@23751
  1362
  let ?case1 = "?case1 {(N, M). ?R N M}"
wenzelm@10249
  1363
berghofe@23751
  1364
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
wenzelm@18258
  1365
  then have "\<exists>a' M0' K.
nipkow@11464
  1366
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
wenzelm@18258
  1367
  then show "?case1 \<or> ?case2"
wenzelm@10249
  1368
  proof (elim exE conjE)
wenzelm@10249
  1369
    fix a' M0' K
wenzelm@10249
  1370
    assume N: "N = M0' + K" and r: "?r K a'"
wenzelm@10249
  1371
    assume "M0 + {#a#} = M0' + {#a'#}"
wenzelm@18258
  1372
    then have "M0 = M0' \<and> a = a' \<or>
nipkow@11464
  1373
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
wenzelm@10249
  1374
      by (simp only: add_eq_conv_ex)
wenzelm@18258
  1375
    then show ?thesis
wenzelm@10249
  1376
    proof (elim disjE conjE exE)
wenzelm@10249
  1377
      assume "M0 = M0'" "a = a'"
nipkow@11464
  1378
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@18258
  1379
      then have ?case2 .. then show ?thesis ..
wenzelm@10249
  1380
    next
wenzelm@10249
  1381
      fix K'
wenzelm@10249
  1382
      assume "M0' = K' + {#a#}"
haftmann@34943
  1383
      with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
wenzelm@10249
  1384
wenzelm@10249
  1385
      assume "M0 = K' + {#a'#}"
wenzelm@10249
  1386
      with r have "?R (K' + K) M0" by blast
wenzelm@18258
  1387
      with n have ?case1 by simp then show ?thesis ..
wenzelm@10249
  1388
    qed
wenzelm@10249
  1389
  qed
wenzelm@10249
  1390
qed
wenzelm@10249
  1391
berghofe@23751
  1392
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
wenzelm@10249
  1393
proof
wenzelm@10249
  1394
  let ?R = "mult1 r"
wenzelm@10249
  1395
  let ?W = "acc ?R"
wenzelm@10249
  1396
  {
wenzelm@10249
  1397
    fix M M0 a
berghofe@23751
  1398
    assume M0: "M0 \<in> ?W"
berghofe@23751
  1399
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1400
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
berghofe@23751
  1401
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
  1402
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
  1403
      fix N
berghofe@23751
  1404
      assume "(N, M0 + {#a#}) \<in> ?R"
berghofe@23751
  1405
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
berghofe@23751
  1406
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
wenzelm@10249
  1407
        by (rule less_add)
berghofe@23751
  1408
      then show "N \<in> ?W"
wenzelm@10249
  1409
      proof (elim exE disjE conjE)
berghofe@23751
  1410
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
berghofe@23751
  1411
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
berghofe@23751
  1412
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
berghofe@23751
  1413
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
  1414
      next
wenzelm@10249
  1415
        fix K
wenzelm@10249
  1416
        assume N: "N = M0 + K"
berghofe@23751
  1417
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
berghofe@23751
  1418
        then have "M0 + K \<in> ?W"
wenzelm@10249
  1419
        proof (induct K)
wenzelm@18730
  1420
          case empty
berghofe@23751
  1421
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
  1422
        next
wenzelm@18730
  1423
          case (add K x)
berghofe@23751
  1424
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
  1425
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
  1426
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
  1427
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
haftmann@34943
  1428
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
wenzelm@10249
  1429
        qed
berghofe@23751
  1430
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
  1431
      qed
wenzelm@10249
  1432
    qed
wenzelm@10249
  1433
  } note tedious_reasoning = this
wenzelm@10249
  1434
berghofe@23751
  1435
  assume wf: "wf r"
wenzelm@10249
  1436
  fix M
berghofe@23751
  1437
  show "M \<in> ?W"
wenzelm@10249
  1438
  proof (induct M)
berghofe@23751
  1439
    show "{#} \<in> ?W"
wenzelm@10249
  1440
    proof (rule accI)
berghofe@23751
  1441
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
  1442
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
  1443
    qed
wenzelm@10249
  1444
berghofe@23751
  1445
    fix M a assume "M \<in> ?W"
berghofe@23751
  1446
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1447
    proof induct
wenzelm@10249
  1448
      fix a
berghofe@23751
  1449
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1450
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1451
      proof
berghofe@23751
  1452
        fix M assume "M \<in> ?W"
berghofe@23751
  1453
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
  1454
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
  1455
      qed
wenzelm@10249
  1456
    qed
berghofe@23751
  1457
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
wenzelm@10249
  1458
  qed
wenzelm@10249
  1459
qed
wenzelm@10249
  1460
berghofe@23751
  1461
theorem wf_mult1: "wf r ==> wf (mult1 r)"
nipkow@26178
  1462
by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
  1463
berghofe@23751
  1464
theorem wf_mult: "wf r ==> wf (mult r)"
nipkow@26178
  1465
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
  1466
wenzelm@10249
  1467
wenzelm@10249
  1468
subsubsection {* Closure-free presentation *}
wenzelm@10249
  1469
wenzelm@10249
  1470
text {* One direction. *}
wenzelm@10249
  1471
wenzelm@10249
  1472
lemma mult_implies_one_step:
berghofe@23751
  1473
  "trans r ==> (M, N) \<in> mult r ==>
nipkow@11464
  1474
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
berghofe@23751
  1475
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
nipkow@26178
  1476
apply (unfold mult_def mult1_def set_of_def)
nipkow@26178
  1477
apply (erule converse_trancl_induct, clarify)
nipkow@26178
  1478
 apply (rule_tac x = M0 in exI, simp, clarify)
nipkow@26178
  1479
apply (case_tac "a :# K")
nipkow@26178
  1480
 apply (rule_tac x = I in exI)
nipkow@26178
  1481
 apply (simp (no_asm))
nipkow@26178
  1482
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
haftmann@34943
  1483
 apply (simp (no_asm_simp) add: add_assoc [symmetric])
nipkow@26178
  1484
 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
nipkow@26178
  1485
 apply (simp add: diff_union_single_conv)
nipkow@26178
  1486
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1487
 apply blast
nipkow@26178
  1488
apply (subgoal_tac "a :# I")
nipkow@26178
  1489
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow@26178
  1490
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow@26178
  1491
 apply (rule_tac x = "K + Ka" in exI)
nipkow@26178
  1492
 apply (rule conjI)
nipkow@39302
  1493
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1494
 apply (rule conjI)
nipkow@26178
  1495
  apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
nipkow@39302
  1496
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1497
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1498
 apply blast
nipkow@26178
  1499
apply (subgoal_tac "a :# (M0 + {#a#})")
nipkow@26178
  1500
 apply simp
nipkow@26178
  1501
apply (simp (no_asm))
nipkow@26178
  1502
done
wenzelm@10249
  1503
wenzelm@10249
  1504
lemma one_step_implies_mult_aux:
berghofe@23751
  1505
  "trans r ==>
berghofe@23751
  1506
    \<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
  1507
      --> (I + K, I + J) \<in> mult r"
nipkow@26178
  1508
apply (induct_tac n, auto)
nipkow@26178
  1509
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow@26178
  1510
apply (rename_tac "J'", simp)
nipkow@26178
  1511
apply (erule notE, auto)
nipkow@26178
  1512
apply (case_tac "J' = {#}")
nipkow@26178
  1513
 apply (simp add: mult_def)
nipkow@26178
  1514
 apply (rule r_into_trancl)
nipkow@26178
  1515
 apply (simp add: mult1_def set_of_def, blast)
nipkow@26178
  1516
txt {* Now we know @{term "J' \<noteq> {#}"}. *}
nipkow@26178
  1517
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@26178
  1518
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
nipkow@26178
  1519
apply (erule ssubst)
nipkow@26178
  1520
apply (simp add: Ball_def, auto)
nipkow@26178
  1521
apply (subgoal_tac
nipkow@26178
  1522
  "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
nipkow@26178
  1523
    (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow@26178
  1524
 prefer 2
nipkow@26178
  1525
 apply force
haftmann@34943
  1526
apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
nipkow@26178
  1527
apply (erule trancl_trans)
nipkow@26178
  1528
apply (rule r_into_trancl)
nipkow@26178
  1529
apply (simp add: mult1_def set_of_def)
nipkow@26178
  1530
apply (rule_tac x = a in exI)
nipkow@26178
  1531
apply (rule_tac x = "I + J'" in exI)
haftmann@34943
  1532
apply (simp add: add_ac)
nipkow@26178
  1533
done
wenzelm@10249
  1534
wenzelm@17161
  1535
lemma one_step_implies_mult:
berghofe@23751
  1536
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
berghofe@23751
  1537
    ==> (I + K, I + J) \<in> mult r"
nipkow@26178
  1538
using one_step_implies_mult_aux by blast
wenzelm@10249
  1539
wenzelm@10249
  1540
wenzelm@10249
  1541
subsubsection {* Partial-order properties *}
wenzelm@10249
  1542
haftmann@35273
  1543
definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
haftmann@35273
  1544
  "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
wenzelm@10249
  1545
haftmann@35273
  1546
definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
haftmann@35273
  1547
  "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
haftmann@35273
  1548
haftmann@35308
  1549
notation (xsymbols) less_multiset (infix "\<subset>#" 50)
haftmann@35308
  1550
notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
wenzelm@10249
  1551
haftmann@35268
  1552
interpretation multiset_order: order le_multiset less_multiset
haftmann@35268
  1553
proof -
haftmann@35268
  1554
  have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
haftmann@35268
  1555
  proof
haftmann@35268
  1556
    fix M :: "'a multiset"
haftmann@35268
  1557
    assume "M \<subset># M"
haftmann@35268
  1558
    then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
haftmann@35268
  1559
    have "trans {(x'::'a, x). x' < x}"
haftmann@35268
  1560
      by (rule transI) simp
haftmann@35268
  1561
    moreover note MM
haftmann@35268
  1562
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
haftmann@35268
  1563
      \<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
  1564
      by (rule mult_implies_one_step)
haftmann@35268
  1565
    then obtain I J K where "M = I + J" and "M = I + K"
haftmann@35268
  1566
      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
  1567
    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
  1568
    have "finite (set_of K)" by simp
haftmann@35268
  1569
    moreover note aux2
haftmann@35268
  1570
    ultimately have "set_of K = {}"
haftmann@35268
  1571
      by (induct rule: finite_induct) (auto intro: order_less_trans)
haftmann@35268
  1572
    with aux1 show False by simp
haftmann@35268
  1573
  qed
haftmann@35268
  1574
  have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N"
haftmann@35268
  1575
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
wenzelm@46921
  1576
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
wenzelm@46921
  1577
    by default (auto simp add: le_multiset_def irrefl dest: trans)
haftmann@35268
  1578
qed
wenzelm@10249
  1579
wenzelm@46730
  1580
lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R"
wenzelm@46730
  1581
  by simp
haftmann@26567
  1582
wenzelm@10249
  1583
wenzelm@10249
  1584
subsubsection {* Monotonicity of multiset union *}
wenzelm@10249
  1585
wenzelm@46730
  1586
lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
nipkow@26178
  1587
apply (unfold mult1_def)
nipkow@26178
  1588
apply auto
nipkow@26178
  1589
apply (rule_tac x = a in exI)
nipkow@26178
  1590
apply (rule_tac x = "C + M0" in exI)
haftmann@34943
  1591
apply (simp add: add_assoc)
nipkow@26178
  1592
done
wenzelm@10249
  1593
haftmann@35268
  1594
lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
nipkow@26178
  1595
apply (unfold less_multiset_def mult_def)
nipkow@26178
  1596
apply (erule trancl_induct)
noschinl@40249
  1597
 apply (blast intro: mult1_union)
noschinl@40249
  1598
apply (blast intro: mult1_union trancl_trans)
nipkow@26178
  1599
done
wenzelm@10249
  1600
haftmann@35268
  1601
lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order multiset)"
haftmann@34943
  1602
apply (subst add_commute [of B C])
haftmann@34943
  1603
apply (subst add_commute [of D C])
nipkow@26178
  1604
apply (erule union_less_mono2)
nipkow@26178
  1605
done
wenzelm@10249
  1606
wenzelm@17161
  1607
lemma union_less_mono:
haftmann@35268
  1608
  "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)"
haftmann@35268
  1609
  by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
wenzelm@10249
  1610
haftmann@35268
  1611
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
haftmann@35268
  1612
proof
haftmann@35268
  1613
qed (auto simp add: le_multiset_def intro: union_less_mono2)
wenzelm@26145
  1614
paulson@15072
  1615
krauss@29125
  1616
subsection {* Termination proofs with multiset orders *}
krauss@29125
  1617
krauss@29125
  1618
lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
krauss@29125
  1619
  and multi_member_this: "x \<in># {# x #} + XS"
krauss@29125
  1620
  and multi_member_last: "x \<in># {# x #}"
krauss@29125
  1621
  by auto
krauss@29125
  1622
krauss@29125
  1623
definition "ms_strict = mult pair_less"
haftmann@37765
  1624
definition "ms_weak = ms_strict \<union> Id"
krauss@29125
  1625
krauss@29125
  1626
lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
krauss@29125
  1627
unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
krauss@29125
  1628
by (auto intro: wf_mult1 wf_trancl simp: mult_def)
krauss@29125
  1629
krauss@29125
  1630
lemma smsI:
krauss@29125
  1631
  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
krauss@29125
  1632
  unfolding ms_strict_def
krauss@29125
  1633
by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
krauss@29125
  1634
krauss@29125
  1635
lemma wmsI:
krauss@29125
  1636
  "(set_of A, set_of B) \<in> max_strict \<or> A = {#} \<and> B = {#}
krauss@29125
  1637
  \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
krauss@29125
  1638
unfolding ms_weak_def ms_strict_def
krauss@29125
  1639
by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
krauss@29125
  1640
krauss@29125
  1641
inductive pw_leq
krauss@29125
  1642
where
krauss@29125
  1643
  pw_leq_empty: "pw_leq {#} {#}"
krauss@29125
  1644
| pw_leq_step:  "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
krauss@29125
  1645
krauss@29125
  1646
lemma pw_leq_lstep:
krauss@29125
  1647
  "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
krauss@29125
  1648
by (drule pw_leq_step) (rule pw_leq_empty, simp)
krauss@29125
  1649
krauss@29125
  1650
lemma pw_leq_split:
krauss@29125
  1651
  assumes "pw_leq X Y"
krauss@29125
  1652
  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
  1653
  using assms
krauss@29125
  1654
proof (induct)
krauss@29125
  1655
  case pw_leq_empty thus ?case by auto
krauss@29125
  1656
next
krauss@29125
  1657
  case (pw_leq_step x y X Y)
krauss@29125
  1658
  then obtain A B Z where
krauss@29125
  1659
    [simp]: "X = A + Z" "Y = B + Z" 
krauss@29125
  1660
      and 1[simp]: "(set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#})" 
krauss@29125
  1661
    by auto
krauss@29125
  1662
  from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less" 
krauss@29125
  1663
    unfolding pair_leq_def by auto
krauss@29125
  1664
  thus ?case
krauss@29125
  1665
  proof
krauss@29125
  1666
    assume [simp]: "x = y"
krauss@29125
  1667
    have
krauss@29125
  1668
      "{#x#} + X = A + ({#y#}+Z) 
krauss@29125
  1669
      \<and> {#y#} + Y = B + ({#y#}+Z)
krauss@29125
  1670
      \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
krauss@29125
  1671
      by (auto simp: add_ac)
krauss@29125
  1672
    thus ?case by (intro exI)
krauss@29125
  1673
  next
krauss@29125
  1674
    assume A: "(x, y) \<in> pair_less"
krauss@29125
  1675
    let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
krauss@29125
  1676
    have "{#x#} + X = ?A' + Z"
krauss@29125
  1677
      "{#y#} + Y = ?B' + Z"
krauss@29125
  1678
      by (auto simp add: add_ac)
krauss@29125
  1679
    moreover have 
krauss@29125
  1680
      "(set_of ?A', set_of ?B') \<in> max_strict"
krauss@29125
  1681
      using 1 A unfolding max_strict_def 
krauss@29125
  1682
      by (auto elim!: max_ext.cases)
krauss@29125
  1683
    ultimately show ?thesis by blast
krauss@29125
  1684
  qed
krauss@29125
  1685
qed
krauss@29125
  1686
krauss@29125
  1687
lemma 
krauss@29125
  1688
  assumes pwleq: "pw_leq Z Z'"
krauss@29125
  1689
  shows ms_strictI: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
krauss@29125
  1690
  and   ms_weakI1:  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
krauss@29125
  1691
  and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
krauss@29125
  1692
proof -
krauss@29125
  1693
  from pw_leq_split[OF pwleq] 
krauss@29125
  1694
  obtain A' B' Z''
krauss@29125
  1695
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
krauss@29125
  1696
    and mx_or_empty: "(set_of A', set_of B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
krauss@29125
  1697
    by blast
krauss@29125
  1698
  {
krauss@29125
  1699
    assume max: "(set_of A, set_of B) \<in> max_strict"
krauss@29125
  1700
    from mx_or_empty
krauss@29125
  1701
    have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
krauss@29125
  1702
    proof
krauss@29125
  1703
      assume max': "(set_of A', set_of B') \<in> max_strict"
krauss@29125
  1704
      with max have "(set_of (A + A'), set_of (B + B')) \<in> max_strict"
krauss@29125
  1705
        by (auto simp: max_strict_def intro: max_ext_additive)
krauss@29125
  1706
      thus ?thesis by (rule smsI) 
krauss@29125
  1707
    next
krauss@29125
  1708
      assume [simp]: "A' = {#} \<and> B' = {#}"
krauss@29125
  1709
      show ?thesis by (rule smsI) (auto intro: max)
krauss@29125
  1710
    qed
krauss@29125
  1711
    thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add:add_ac)
krauss@29125
  1712
    thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
krauss@29125
  1713
  }
krauss@29125
  1714
  from mx_or_empty
krauss@29125
  1715
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
krauss@29125
  1716
  thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:add_ac)
krauss@29125
  1717
qed
krauss@29125
  1718
nipkow@39301
  1719
lemma empty_neutral: "{#} + x = x" "x + {#} = x"
krauss@29125
  1720
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
krauss@29125
  1721
and nonempty_single: "{# x #} \<noteq> {#}"
krauss@29125
  1722
by auto
krauss@29125
  1723
krauss@29125
  1724
setup {*
krauss@29125
  1725
let
wenzelm@35402
  1726
  fun msetT T = Type (@{type_name multiset}, [T]);
krauss@29125
  1727
wenzelm@35402
  1728
  fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
krauss@29125
  1729
    | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
krauss@29125
  1730
    | mk_mset T (x :: xs) =
krauss@29125
  1731
          Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
krauss@29125
  1732
                mk_mset T [x] $ mk_mset T xs
krauss@29125
  1733
krauss@29125
  1734
  fun mset_member_tac m i =
krauss@29125
  1735
      (if m <= 0 then
krauss@29125
  1736
           rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
krauss@29125
  1737
       else
krauss@29125
  1738
           rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
krauss@29125
  1739
krauss@29125
  1740
  val mset_nonempty_tac =
krauss@29125
  1741
      rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
krauss@29125
  1742
krauss@29125
  1743
  val regroup_munion_conv =
wenzelm@35402
  1744
      Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
nipkow@39301
  1745
        (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
krauss@29125
  1746
krauss@29125
  1747
  fun unfold_pwleq_tac i =
krauss@29125
  1748
    (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
krauss@29125
  1749
      ORELSE (rtac @{thm pw_leq_lstep} i)
krauss@29125
  1750
      ORELSE (rtac @{thm pw_leq_empty} i)
krauss@29125
  1751
krauss@29125
  1752
  val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
krauss@29125
  1753
                      @{thm Un_insert_left}, @{thm Un_empty_left}]
krauss@29125
  1754
in
krauss@29125
  1755
  ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset 
krauss@29125
  1756
  {
krauss@29125
  1757
    msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
krauss@29125
  1758
    mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
krauss@29125
  1759
    mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
wenzelm@30595
  1760
    smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
wenzelm@30595
  1761
    reduction_pair= @{thm ms_reduction_pair}
krauss@29125
  1762
  })
wenzelm@10249
  1763
end
krauss@29125
  1764
*}
krauss@29125
  1765
haftmann@34943
  1766
haftmann@34943
  1767
subsection {* Legacy theorem bindings *}
haftmann@34943
  1768
nipkow@39302
  1769
lemmas multi_count_eq = multiset_eq_iff [symmetric]
haftmann@34943
  1770
haftmann@34943
  1771
lemma union_commute: "M + N = N + (M::'a multiset)"
haftmann@34943
  1772
  by (fact add_commute)
haftmann@34943
  1773
haftmann@34943
  1774
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
haftmann@34943
  1775
  by (fact add_assoc)
haftmann@34943
  1776
haftmann@34943
  1777
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
haftmann@34943
  1778
  by (fact add_left_commute)
haftmann@34943
  1779
haftmann@34943
  1780
lemmas union_ac = union_assoc union_commute union_lcomm
haftmann@34943
  1781
haftmann@34943
  1782
lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1783
  by (fact add_right_cancel)
haftmann@34943
  1784
haftmann@34943
  1785
lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1786
  by (fact add_left_cancel)
haftmann@34943
  1787
haftmann@34943
  1788
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
haftmann@34943
  1789
  by (fact add_imp_eq)
haftmann@34943
  1790
haftmann@35268
  1791
lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N"
haftmann@35268
  1792
  by (fact order_less_trans)
haftmann@35268
  1793
haftmann@35268
  1794
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
haftmann@35268
  1795
  by (fact inf.commute)
haftmann@35268
  1796
haftmann@35268
  1797
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
haftmann@35268
  1798
  by (fact inf.assoc [symmetric])
haftmann@35268
  1799
haftmann@35268
  1800
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
haftmann@35268
  1801
  by (fact inf.left_commute)
haftmann@35268
  1802
haftmann@35268
  1803
lemmas multiset_inter_ac =
haftmann@35268
  1804
  multiset_inter_commute
haftmann@35268
  1805
  multiset_inter_assoc
haftmann@35268
  1806
  multiset_inter_left_commute
haftmann@35268
  1807
haftmann@35268
  1808
lemma mult_less_not_refl:
haftmann@35268
  1809
  "\<not> M \<subset># (M::'a::order multiset)"
haftmann@35268
  1810
  by (fact multiset_order.less_irrefl)
haftmann@35268
  1811
haftmann@35268
  1812
lemma mult_less_trans:
haftmann@35268
  1813
  "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)"
haftmann@35268
  1814
  by (fact multiset_order.less_trans)
haftmann@35268
  1815
    
haftmann@35268
  1816
lemma mult_less_not_sym:
haftmann@35268
  1817
  "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
haftmann@35268
  1818
  by (fact multiset_order.less_not_sym)
haftmann@35268
  1819
haftmann@35268
  1820
lemma mult_less_asym:
haftmann@35268
  1821
  "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P"
haftmann@35268
  1822
  by (fact multiset_order.less_asym)
haftmann@34943
  1823
blanchet@35712
  1824
ML {*
blanchet@35712
  1825
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
blanchet@35712
  1826
                      (Const _ $ t') =
blanchet@35712
  1827
    let
blanchet@35712
  1828
      val (maybe_opt, ps) =
blanchet@35712
  1829
        Nitpick_Model.dest_plain_fun t' ||> op ~~
blanchet@35712
  1830
        ||> map (apsnd (snd o HOLogic.dest_number))
blanchet@35712
  1831
      fun elems_for t =
blanchet@35712
  1832
        case AList.lookup (op =) ps t of
blanchet@35712
  1833
          SOME n => replicate n t
blanchet@35712
  1834
        | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]
blanchet@35712
  1835
    in
blanchet@35712
  1836
      case maps elems_for (all_values elem_T) @
blanchet@37261
  1837
           (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
blanchet@37261
  1838
            else []) of
blanchet@35712
  1839
        [] => Const (@{const_name zero_class.zero}, T)
blanchet@35712
  1840
      | ts => foldl1 (fn (t1, t2) =>
blanchet@35712
  1841
                         Const (@{const_name plus_class.plus}, T --> T --> T)
blanchet@35712
  1842
                         $ t1 $ t2)
blanchet@35712
  1843
                     (map (curry (op $) (Const (@{const_name single},
blanchet@35712
  1844
                                                elem_T --> T))) ts)
blanchet@35712
  1845
    end
blanchet@35712
  1846
  | multiset_postproc _ _ _ _ t = t
blanchet@35712
  1847
*}
blanchet@35712
  1848
blanchet@38287
  1849
declaration {*
blanchet@38287
  1850
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
blanchet@38242
  1851
    multiset_postproc
blanchet@35712
  1852
*}
blanchet@35712
  1853
haftmann@49822
  1854
hide_const (open) fold
haftmann@49822
  1855
blanchet@37169
  1856
end
haftmann@49388
  1857