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