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