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