src/HOL/Library/Multiset.thy
author wenzelm
Sat Nov 04 18:41:37 2000 +0100 (2000-11-04)
changeset 10392 f27afee8475d
parent 10313 51e830bb7abe
child 10714 07f75bf77a33
permissions -rw-r--r--
tuned;
wenzelm@10249
     1
(*  Title:      HOL/Library/Multiset.thy
wenzelm@10249
     2
    ID:         $Id$
wenzelm@10249
     3
    Author:     Tobias Nipkow, TU Muenchen
wenzelm@10249
     4
    Author:     Markus Wenzel, TU Muenchen
wenzelm@10249
     5
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@10249
     6
*)
wenzelm@10249
     7
wenzelm@10249
     8
header {*
wenzelm@10249
     9
 \title{Multisets}
wenzelm@10249
    10
 \author{Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson}
wenzelm@10249
    11
*}
wenzelm@10249
    12
wenzelm@10249
    13
theory Multiset = Accessible_Part:
wenzelm@10249
    14
wenzelm@10249
    15
subsection {* The type of multisets *}
wenzelm@10249
    16
wenzelm@10249
    17
typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}"
wenzelm@10249
    18
proof
wenzelm@10277
    19
  show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
wenzelm@10249
    20
qed
wenzelm@10249
    21
wenzelm@10249
    22
lemmas multiset_typedef [simp] =
wenzelm@10277
    23
    Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
wenzelm@10277
    24
  and [simp] = Rep_multiset_inject [symmetric]
wenzelm@10249
    25
wenzelm@10249
    26
constdefs
wenzelm@10249
    27
  Mempty :: "'a multiset"    ("{#}")
wenzelm@10249
    28
  "{#} == Abs_multiset (\<lambda>a. 0)"
wenzelm@10249
    29
wenzelm@10249
    30
  single :: "'a => 'a multiset"    ("{#_#}")
wenzelm@10249
    31
  "{#a#} == Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
wenzelm@10249
    32
wenzelm@10249
    33
  count :: "'a multiset => 'a => nat"
wenzelm@10249
    34
  "count == Rep_multiset"
wenzelm@10249
    35
wenzelm@10249
    36
  MCollect :: "'a multiset => ('a => bool) => 'a multiset"
wenzelm@10249
    37
  "MCollect M P == Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
wenzelm@10249
    38
wenzelm@10249
    39
syntax
wenzelm@10249
    40
  "_Melem" :: "'a => 'a multiset => bool"    ("(_/ :# _)" [50, 51] 50)
wenzelm@10249
    41
  "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
wenzelm@10249
    42
translations
wenzelm@10249
    43
  "a :# M" == "0 < count M a"
wenzelm@10249
    44
  "{#x:M. P#}" == "MCollect M (\<lambda>x. P)"
wenzelm@10249
    45
wenzelm@10249
    46
constdefs
wenzelm@10249
    47
  set_of :: "'a multiset => 'a set"
wenzelm@10249
    48
  "set_of M == {x. x :# M}"
wenzelm@10249
    49
wenzelm@10313
    50
instance multiset :: ("term") plus ..
wenzelm@10313
    51
instance multiset :: ("term") minus ..
wenzelm@10313
    52
instance multiset :: ("term") zero ..
wenzelm@10249
    53
wenzelm@10249
    54
defs (overloaded)
wenzelm@10249
    55
  union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)"
wenzelm@10249
    56
  diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
wenzelm@10249
    57
  Zero_def [simp]: "0 == {#}"
wenzelm@10249
    58
  size_def: "size M == setsum (count M) (set_of M)"
wenzelm@10249
    59
wenzelm@10249
    60
wenzelm@10249
    61
text {*
wenzelm@10249
    62
 \medskip Preservation of the representing set @{term multiset}.
wenzelm@10249
    63
*}
wenzelm@10249
    64
wenzelm@10249
    65
lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
wenzelm@10249
    66
  apply (simp add: multiset_def)
wenzelm@10249
    67
  done
wenzelm@10249
    68
wenzelm@10249
    69
lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
wenzelm@10249
    70
  apply (simp add: multiset_def)
wenzelm@10249
    71
  done
wenzelm@10249
    72
wenzelm@10249
    73
lemma union_preserves_multiset [simp]:
wenzelm@10249
    74
    "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
wenzelm@10249
    75
  apply (unfold multiset_def)
wenzelm@10249
    76
  apply simp
wenzelm@10249
    77
  apply (drule finite_UnI)
wenzelm@10249
    78
   apply assumption
wenzelm@10249
    79
  apply (simp del: finite_Un add: Un_def)
wenzelm@10249
    80
  done
wenzelm@10249
    81
wenzelm@10249
    82
lemma diff_preserves_multiset [simp]:
wenzelm@10249
    83
    "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
wenzelm@10249
    84
  apply (unfold multiset_def)
wenzelm@10249
    85
  apply simp
wenzelm@10249
    86
  apply (rule finite_subset)
wenzelm@10249
    87
   prefer 2
wenzelm@10249
    88
   apply assumption
wenzelm@10249
    89
  apply auto
wenzelm@10249
    90
  done
wenzelm@10249
    91
wenzelm@10249
    92
wenzelm@10249
    93
subsection {* Algebraic properties of multisets *}
wenzelm@10249
    94
wenzelm@10249
    95
subsubsection {* Union *}
wenzelm@10249
    96
wenzelm@10249
    97
theorem union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
wenzelm@10249
    98
  apply (simp add: union_def Mempty_def)
wenzelm@10249
    99
  done
wenzelm@10249
   100
wenzelm@10249
   101
theorem union_commute: "M + N = N + (M::'a multiset)"
wenzelm@10249
   102
  apply (simp add: union_def add_ac)
wenzelm@10249
   103
  done
wenzelm@10249
   104
wenzelm@10249
   105
theorem union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
wenzelm@10249
   106
  apply (simp add: union_def add_ac)
wenzelm@10249
   107
  done
wenzelm@10249
   108
wenzelm@10249
   109
theorem union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
wenzelm@10249
   110
  apply (rule union_commute [THEN trans])
wenzelm@10249
   111
  apply (rule union_assoc [THEN trans])
wenzelm@10249
   112
  apply (rule union_commute [THEN arg_cong])
wenzelm@10249
   113
  done
wenzelm@10249
   114
wenzelm@10249
   115
theorems union_ac = union_assoc union_commute union_lcomm
wenzelm@10249
   116
wenzelm@10277
   117
instance multiset :: ("term") plus_ac0
wenzelm@10277
   118
  apply intro_classes
wenzelm@10277
   119
    apply (rule union_commute)
wenzelm@10277
   120
   apply (rule union_assoc)
wenzelm@10277
   121
  apply simp
wenzelm@10277
   122
  done
wenzelm@10277
   123
wenzelm@10249
   124
wenzelm@10249
   125
subsubsection {* Difference *}
wenzelm@10249
   126
wenzelm@10249
   127
theorem diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
wenzelm@10249
   128
  apply (simp add: Mempty_def diff_def)
wenzelm@10249
   129
  done
wenzelm@10249
   130
wenzelm@10249
   131
theorem diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
wenzelm@10249
   132
  apply (simp add: union_def diff_def)
wenzelm@10249
   133
  done
wenzelm@10249
   134
wenzelm@10249
   135
wenzelm@10249
   136
subsubsection {* Count of elements *}
wenzelm@10249
   137
wenzelm@10249
   138
theorem count_empty [simp]: "count {#} a = 0"
wenzelm@10249
   139
  apply (simp add: count_def Mempty_def)
wenzelm@10249
   140
  done
wenzelm@10249
   141
wenzelm@10249
   142
theorem count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
wenzelm@10249
   143
  apply (simp add: count_def single_def)
wenzelm@10249
   144
  done
wenzelm@10249
   145
wenzelm@10249
   146
theorem count_union [simp]: "count (M + N) a = count M a + count N a"
wenzelm@10249
   147
  apply (simp add: count_def union_def)
wenzelm@10249
   148
  done
wenzelm@10249
   149
wenzelm@10249
   150
theorem count_diff [simp]: "count (M - N) a = count M a - count N a"
wenzelm@10249
   151
  apply (simp add: count_def diff_def)
wenzelm@10249
   152
  done
wenzelm@10249
   153
wenzelm@10249
   154
wenzelm@10249
   155
subsubsection {* Set of elements *}
wenzelm@10249
   156
wenzelm@10249
   157
theorem set_of_empty [simp]: "set_of {#} = {}"
wenzelm@10249
   158
  apply (simp add: set_of_def)
wenzelm@10249
   159
  done
wenzelm@10249
   160
wenzelm@10249
   161
theorem set_of_single [simp]: "set_of {#b#} = {b}"
wenzelm@10249
   162
  apply (simp add: set_of_def)
wenzelm@10249
   163
  done
wenzelm@10249
   164
wenzelm@10249
   165
theorem set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
wenzelm@10249
   166
  apply (auto simp add: set_of_def)
wenzelm@10249
   167
  done
wenzelm@10249
   168
wenzelm@10249
   169
theorem set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
wenzelm@10249
   170
  apply (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
wenzelm@10249
   171
  done
wenzelm@10249
   172
wenzelm@10249
   173
theorem mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
wenzelm@10249
   174
  apply (auto simp add: set_of_def)
wenzelm@10249
   175
  done
wenzelm@10249
   176
wenzelm@10249
   177
wenzelm@10249
   178
subsubsection {* Size *}
wenzelm@10249
   179
wenzelm@10249
   180
theorem size_empty [simp]: "size {#} = 0"
wenzelm@10249
   181
  apply (simp add: size_def)
wenzelm@10249
   182
  done
wenzelm@10249
   183
wenzelm@10249
   184
theorem size_single [simp]: "size {#b#} = 1"
wenzelm@10249
   185
  apply (simp add: size_def)
wenzelm@10249
   186
  done
wenzelm@10249
   187
wenzelm@10249
   188
theorem finite_set_of [iff]: "finite (set_of M)"
wenzelm@10249
   189
  apply (cut_tac x = M in Rep_multiset)
wenzelm@10249
   190
  apply (simp add: multiset_def set_of_def count_def)
wenzelm@10249
   191
  done
wenzelm@10249
   192
wenzelm@10249
   193
theorem setsum_count_Int:
wenzelm@10249
   194
    "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
wenzelm@10249
   195
  apply (erule finite_induct)
wenzelm@10249
   196
   apply simp
wenzelm@10249
   197
  apply (simp add: Int_insert_left set_of_def)
wenzelm@10249
   198
  done
wenzelm@10249
   199
wenzelm@10249
   200
theorem size_union [simp]: "size (M + N::'a multiset) = size M + size N"
wenzelm@10249
   201
  apply (unfold size_def)
wenzelm@10249
   202
  apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
wenzelm@10249
   203
   prefer 2
wenzelm@10249
   204
   apply (rule ext)
wenzelm@10249
   205
   apply simp
wenzelm@10249
   206
  apply (simp (no_asm_simp) add: setsum_Un setsum_addf setsum_count_Int)
wenzelm@10249
   207
  apply (subst Int_commute)
wenzelm@10249
   208
  apply (simp (no_asm_simp) add: setsum_count_Int)
wenzelm@10249
   209
  done
wenzelm@10249
   210
wenzelm@10249
   211
theorem size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
wenzelm@10249
   212
  apply (unfold size_def Mempty_def count_def)
wenzelm@10249
   213
  apply auto
wenzelm@10249
   214
  apply (simp add: set_of_def count_def expand_fun_eq)
wenzelm@10249
   215
  done
wenzelm@10249
   216
wenzelm@10249
   217
theorem size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
wenzelm@10249
   218
  apply (unfold size_def)
wenzelm@10249
   219
  apply (drule setsum_SucD)
wenzelm@10249
   220
  apply auto
wenzelm@10249
   221
  done
wenzelm@10249
   222
wenzelm@10249
   223
wenzelm@10249
   224
subsubsection {* Equality of multisets *}
wenzelm@10249
   225
wenzelm@10249
   226
theorem multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
wenzelm@10249
   227
  apply (simp add: count_def expand_fun_eq)
wenzelm@10249
   228
  done
wenzelm@10249
   229
wenzelm@10249
   230
theorem single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
wenzelm@10249
   231
  apply (simp add: single_def Mempty_def expand_fun_eq)
wenzelm@10249
   232
  done
wenzelm@10249
   233
wenzelm@10249
   234
theorem single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
wenzelm@10249
   235
  apply (auto simp add: single_def expand_fun_eq)
wenzelm@10249
   236
  done
wenzelm@10249
   237
wenzelm@10249
   238
theorem union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
wenzelm@10249
   239
  apply (auto simp add: union_def Mempty_def expand_fun_eq)
wenzelm@10249
   240
  done
wenzelm@10249
   241
wenzelm@10249
   242
theorem empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
wenzelm@10249
   243
  apply (auto simp add: union_def Mempty_def expand_fun_eq)
wenzelm@10249
   244
  done
wenzelm@10249
   245
wenzelm@10249
   246
theorem union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
wenzelm@10249
   247
  apply (simp add: union_def expand_fun_eq)
wenzelm@10249
   248
  done
wenzelm@10249
   249
wenzelm@10249
   250
theorem union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
wenzelm@10249
   251
  apply (simp add: union_def expand_fun_eq)
wenzelm@10249
   252
  done
wenzelm@10249
   253
wenzelm@10249
   254
theorem union_is_single:
wenzelm@10249
   255
    "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
wenzelm@10249
   256
  apply (unfold Mempty_def single_def union_def)
wenzelm@10249
   257
  apply (simp add: add_is_1 expand_fun_eq)
wenzelm@10249
   258
  apply blast
wenzelm@10249
   259
  done
wenzelm@10249
   260
wenzelm@10249
   261
theorem single_is_union:
wenzelm@10249
   262
  "({#a#} = M + N) =
wenzelm@10249
   263
    ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
wenzelm@10249
   264
  apply (unfold Mempty_def single_def union_def)
wenzelm@10249
   265
  apply (simp add: one_is_add expand_fun_eq)
wenzelm@10249
   266
  apply (blast dest: sym)
wenzelm@10249
   267
  done
wenzelm@10249
   268
wenzelm@10249
   269
theorem add_eq_conv_diff:
wenzelm@10249
   270
  "(M + {#a#} = N + {#b#}) =
wenzelm@10249
   271
    (M = N \<and> a = b \<or>
wenzelm@10249
   272
      M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
wenzelm@10249
   273
  apply (unfold single_def union_def diff_def)
wenzelm@10249
   274
  apply (simp (no_asm) add: expand_fun_eq)
wenzelm@10249
   275
  apply (rule conjI)
wenzelm@10249
   276
   apply force
wenzelm@10249
   277
  apply clarify
wenzelm@10249
   278
  apply (rule conjI)
wenzelm@10249
   279
   apply blast
wenzelm@10249
   280
  apply clarify
wenzelm@10249
   281
  apply (rule iffI)
wenzelm@10249
   282
   apply (rule conjI)
wenzelm@10249
   283
    apply clarify
wenzelm@10249
   284
    apply (rule conjI)
wenzelm@10249
   285
     apply (simp add: eq_sym_conv)   (* FIXME blast fails !? *)
wenzelm@10249
   286
    apply fast
wenzelm@10249
   287
   apply simp
wenzelm@10249
   288
  apply force
wenzelm@10249
   289
  done
wenzelm@10249
   290
wenzelm@10249
   291
(*
wenzelm@10249
   292
val prems = Goal
wenzelm@10249
   293
 "[| !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> finite F --> P F";
wenzelm@10249
   294
by (res_inst_tac [("a","F"),("f","\<lambda>A. if finite A then card A else 0")]
wenzelm@10249
   295
     measure_induct 1);
wenzelm@10249
   296
by (Clarify_tac 1);
wenzelm@10249
   297
by (resolve_tac prems 1);
wenzelm@10249
   298
 by (assume_tac 1);
wenzelm@10249
   299
by (Clarify_tac 1);
wenzelm@10249
   300
by (subgoal_tac "finite G" 1);
wenzelm@10249
   301
 by (fast_tac (claset() addDs [finite_subset,order_less_le RS iffD1]) 2);
wenzelm@10249
   302
by (etac allE 1);
wenzelm@10249
   303
by (etac impE 1);
wenzelm@10249
   304
 by (Blast_tac 2);
wenzelm@10249
   305
by (asm_simp_tac (simpset() addsimps [psubset_card]) 1);
wenzelm@10249
   306
no_qed();
wenzelm@10249
   307
val lemma = result();
wenzelm@10249
   308
wenzelm@10249
   309
val prems = Goal
wenzelm@10249
   310
 "[| finite F; !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> P F";
wenzelm@10249
   311
by (rtac (lemma RS mp) 1);
wenzelm@10249
   312
by (REPEAT(ares_tac prems 1));
wenzelm@10249
   313
qed "finite_psubset_induct";
wenzelm@10249
   314
wenzelm@10249
   315
Better: use wf_finite_psubset in WF_Rel
wenzelm@10249
   316
*)
wenzelm@10249
   317
wenzelm@10249
   318
wenzelm@10249
   319
subsection {* Induction over multisets *}
wenzelm@10249
   320
wenzelm@10249
   321
lemma setsum_decr:
wenzelm@10249
   322
  "finite F ==> 0 < f a ==>
wenzelm@10249
   323
    setsum (f (a := f a - 1)) F = (if a \<in> F then setsum f F - 1 else setsum f F)"
wenzelm@10249
   324
  apply (erule finite_induct)
wenzelm@10249
   325
   apply auto
wenzelm@10249
   326
  apply (drule_tac a = a in mk_disjoint_insert)
wenzelm@10249
   327
  apply auto
wenzelm@10249
   328
  done
wenzelm@10249
   329
wenzelm@10313
   330
lemma rep_multiset_induct_aux:
wenzelm@10249
   331
  "P (\<lambda>a. 0) ==> (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1)))
wenzelm@10249
   332
    ==> \<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f"
wenzelm@10249
   333
proof -
wenzelm@10249
   334
  case antecedent
wenzelm@10249
   335
  note prems = this [unfolded multiset_def]
wenzelm@10249
   336
  show ?thesis
wenzelm@10249
   337
    apply (unfold multiset_def)
wenzelm@10249
   338
    apply (induct_tac n)
wenzelm@10249
   339
     apply simp
wenzelm@10249
   340
     apply clarify
wenzelm@10249
   341
     apply (subgoal_tac "f = (\<lambda>a.0)")
wenzelm@10249
   342
      apply simp
wenzelm@10249
   343
      apply (rule prems)
wenzelm@10249
   344
     apply (rule ext)
wenzelm@10249
   345
     apply force
wenzelm@10249
   346
    apply clarify
wenzelm@10249
   347
    apply (frule setsum_SucD)
wenzelm@10249
   348
    apply clarify
wenzelm@10249
   349
    apply (rename_tac a)
wenzelm@10249
   350
    apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}")
wenzelm@10249
   351
     prefer 2
wenzelm@10249
   352
     apply (rule finite_subset)
wenzelm@10249
   353
      prefer 2
wenzelm@10249
   354
      apply assumption
wenzelm@10249
   355
     apply simp
wenzelm@10249
   356
     apply blast
wenzelm@10249
   357
    apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
wenzelm@10249
   358
     prefer 2
wenzelm@10249
   359
     apply (rule ext)
wenzelm@10249
   360
     apply (simp (no_asm_simp))
wenzelm@10249
   361
     apply (erule ssubst, rule prems)
wenzelm@10249
   362
     apply blast
wenzelm@10249
   363
    apply (erule allE, erule impE, erule_tac [2] mp)
wenzelm@10249
   364
     apply blast
wenzelm@10249
   365
    apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply)
wenzelm@10249
   366
    apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}")
wenzelm@10249
   367
     prefer 2
wenzelm@10249
   368
     apply blast
wenzelm@10249
   369
    apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}")
wenzelm@10249
   370
     prefer 2
wenzelm@10249
   371
     apply blast
wenzelm@10249
   372
    apply (simp add: le_imp_diff_is_add setsum_diff1 cong: conj_cong)
wenzelm@10249
   373
    done
wenzelm@10249
   374
qed
wenzelm@10249
   375
wenzelm@10313
   376
theorem rep_multiset_induct:
wenzelm@10249
   377
  "f \<in> multiset ==> P (\<lambda>a. 0) ==>
wenzelm@10249
   378
    (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
wenzelm@10313
   379
  apply (insert rep_multiset_induct_aux)
wenzelm@10249
   380
  apply blast
wenzelm@10249
   381
  done
wenzelm@10249
   382
wenzelm@10249
   383
theorem multiset_induct [induct type: multiset]:
wenzelm@10249
   384
  "P {#} ==> (!!M x. P M ==> P (M + {#x#})) ==> P M"
wenzelm@10249
   385
proof -
wenzelm@10249
   386
  note defns = union_def single_def Mempty_def
wenzelm@10249
   387
  assume prem1 [unfolded defns]: "P {#}"
wenzelm@10249
   388
  assume prem2 [unfolded defns]: "!!M x. P M ==> P (M + {#x#})"
wenzelm@10249
   389
  show ?thesis
wenzelm@10249
   390
    apply (rule Rep_multiset_inverse [THEN subst])
wenzelm@10313
   391
    apply (rule Rep_multiset [THEN rep_multiset_induct])
wenzelm@10249
   392
     apply (rule prem1)
wenzelm@10249
   393
    apply (subgoal_tac "f (b := f b + 1) = (\<lambda>a. f a + (if a = b then 1 else 0))")
wenzelm@10249
   394
     prefer 2
wenzelm@10249
   395
     apply (simp add: expand_fun_eq)
wenzelm@10249
   396
    apply (erule ssubst)
wenzelm@10249
   397
    apply (erule Abs_multiset_inverse [THEN subst])
wenzelm@10249
   398
    apply (erule prem2 [simplified])
wenzelm@10249
   399
    done
wenzelm@10249
   400
qed
wenzelm@10249
   401
wenzelm@10249
   402
wenzelm@10249
   403
lemma MCollect_preserves_multiset:
wenzelm@10249
   404
    "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
wenzelm@10249
   405
  apply (simp add: multiset_def)
wenzelm@10249
   406
  apply (rule finite_subset)
wenzelm@10249
   407
   apply auto
wenzelm@10249
   408
  done
wenzelm@10249
   409
wenzelm@10249
   410
theorem count_MCollect [simp]:
wenzelm@10249
   411
    "count {# x:M. P x #} a = (if P a then count M a else 0)"
wenzelm@10249
   412
  apply (unfold count_def MCollect_def)
wenzelm@10249
   413
  apply (simp add: MCollect_preserves_multiset)
wenzelm@10249
   414
  done
wenzelm@10249
   415
wenzelm@10249
   416
theorem set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
wenzelm@10249
   417
  apply (auto simp add: set_of_def)
wenzelm@10249
   418
  done
wenzelm@10249
   419
wenzelm@10249
   420
theorem multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
wenzelm@10249
   421
  apply (subst multiset_eq_conv_count_eq)
wenzelm@10249
   422
  apply auto
wenzelm@10249
   423
  done
wenzelm@10249
   424
wenzelm@10277
   425
declare Rep_multiset_inject [symmetric, simp del]
wenzelm@10249
   426
declare multiset_typedef [simp del]
wenzelm@10249
   427
wenzelm@10249
   428
theorem add_eq_conv_ex:
wenzelm@10249
   429
  "(M + {#a#} = N + {#b#}) =
wenzelm@10249
   430
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
wenzelm@10249
   431
  apply (auto simp add: add_eq_conv_diff)
wenzelm@10249
   432
  done
wenzelm@10249
   433
wenzelm@10249
   434
wenzelm@10249
   435
subsection {* Multiset orderings *}
wenzelm@10249
   436
wenzelm@10249
   437
subsubsection {* Well-foundedness *}
wenzelm@10249
   438
wenzelm@10249
   439
constdefs
wenzelm@10249
   440
  mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
wenzelm@10249
   441
  "mult1 r ==
wenzelm@10249
   442
    {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
wenzelm@10249
   443
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
wenzelm@10249
   444
wenzelm@10249
   445
  mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
wenzelm@10392
   446
  "mult r == (mult1 r)\<^sup>+"
wenzelm@10249
   447
wenzelm@10249
   448
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
wenzelm@10277
   449
  by (simp add: mult1_def)
wenzelm@10249
   450
wenzelm@10249
   451
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
wenzelm@10249
   452
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
wenzelm@10249
   453
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@10249
   454
  (concl is "?case1 (mult1 r) \<or> ?case2")
wenzelm@10249
   455
proof (unfold mult1_def)
wenzelm@10249
   456
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
wenzelm@10249
   457
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
wenzelm@10249
   458
  let ?case1 = "?case1 {(N, M). ?R N M}"
wenzelm@10249
   459
wenzelm@10249
   460
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
wenzelm@10249
   461
  hence "\<exists>a' M0' K.
wenzelm@10249
   462
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
wenzelm@10249
   463
  thus "?case1 \<or> ?case2"
wenzelm@10249
   464
  proof (elim exE conjE)
wenzelm@10249
   465
    fix a' M0' K
wenzelm@10249
   466
    assume N: "N = M0' + K" and r: "?r K a'"
wenzelm@10249
   467
    assume "M0 + {#a#} = M0' + {#a'#}"
wenzelm@10249
   468
    hence "M0 = M0' \<and> a = a' \<or>
wenzelm@10249
   469
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
wenzelm@10249
   470
      by (simp only: add_eq_conv_ex)
wenzelm@10249
   471
    thus ?thesis
wenzelm@10249
   472
    proof (elim disjE conjE exE)
wenzelm@10249
   473
      assume "M0 = M0'" "a = a'"
wenzelm@10249
   474
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@10249
   475
      hence ?case2 .. thus ?thesis ..
wenzelm@10249
   476
    next
wenzelm@10249
   477
      fix K'
wenzelm@10249
   478
      assume "M0' = K' + {#a#}"
wenzelm@10249
   479
      with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
wenzelm@10249
   480
wenzelm@10249
   481
      assume "M0 = K' + {#a'#}"
wenzelm@10249
   482
      with r have "?R (K' + K) M0" by blast
wenzelm@10249
   483
      with n have ?case1 by simp thus ?thesis ..
wenzelm@10249
   484
    qed
wenzelm@10249
   485
  qed
wenzelm@10249
   486
qed
wenzelm@10249
   487
wenzelm@10249
   488
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
wenzelm@10249
   489
proof
wenzelm@10249
   490
  let ?R = "mult1 r"
wenzelm@10249
   491
  let ?W = "acc ?R"
wenzelm@10249
   492
  {
wenzelm@10249
   493
    fix M M0 a
wenzelm@10249
   494
    assume M0: "M0 \<in> ?W"
wenzelm@10249
   495
      and wf_hyp: "\<forall>b. (b, a) \<in> r --> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
wenzelm@10249
   496
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
wenzelm@10249
   497
    have "M0 + {#a#} \<in> ?W"
wenzelm@10249
   498
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
   499
      fix N
wenzelm@10249
   500
      assume "(N, M0 + {#a#}) \<in> ?R"
wenzelm@10249
   501
      hence "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
wenzelm@10249
   502
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
wenzelm@10249
   503
        by (rule less_add)
wenzelm@10249
   504
      thus "N \<in> ?W"
wenzelm@10249
   505
      proof (elim exE disjE conjE)
wenzelm@10249
   506
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
wenzelm@10249
   507
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
wenzelm@10249
   508
        hence "M + {#a#} \<in> ?W" ..
wenzelm@10249
   509
        thus "N \<in> ?W" by (simp only: N)
wenzelm@10249
   510
      next
wenzelm@10249
   511
        fix K
wenzelm@10249
   512
        assume N: "N = M0 + K"
wenzelm@10249
   513
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
wenzelm@10249
   514
        have "?this --> M0 + K \<in> ?W" (is "?P K")
wenzelm@10249
   515
        proof (induct K)
wenzelm@10249
   516
          from M0 have "M0 + {#} \<in> ?W" by simp
wenzelm@10249
   517
          thus "?P {#}" ..
wenzelm@10249
   518
wenzelm@10249
   519
          fix K x assume hyp: "?P K"
wenzelm@10249
   520
          show "?P (K + {#x#})"
wenzelm@10249
   521
          proof
wenzelm@10249
   522
            assume a: "\<forall>b. b :# (K + {#x#}) --> (b, a) \<in> r"
wenzelm@10249
   523
            hence "(x, a) \<in> r" by simp
wenzelm@10249
   524
            with wf_hyp have b: "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
wenzelm@10249
   525
wenzelm@10249
   526
            from a hyp have "M0 + K \<in> ?W" by simp
wenzelm@10249
   527
            with b have "(M0 + K) + {#x#} \<in> ?W" ..
wenzelm@10249
   528
            thus "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
wenzelm@10249
   529
          qed
wenzelm@10249
   530
        qed
wenzelm@10249
   531
        hence "M0 + K \<in> ?W" ..
wenzelm@10249
   532
        thus "N \<in> ?W" by (simp only: N)
wenzelm@10249
   533
      qed
wenzelm@10249
   534
    qed
wenzelm@10249
   535
  } note tedious_reasoning = this
wenzelm@10249
   536
wenzelm@10249
   537
  assume wf: "wf r"
wenzelm@10249
   538
  fix M
wenzelm@10249
   539
  show "M \<in> ?W"
wenzelm@10249
   540
  proof (induct M)
wenzelm@10249
   541
    show "{#} \<in> ?W"
wenzelm@10249
   542
    proof (rule accI)
wenzelm@10249
   543
      fix b assume "(b, {#}) \<in> ?R"
wenzelm@10249
   544
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
   545
    qed
wenzelm@10249
   546
wenzelm@10249
   547
    fix M a assume "M \<in> ?W"
wenzelm@10249
   548
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
   549
    proof induct
wenzelm@10249
   550
      fix a
wenzelm@10249
   551
      assume "\<forall>b. (b, a) \<in> r --> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
wenzelm@10249
   552
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
   553
      proof
wenzelm@10249
   554
        fix M assume "M \<in> ?W"
wenzelm@10249
   555
        thus "M + {#a#} \<in> ?W"
wenzelm@10249
   556
          by (rule acc_induct) (rule tedious_reasoning)
wenzelm@10249
   557
      qed
wenzelm@10249
   558
    qed
wenzelm@10249
   559
    thus "M + {#a#} \<in> ?W" ..
wenzelm@10249
   560
  qed
wenzelm@10249
   561
qed
wenzelm@10249
   562
wenzelm@10249
   563
theorem wf_mult1: "wf r ==> wf (mult1 r)"
wenzelm@10249
   564
  by (rule acc_wfI, rule all_accessible)
wenzelm@10249
   565
wenzelm@10249
   566
theorem wf_mult: "wf r ==> wf (mult r)"
wenzelm@10249
   567
  by (unfold mult_def, rule wf_trancl, rule wf_mult1)
wenzelm@10249
   568
wenzelm@10249
   569
wenzelm@10249
   570
subsubsection {* Closure-free presentation *}
wenzelm@10249
   571
wenzelm@10249
   572
(*Badly needed: a linear arithmetic procedure for multisets*)
wenzelm@10249
   573
wenzelm@10249
   574
lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
wenzelm@10249
   575
  apply (simp add: multiset_eq_conv_count_eq)
wenzelm@10249
   576
  done
wenzelm@10249
   577
wenzelm@10249
   578
text {* One direction. *}
wenzelm@10249
   579
wenzelm@10249
   580
lemma mult_implies_one_step:
wenzelm@10249
   581
  "trans r ==> (M, N) \<in> mult r ==>
wenzelm@10249
   582
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
wenzelm@10249
   583
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
wenzelm@10249
   584
  apply (unfold mult_def mult1_def set_of_def)
wenzelm@10249
   585
  apply (erule converse_trancl_induct)
wenzelm@10249
   586
  apply clarify
wenzelm@10249
   587
   apply (rule_tac x = M0 in exI)
wenzelm@10249
   588
   apply simp
wenzelm@10249
   589
  apply clarify
wenzelm@10249
   590
  apply (case_tac "a :# K")
wenzelm@10249
   591
   apply (rule_tac x = I in exI)
wenzelm@10249
   592
   apply (simp (no_asm))
wenzelm@10249
   593
   apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
wenzelm@10249
   594
   apply (simp (no_asm_simp) add: union_assoc [symmetric])
wenzelm@10249
   595
   apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
wenzelm@10249
   596
   apply (simp add: diff_union_single_conv)
wenzelm@10249
   597
   apply (simp (no_asm_use) add: trans_def)
wenzelm@10249
   598
   apply blast
wenzelm@10249
   599
  apply (subgoal_tac "a :# I")
wenzelm@10249
   600
   apply (rule_tac x = "I - {#a#}" in exI)
wenzelm@10249
   601
   apply (rule_tac x = "J + {#a#}" in exI)
wenzelm@10249
   602
   apply (rule_tac x = "K + Ka" in exI)
wenzelm@10249
   603
   apply (rule conjI)
wenzelm@10249
   604
    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
wenzelm@10249
   605
   apply (rule conjI)
wenzelm@10249
   606
    apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
wenzelm@10249
   607
    apply simp
wenzelm@10249
   608
    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
wenzelm@10249
   609
   apply (simp (no_asm_use) add: trans_def)
wenzelm@10249
   610
   apply blast
wenzelm@10277
   611
  apply (subgoal_tac "a :# (M0 + {#a#})")
wenzelm@10249
   612
   apply simp
wenzelm@10249
   613
  apply (simp (no_asm))
wenzelm@10249
   614
  done
wenzelm@10249
   615
wenzelm@10249
   616
lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
wenzelm@10249
   617
  apply (simp add: multiset_eq_conv_count_eq)
wenzelm@10249
   618
  done
wenzelm@10249
   619
wenzelm@10249
   620
lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
wenzelm@10249
   621
  apply (erule size_eq_Suc_imp_elem [THEN exE])
wenzelm@10249
   622
  apply (drule elem_imp_eq_diff_union)
wenzelm@10249
   623
  apply auto
wenzelm@10249
   624
  done
wenzelm@10249
   625
wenzelm@10249
   626
lemma one_step_implies_mult_aux:
wenzelm@10249
   627
  "trans r ==>
wenzelm@10249
   628
    \<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))
wenzelm@10249
   629
      --> (I + K, I + J) \<in> mult r"
wenzelm@10249
   630
  apply (induct_tac n)
wenzelm@10249
   631
   apply auto
wenzelm@10249
   632
  apply (frule size_eq_Suc_imp_eq_union)
wenzelm@10249
   633
  apply clarify
wenzelm@10249
   634
  apply (rename_tac "J'")
wenzelm@10249
   635
  apply simp
wenzelm@10249
   636
  apply (erule notE)
wenzelm@10249
   637
   apply auto
wenzelm@10249
   638
  apply (case_tac "J' = {#}")
wenzelm@10249
   639
   apply (simp add: mult_def)
wenzelm@10249
   640
   apply (rule r_into_trancl)
wenzelm@10249
   641
   apply (simp add: mult1_def set_of_def)
wenzelm@10249
   642
   apply blast
wenzelm@10249
   643
  txt {* Now we know @{term "J' \<noteq> {#}"}. *}
wenzelm@10249
   644
  apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
wenzelm@10249
   645
  apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
wenzelm@10249
   646
  apply (erule ssubst)
wenzelm@10249
   647
  apply (simp add: Ball_def)
wenzelm@10249
   648
  apply auto
wenzelm@10249
   649
  apply (subgoal_tac
wenzelm@10249
   650
    "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
wenzelm@10249
   651
      (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
wenzelm@10249
   652
   prefer 2
wenzelm@10249
   653
   apply force
wenzelm@10249
   654
  apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
wenzelm@10249
   655
  apply (erule trancl_trans)
wenzelm@10249
   656
  apply (rule r_into_trancl)
wenzelm@10249
   657
  apply (simp add: mult1_def set_of_def)
wenzelm@10249
   658
  apply (rule_tac x = a in exI)
wenzelm@10249
   659
  apply (rule_tac x = "I + J'" in exI)
wenzelm@10249
   660
  apply (simp add: union_ac)
wenzelm@10249
   661
  done
wenzelm@10249
   662
wenzelm@10249
   663
theorem one_step_implies_mult:
wenzelm@10249
   664
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
wenzelm@10249
   665
    ==> (I + K, I + J) \<in> mult r"
wenzelm@10249
   666
  apply (insert one_step_implies_mult_aux)
wenzelm@10249
   667
  apply blast
wenzelm@10249
   668
  done
wenzelm@10249
   669
wenzelm@10249
   670
wenzelm@10249
   671
subsubsection {* Partial-order properties *}
wenzelm@10249
   672
wenzelm@10313
   673
instance multiset :: ("term") ord ..
wenzelm@10249
   674
wenzelm@10249
   675
defs (overloaded)
wenzelm@10249
   676
  less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
wenzelm@10249
   677
  le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
wenzelm@10249
   678
wenzelm@10249
   679
lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
wenzelm@10249
   680
  apply (unfold trans_def)
wenzelm@10249
   681
  apply (blast intro: order_less_trans)
wenzelm@10249
   682
  done
wenzelm@10249
   683
wenzelm@10249
   684
text {*
wenzelm@10249
   685
 \medskip Irreflexivity.
wenzelm@10249
   686
*}
wenzelm@10249
   687
wenzelm@10249
   688
lemma mult_irrefl_aux:
wenzelm@10249
   689
    "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) --> A = {}"
wenzelm@10249
   690
  apply (erule finite_induct)
wenzelm@10249
   691
   apply (auto intro: order_less_trans)
wenzelm@10249
   692
  done
wenzelm@10249
   693
wenzelm@10249
   694
theorem mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
wenzelm@10249
   695
  apply (unfold less_multiset_def)
wenzelm@10249
   696
  apply auto
wenzelm@10249
   697
  apply (drule trans_base_order [THEN mult_implies_one_step])
wenzelm@10249
   698
  apply auto
wenzelm@10249
   699
  apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
wenzelm@10249
   700
  apply (simp add: set_of_eq_empty_iff)
wenzelm@10249
   701
  done
wenzelm@10249
   702
wenzelm@10249
   703
lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
wenzelm@10249
   704
  apply (insert mult_less_not_refl)
wenzelm@10249
   705
  apply blast
wenzelm@10249
   706
  done
wenzelm@10249
   707
wenzelm@10249
   708
wenzelm@10249
   709
text {* Transitivity. *}
wenzelm@10249
   710
wenzelm@10249
   711
theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
wenzelm@10249
   712
  apply (unfold less_multiset_def mult_def)
wenzelm@10249
   713
  apply (blast intro: trancl_trans)
wenzelm@10249
   714
  done
wenzelm@10249
   715
wenzelm@10249
   716
text {* Asymmetry. *}
wenzelm@10249
   717
wenzelm@10249
   718
theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
wenzelm@10249
   719
  apply auto
wenzelm@10249
   720
  apply (rule mult_less_not_refl [THEN notE])
wenzelm@10249
   721
  apply (erule mult_less_trans)
wenzelm@10249
   722
  apply assumption
wenzelm@10249
   723
  done
wenzelm@10249
   724
wenzelm@10249
   725
theorem mult_less_asym:
wenzelm@10249
   726
    "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
wenzelm@10249
   727
  apply (insert mult_less_not_sym)
wenzelm@10249
   728
  apply blast
wenzelm@10249
   729
  done
wenzelm@10249
   730
wenzelm@10249
   731
theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
wenzelm@10249
   732
  apply (unfold le_multiset_def)
wenzelm@10249
   733
  apply auto
wenzelm@10249
   734
  done
wenzelm@10249
   735
wenzelm@10249
   736
text {* Anti-symmetry. *}
wenzelm@10249
   737
wenzelm@10249
   738
theorem mult_le_antisym:
wenzelm@10249
   739
    "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
wenzelm@10249
   740
  apply (unfold le_multiset_def)
wenzelm@10249
   741
  apply (blast dest: mult_less_not_sym)
wenzelm@10249
   742
  done
wenzelm@10249
   743
wenzelm@10249
   744
text {* Transitivity. *}
wenzelm@10249
   745
wenzelm@10249
   746
theorem mult_le_trans:
wenzelm@10249
   747
    "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
wenzelm@10249
   748
  apply (unfold le_multiset_def)
wenzelm@10249
   749
  apply (blast intro: mult_less_trans)
wenzelm@10249
   750
  done
wenzelm@10249
   751
wenzelm@10249
   752
theorem mult_less_le: "M < N = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
wenzelm@10249
   753
  apply (unfold le_multiset_def)
wenzelm@10249
   754
  apply auto
wenzelm@10249
   755
  done
wenzelm@10249
   756
wenzelm@10277
   757
text {* Partial order. *}
wenzelm@10277
   758
wenzelm@10277
   759
instance multiset :: (order) order
wenzelm@10277
   760
  apply intro_classes
wenzelm@10277
   761
     apply (rule mult_le_refl)
wenzelm@10277
   762
    apply (erule mult_le_trans)
wenzelm@10277
   763
    apply assumption
wenzelm@10277
   764
   apply (erule mult_le_antisym)
wenzelm@10277
   765
   apply assumption
wenzelm@10277
   766
  apply (rule mult_less_le)
wenzelm@10277
   767
  done
wenzelm@10277
   768
wenzelm@10249
   769
wenzelm@10249
   770
subsubsection {* Monotonicity of multiset union *}
wenzelm@10249
   771
wenzelm@10249
   772
theorem mult1_union:
wenzelm@10249
   773
    "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
wenzelm@10249
   774
  apply (unfold mult1_def)
wenzelm@10249
   775
  apply auto
wenzelm@10249
   776
  apply (rule_tac x = a in exI)
wenzelm@10249
   777
  apply (rule_tac x = "C + M0" in exI)
wenzelm@10249
   778
  apply (simp add: union_assoc)
wenzelm@10249
   779
  done
wenzelm@10249
   780
wenzelm@10249
   781
lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
wenzelm@10249
   782
  apply (unfold less_multiset_def mult_def)
wenzelm@10249
   783
  apply (erule trancl_induct)
wenzelm@10249
   784
   apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
wenzelm@10249
   785
  apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
wenzelm@10249
   786
  done
wenzelm@10249
   787
wenzelm@10249
   788
lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
wenzelm@10249
   789
  apply (subst union_commute [of B C])
wenzelm@10249
   790
  apply (subst union_commute [of D C])
wenzelm@10249
   791
  apply (erule union_less_mono2)
wenzelm@10249
   792
  done
wenzelm@10249
   793
wenzelm@10249
   794
theorem union_less_mono:
wenzelm@10249
   795
    "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
wenzelm@10249
   796
  apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
wenzelm@10249
   797
  done
wenzelm@10249
   798
wenzelm@10249
   799
theorem union_le_mono:
wenzelm@10249
   800
    "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
wenzelm@10249
   801
  apply (unfold le_multiset_def)
wenzelm@10249
   802
  apply (blast intro: union_less_mono union_less_mono1 union_less_mono2)
wenzelm@10249
   803
  done
wenzelm@10249
   804
wenzelm@10249
   805
theorem empty_leI [iff]: "{#} <= (M::'a::order multiset)"
wenzelm@10249
   806
  apply (unfold le_multiset_def less_multiset_def)
wenzelm@10249
   807
  apply (case_tac "M = {#}")
wenzelm@10249
   808
   prefer 2
wenzelm@10249
   809
   apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
wenzelm@10249
   810
    prefer 2
wenzelm@10249
   811
    apply (rule one_step_implies_mult)
wenzelm@10249
   812
      apply (simp only: trans_def)
wenzelm@10249
   813
      apply auto
wenzelm@10249
   814
  apply (blast intro: order_less_trans)
wenzelm@10249
   815
  done
wenzelm@10249
   816
wenzelm@10249
   817
theorem union_upper1: "A <= A + (B::'a::order multiset)"
wenzelm@10249
   818
  apply (subgoal_tac "A + {#} <= A + B")
wenzelm@10249
   819
   prefer 2
wenzelm@10249
   820
   apply (rule union_le_mono)
wenzelm@10249
   821
    apply auto
wenzelm@10249
   822
  done
wenzelm@10249
   823
wenzelm@10249
   824
theorem union_upper2: "B <= A + (B::'a::order multiset)"
wenzelm@10249
   825
  apply (subst union_commute, rule union_upper1)
wenzelm@10249
   826
  done
wenzelm@10249
   827
wenzelm@10249
   828
end