src/HOL/Library/Multiset.thy
author paulson <lp15@cam.ac.uk>
Tue, 24 May 2016 13:57:04 +0100
changeset 63127 360d9997fac9
parent 63099 af0e964aad7b
child 63195 f3f08c0d4aaf
permissions -rw-r--r--
new theorem
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
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
     2
    Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
     3
    Author:     Andrei Popescu, TU Muenchen
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
     4
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
     5
    Author:     Dmitriy Traytel, TU Muenchen
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
     6
    Author:     Mathias Fleury, MPII
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     7
*)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     8
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
     9
section \<open>(Finite) multisets\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    10
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
    11
theory Multiset
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents: 51548
diff changeset
    12
imports Main
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
    13
begin
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    14
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    15
subsection \<open>The type of multisets\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    16
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    17
definition "multiset = {f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    18
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    19
typedef 'a multiset = "multiset :: ('a \<Rightarrow> nat) set"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    20
  morphisms count Abs_multiset
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    21
  unfolding multiset_def
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    22
proof
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    23
  show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    24
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    25
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    26
setup_lifting type_definition_multiset
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
    27
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    28
lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    29
  by (simp only: count_inject [symmetric] fun_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    30
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    31
lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    32
  using multiset_eq_iff by auto
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    33
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    34
text \<open>Preservation of the representing set @{term multiset}.\<close>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    35
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    36
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    37
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    38
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    39
lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    40
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    41
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    42
lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    43
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    44
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    45
lemma diff_preserves_multiset:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    46
  assumes "M \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    47
  shows "(\<lambda>a. M a - N a) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    48
proof -
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    49
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    50
    by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    51
  with assms show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    52
    by (auto simp add: multiset_def intro: finite_subset)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    53
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    54
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    55
lemma filter_preserves_multiset:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    56
  assumes "M \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    57
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    58
proof -
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    59
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    60
    by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    61
  with assms show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    62
    by (auto simp add: multiset_def intro: finite_subset)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    63
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    64
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    65
lemmas in_multiset = const0_in_multiset only1_in_multiset
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    66
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    67
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    68
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    69
subsection \<open>Representing multisets\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    70
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    71
text \<open>Multiset enumeration\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    72
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    73
instantiation multiset :: (type) cancel_comm_monoid_add
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    74
begin
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    75
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    76
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    77
by (rule const0_in_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    78
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    79
abbreviation Mempty :: "'a multiset" ("{#}") where
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    80
  "Mempty \<equiv> 0"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    81
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    82
lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    83
by (rule union_preserves_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    84
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    85
lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
    86
by (rule diff_preserves_multiset)
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
    87
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    88
instance
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
    89
  by (standard; transfer; simp add: fun_eq_iff)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    90
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    91
end
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    92
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    93
lift_definition single :: "'a \<Rightarrow> 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    94
by (rule only1_in_multiset)
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
    95
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    96
syntax
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    97
  "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
25507
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
    98
translations
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
    99
  "{#x, xs#}" == "{#x#} + {#xs#}"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   100
  "{#x#}" == "CONST single x"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   101
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   102
lemma count_empty [simp]: "count {#} a = 0"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   103
  by (simp add: zero_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   104
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   105
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   106
  by (simp add: single.rep_eq)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29509
diff changeset
   107
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   108
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   109
subsection \<open>Basic operations\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   110
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   111
subsubsection \<open>Conversion to set and membership\<close>
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   112
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   113
definition set_mset :: "'a multiset \<Rightarrow> 'a set"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   114
  where "set_mset M = {x. count M x > 0}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   115
62537
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   116
abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   117
  where "Melem a M \<equiv> a \<in> set_mset M"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   118
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   119
notation
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   120
  Melem  ("op \<in>#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   121
  Melem  ("(_/ \<in># _)" [51, 51] 50)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   122
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   123
notation  (ASCII)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   124
  Melem  ("op :#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   125
  Melem  ("(_/ :# _)" [51, 51] 50)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   126
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   127
abbreviation not_Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   128
  where "not_Melem a M \<equiv> a \<notin> set_mset M"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   129
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   130
notation
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   131
  not_Melem  ("op \<notin>#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   132
  not_Melem  ("(_/ \<notin># _)" [51, 51] 50)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   133
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   134
notation  (ASCII)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   135
  not_Melem  ("op ~:#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   136
  not_Melem  ("(_/ ~:# _)" [51, 51] 50)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   137
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   138
context
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   139
begin
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   140
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   141
qualified abbreviation Ball :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   142
  where "Ball M \<equiv> Set.Ball (set_mset M)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   143
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   144
qualified abbreviation Bex :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   145
  where "Bex M \<equiv> Set.Bex (set_mset M)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   146
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   147
end
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   148
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   149
syntax
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   150
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>#_./ _)" [0, 0, 10] 10)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   151
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>#_./ _)" [0, 0, 10] 10)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   152
62537
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   153
syntax  (ASCII)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   154
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_:#_./ _)" [0, 0, 10] 10)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   155
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_:#_./ _)" [0, 0, 10] 10)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   156
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   157
translations
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   158
  "\<forall>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Ball A (\<lambda>x. P)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   159
  "\<exists>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Bex A (\<lambda>x. P)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   160
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   161
lemma count_eq_zero_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   162
  "count M x = 0 \<longleftrightarrow> x \<notin># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   163
  by (auto simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   164
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   165
lemma not_in_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   166
  "x \<notin># M \<longleftrightarrow> count M x = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   167
  by (auto simp add: count_eq_zero_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   168
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   169
lemma count_greater_zero_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   170
  "count M x > 0 \<longleftrightarrow> x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   171
  by (auto simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   172
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   173
lemma count_inI:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   174
  assumes "count M x = 0 \<Longrightarrow> False"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   175
  shows "x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   176
proof (rule ccontr)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   177
  assume "x \<notin># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   178
  with assms show False by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   179
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   180
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   181
lemma in_countE:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   182
  assumes "x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   183
  obtains n where "count M x = Suc n"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   184
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   185
  from assms have "count M x > 0" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   186
  then obtain n where "count M x = Suc n"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   187
    using gr0_conv_Suc by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   188
  with that show thesis .
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   189
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   190
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   191
lemma count_greater_eq_Suc_zero_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   192
  "count M x \<ge> Suc 0 \<longleftrightarrow> x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   193
  by (simp add: Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   194
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   195
lemma count_greater_eq_one_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   196
  "count M x \<ge> 1 \<longleftrightarrow> x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   197
  by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   198
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   199
lemma set_mset_empty [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   200
  "set_mset {#} = {}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   201
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   202
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   203
lemma set_mset_single [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   204
  "set_mset {#b#} = {b}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   205
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   206
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   207
lemma set_mset_eq_empty_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   208
  "set_mset M = {} \<longleftrightarrow> M = {#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   209
  by (auto simp add: multiset_eq_iff count_eq_zero_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   210
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   211
lemma finite_set_mset [iff]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   212
  "finite (set_mset M)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   213
  using count [of M] by (simp add: multiset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   214
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   215
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   216
subsubsection \<open>Union\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   217
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   218
lemma count_union [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   219
  "count (M + N) a = count M a + count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   220
  by (simp add: plus_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   221
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   222
lemma set_mset_union [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   223
  "set_mset (M + N) = set_mset M \<union> set_mset N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   224
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_union) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   225
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   226
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   227
subsubsection \<open>Difference\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   228
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   229
instance multiset :: (type) comm_monoid_diff
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   230
  by standard (transfer; simp add: fun_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   231
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   232
lemma count_diff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   233
  "count (M - N) a = count M a - count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   234
  by (simp add: minus_multiset.rep_eq)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   235
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   236
lemma in_diff_count:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   237
  "a \<in># M - N \<longleftrightarrow> count N a < count M a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   238
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   239
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   240
lemma count_in_diffI:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   241
  assumes "\<And>n. count N x = n + count M x \<Longrightarrow> False"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   242
  shows "x \<in># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   243
proof (rule ccontr)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   244
  assume "x \<notin># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   245
  then have "count N x = (count N x - count M x) + count M x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   246
    by (simp add: in_diff_count not_less)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   247
  with assms show False by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   248
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   249
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   250
lemma in_diff_countE:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   251
  assumes "x \<in># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   252
  obtains n where "count M x = Suc n + count N x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   253
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   254
  from assms have "count M x - count N x > 0" by (simp add: in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   255
  then have "count M x > count N x" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   256
  then obtain n where "count M x = Suc n + count N x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   257
    using less_iff_Suc_add by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   258
  with that show thesis .
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   259
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   260
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   261
lemma in_diffD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   262
  assumes "a \<in># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   263
  shows "a \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   264
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   265
  have "0 \<le> count N a" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   266
  also from assms have "count N a < count M a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   267
    by (simp add: in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   268
  finally show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   269
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   270
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   271
lemma set_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   272
  "set_mset (M - N) = {a. count N a < count M a}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   273
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   274
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   275
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   276
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   277
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   278
lemma diff_cancel [simp]: "A - A = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   279
  by (fact Groups.diff_cancel)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   280
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   281
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   282
  by (fact add_diff_cancel_right')
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   283
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   284
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   285
  by (fact add_diff_cancel_left')
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   286
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   287
lemma diff_right_commute:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   288
  fixes M N Q :: "'a multiset"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   289
  shows "M - N - Q = M - Q - N"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   290
  by (fact diff_right_commute)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   291
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   292
lemma diff_add:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   293
  fixes M N Q :: "'a multiset"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   294
  shows "M - (N + Q) = M - N - Q"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   295
  by (rule sym) (fact diff_diff_add)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   296
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   297
lemma insert_DiffM: "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   298
  by (clarsimp simp: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   299
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   300
lemma insert_DiffM2 [simp]: "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   301
  by (clarsimp simp: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   302
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   303
lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   304
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   305
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   306
lemma diff_union_single_conv:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   307
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   308
  by (simp add: multiset_eq_iff Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   309
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   310
lemma mset_add [elim?]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   311
  assumes "a \<in># A"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   312
  obtains B where "A = B + {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   313
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   314
  from assms have "A = (A - {#a#}) + {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   315
    by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   316
  with that show thesis .
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   317
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   318
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   319
lemma union_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   320
  "a \<in># A + B \<longleftrightarrow> a \<in># A \<or> a \<in># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   321
  by auto
26143
314c0bcb7df7 Added useful general lemmas from the work with the HeapMonad
bulwahn
parents: 26033
diff changeset
   322
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   323
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   324
subsubsection \<open>Equality of multisets\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   325
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   326
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   327
  by (simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   328
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   329
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   330
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   331
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   332
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   333
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   334
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   335
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   336
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   337
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   338
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   339
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   340
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   341
lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   342
  by (auto simp add: multiset_eq_iff not_in_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   343
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   344
lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   345
  by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   346
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   347
lemma union_single_eq_diff: "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   348
  by (auto dest: sym)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   349
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   350
lemma union_single_eq_member: "M + {#x#} = N \<Longrightarrow> x \<in># N"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   351
  by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   352
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   353
lemma union_is_single:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   354
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N = {#} \<or> M = {#} \<and> N = {#a#}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   355
  (is "?lhs = ?rhs")
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   356
proof
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   357
  show ?lhs if ?rhs using that by auto
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   358
  show ?rhs if ?lhs
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   359
    by (metis Multiset.diff_cancel add.commute add_diff_cancel_left' diff_add_zero diff_single_trivial insert_DiffM that)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   360
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   361
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   362
lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   363
  by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   364
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   365
lemma add_eq_conv_diff:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   366
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   367
  (is "?lhs \<longleftrightarrow> ?rhs")
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44339
diff changeset
   368
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   369
proof
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   370
  show ?lhs if ?rhs
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   371
    using that
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   372
    by (auto simp add: add.assoc add.commute [of "{#b#}"])
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   373
      (drule sym, simp add: add.assoc [symmetric])
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   374
  show ?rhs if ?lhs
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   375
  proof (cases "a = b")
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   376
    case True with \<open>?lhs\<close> show ?thesis by simp
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   377
  next
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   378
    case False
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   379
    from \<open>?lhs\<close> have "a \<in># N + {#b#}" by (rule union_single_eq_member)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   380
    with False have "a \<in># N" by auto
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   381
    moreover from \<open>?lhs\<close> have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   382
    moreover note False
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   383
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   384
  qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   385
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   386
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   387
lemma insert_noteq_member:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   388
  assumes BC: "B + {#b#} = C + {#c#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   389
   and bnotc: "b \<noteq> c"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   390
  shows "c \<in># B"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   391
proof -
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   392
  have "c \<in># C + {#c#}" by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   393
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   394
  then have "c \<in># B + {#b#}" using BC by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   395
  then show "c \<in># B" using nc by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   396
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   397
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   398
lemma add_eq_conv_ex:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   399
  "(M + {#a#} = N + {#b#}) =
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   400
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   401
  by (auto simp add: add_eq_conv_diff)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   402
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   403
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   404
  by (rule exI [where x = "M - {#x#}"]) simp
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   405
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   406
lemma multiset_add_sub_el_shuffle:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   407
  assumes "c \<in># B"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   408
    and "b \<noteq> c"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   409
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   410
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   411
  from \<open>c \<in># B\<close> obtain A where B: "B = A + {#c#}"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   412
    by (blast dest: multi_member_split)
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   413
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   414
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   415
    by (simp add: ac_simps)
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   416
  then show ?thesis using B by simp
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   417
qed
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   418
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   419
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
   420
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
   421
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   422
subsubsection \<open>Pointwise ordering induced by count\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   423
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   424
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<subseteq>#" 50)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   425
  where "A \<subseteq># B = (\<forall>a. count A a \<le> count B a)"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   426
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   427
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   428
  where "A \<subset># B = (A \<subseteq># B \<and> A \<noteq> B)"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   429
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   430
abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supseteq>#" 50)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   431
  where "supseteq_mset A B \<equiv> B \<subseteq># A"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   432
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   433
abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supset>#" 50)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   434
  where "supset_mset A B \<equiv> B \<subset># A"
62208
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   435
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   436
notation (input)
62208
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   437
  subseteq_mset  (infix "\<le>#" 50) and
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   438
  supseteq_mset  (infix "\<ge>#" 50)
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   439
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   440
notation (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   441
  subseteq_mset  (infix "<=#" 50) and
62208
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   442
  subset_mset  (infix "<#" 50) and
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   443
  supseteq_mset  (infix ">=#" 50) and
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   444
  supset_mset  (infix ">#" 50)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   445
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   446
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   447
  by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym)
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   448
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   449
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   450
lemma mset_less_eqI:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   451
  "(\<And>a. count A a \<le> count B a) \<Longrightarrow> A \<subseteq># B"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   452
  by (simp add: subseteq_mset_def)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   453
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   454
lemma mset_less_eq_count:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   455
  "A \<subseteq># B \<Longrightarrow> count A a \<le> count B a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   456
  by (simp add: subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   457
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   458
lemma mset_le_exists_conv: "(A::'a multiset) \<subseteq># B \<longleftrightarrow> (\<exists>C. B = A + C)"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   459
  unfolding subseteq_mset_def
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   460
  apply (rule iffI)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   461
   apply (rule exI [where x = "B - A"])
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   462
   apply (auto intro: multiset_eq_iff [THEN iffD2])
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   463
  done
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   464
62376
85f38d5f8807 Rename ordered_comm_monoid_add to ordered_cancel_comm_monoid_add. Introduce ordreed_comm_monoid_add, canonically_ordered_comm_monoid and dioid. Setup nat, entat and ennreal as dioids.
hoelzl
parents: 62366
diff changeset
   465
interpretation subset_mset: ordered_cancel_comm_monoid_diff  "op +" 0 "op \<le>#" "op <#" "op -"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   466
  by standard (simp, fact mset_le_exists_conv)
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   467
62378
85ed00c1fe7c generalize more theorems to support enat and ennreal
hoelzl
parents: 62376
diff changeset
   468
declare subset_mset.zero_order[simp del]
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   469
  \<comment> \<open>this removes some simp rules not in the usual order for multisets\<close>
62378
85ed00c1fe7c generalize more theorems to support enat and ennreal
hoelzl
parents: 62376
diff changeset
   470
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   471
lemma mset_le_mono_add_right_cancel [simp]: "(A::'a multiset) + C \<subseteq># B + C \<longleftrightarrow> A \<subseteq># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   472
   by (fact subset_mset.add_le_cancel_right)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   473
 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   474
lemma mset_le_mono_add_left_cancel [simp]: "C + (A::'a multiset) \<subseteq># C + B \<longleftrightarrow> A \<subseteq># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   475
   by (fact subset_mset.add_le_cancel_left)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   476
 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   477
lemma mset_le_mono_add: "(A::'a multiset) \<subseteq># B \<Longrightarrow> C \<subseteq># D \<Longrightarrow> A + C \<subseteq># B + D"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   478
   by (fact subset_mset.add_mono)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   479
 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   480
lemma mset_le_add_left [simp]: "(A::'a multiset) \<subseteq># A + B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   481
   unfolding subseteq_mset_def by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   482
 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   483
lemma mset_le_add_right [simp]: "B \<subseteq># (A::'a multiset) + B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   484
   unfolding subseteq_mset_def by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   485
 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   486
lemma single_subset_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   487
  "{#a#} \<subseteq># M \<longleftrightarrow> a \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   488
  by (auto simp add: subseteq_mset_def Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   489
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   490
lemma mset_le_single: "a \<in># B \<Longrightarrow> {#a#} \<subseteq># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   491
  by (simp add: subseteq_mset_def Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   492
 
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   493
lemma multiset_diff_union_assoc:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   494
  fixes A B C D :: "'a multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   495
  shows "C \<subseteq># B \<Longrightarrow> A + B - C = A + (B - C)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   496
  by (fact subset_mset.diff_add_assoc)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   497
 
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   498
lemma mset_le_multiset_union_diff_commute:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   499
  fixes A B C D :: "'a multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   500
  shows "B \<subseteq># A \<Longrightarrow> A - B + C = A + C - B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   501
  by (fact subset_mset.add_diff_assoc2)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   502
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   503
lemma diff_le_self[simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   504
  "(M::'a multiset) - N \<subseteq># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   505
  by (simp add: subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   506
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   507
lemma mset_leD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   508
  assumes "A \<subseteq># B" and "x \<in># A"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   509
  shows "x \<in># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   510
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   511
  from \<open>x \<in># A\<close> have "count A x > 0" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   512
  also from \<open>A \<subseteq># B\<close> have "count A x \<le> count B x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   513
    by (simp add: subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   514
  finally show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   515
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   516
  
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   517
lemma mset_lessD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   518
  "A \<subset># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   519
  by (auto intro: mset_leD [of A])
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   520
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   521
lemma set_mset_mono:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   522
  "A \<subseteq># B \<Longrightarrow> set_mset A \<subseteq> set_mset B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   523
  by (metis mset_leD subsetI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   524
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   525
lemma mset_le_insertD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   526
  "A + {#x#} \<subseteq># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   527
apply (rule conjI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   528
 apply (simp add: mset_leD)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   529
 apply (clarsimp simp: subset_mset_def subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   530
 apply safe
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   531
  apply (erule_tac x = a in allE)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   532
  apply (auto split: if_split_asm)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   533
done
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   534
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   535
lemma mset_less_insertD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   536
  "A + {#x#} \<subset># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   537
  by (rule mset_le_insertD) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   538
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   539
lemma mset_less_of_empty[simp]: "A \<subset># {#} \<longleftrightarrow> False"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   540
  by (auto simp add: subseteq_mset_def subset_mset_def multiset_eq_iff)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   541
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   542
lemma empty_le [simp]: "{#} \<subseteq># A"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   543
  unfolding mset_le_exists_conv by auto
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   544
 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   545
lemma insert_subset_eq_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   546
  "{#a#} + A \<subseteq># B \<longleftrightarrow> a \<in># B \<and> A \<subseteq># B - {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   547
  using le_diff_conv2 [of "Suc 0" "count B a" "count A a"]
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   548
  apply (auto simp add: subseteq_mset_def not_in_iff Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   549
  apply (rule ccontr)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   550
  apply (auto simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   551
  done
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   552
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   553
lemma insert_union_subset_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   554
  "{#a#} + A \<subset># B \<longleftrightarrow> a \<in># B \<and> A \<subset># B - {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   555
  by (auto simp add: insert_subset_eq_iff subset_mset_def insert_DiffM)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   556
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   557
lemma subset_eq_diff_conv:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   558
  "A - C \<subseteq># B \<longleftrightarrow> A \<subseteq># B + C"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   559
  by (simp add: subseteq_mset_def le_diff_conv)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   560
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   561
lemma le_empty [simp]: "M \<subseteq># {#} \<longleftrightarrow> M = {#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   562
  unfolding mset_le_exists_conv by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   563
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   564
lemma multi_psub_of_add_self[simp]: "A \<subset># A + {#x#}"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   565
  by (auto simp: subset_mset_def subseteq_mset_def)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   566
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   567
lemma multi_psub_self[simp]: "(A::'a multiset) \<subset># A = False"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   568
  by simp
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   569
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   570
lemma mset_less_add_bothsides: "N + {#x#} \<subset># M + {#x#} \<Longrightarrow> N \<subset># M"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   571
  by (fact subset_mset.add_less_imp_less_right)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   572
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   573
lemma mset_less_empty_nonempty: "{#} \<subset># S \<longleftrightarrow> S \<noteq> {#}"
62378
85ed00c1fe7c generalize more theorems to support enat and ennreal
hoelzl
parents: 62376
diff changeset
   574
  by (fact subset_mset.zero_less_iff_neq_zero)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   575
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   576
lemma mset_less_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} \<subset># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   577
  by (auto simp: subset_mset_def elim: mset_add)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   578
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   579
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   580
subsubsection \<open>Intersection\<close>
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   581
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   582
definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   583
  multiset_inter_def: "inf_subset_mset A B = A - (A - B)"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   584
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   585
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<subseteq>#" "op \<subset>#"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   586
proof -
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   587
  have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   588
    by arith
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   589
  show "class.semilattice_inf op #\<inter> op \<subseteq># op \<subset>#"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   590
    by standard (auto simp add: multiset_inter_def subseteq_mset_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   591
qed
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   592
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   593
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   594
lemma multiset_inter_count [simp]:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   595
  fixes A B :: "'a multiset"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   596
  shows "count (A #\<inter> B) x = min (count A x) (count B x)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   597
  by (simp add: multiset_inter_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   598
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   599
lemma set_mset_inter [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   600
  "set_mset (A #\<inter> B) = set_mset A \<inter> set_mset B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   601
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] multiset_inter_count) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   602
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   603
lemma diff_intersect_left_idem [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   604
  "M - M #\<inter> N = M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   605
  by (simp add: multiset_eq_iff min_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   606
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   607
lemma diff_intersect_right_idem [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   608
  "M - N #\<inter> M = M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   609
  by (simp add: multiset_eq_iff min_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   610
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   611
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   612
  by (rule multiset_eqI) auto
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   613
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   614
lemma multiset_union_diff_commute:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   615
  assumes "B #\<inter> C = {#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   616
  shows "A + B - C = A - C + B"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   617
proof (rule multiset_eqI)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   618
  fix x
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   619
  from assms have "min (count B x) (count C x) = 0"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   620
    by (auto simp add: multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   621
  then have "count B x = 0 \<or> count C x = 0"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   622
    unfolding min_def by (auto split: if_splits)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   623
  then show "count (A + B - C) x = count (A - C + B) x"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   624
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   625
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   626
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   627
lemma disjunct_not_in:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   628
  "A #\<inter> B = {#} \<longleftrightarrow> (\<forall>a. a \<notin># A \<or> a \<notin># B)" (is "?P \<longleftrightarrow> ?Q")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   629
proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   630
  assume ?P
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   631
  show ?Q
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   632
  proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   633
    fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   634
    from \<open>?P\<close> have "min (count A a) (count B a) = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   635
      by (simp add: multiset_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   636
    then have "count A a = 0 \<or> count B a = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   637
      by (cases "count A a \<le> count B a") (simp_all add: min_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   638
    then show "a \<notin># A \<or> a \<notin># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   639
      by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   640
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   641
next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   642
  assume ?Q
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   643
  show ?P
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   644
  proof (rule multiset_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   645
    fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   646
    from \<open>?Q\<close> have "count A a = 0 \<or> count B a = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   647
      by (auto simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   648
    then show "count (A #\<inter> B) a = count {#} a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   649
      by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   650
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   651
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   652
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   653
lemma empty_inter [simp]: "{#} #\<inter> M = {#}"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   654
  by (simp add: multiset_eq_iff)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   655
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   656
lemma inter_empty [simp]: "M #\<inter> {#} = {#}"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   657
  by (simp add: multiset_eq_iff)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   658
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   659
lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   660
  by (simp add: multiset_eq_iff not_in_iff)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   661
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   662
lemma inter_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   663
  by (auto simp add: multiset_eq_iff elim: mset_add)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   664
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   665
lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   666
  by (simp add: multiset_eq_iff not_in_iff)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   667
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   668
lemma inter_add_right2: "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   669
  by (auto simp add: multiset_eq_iff elim: mset_add)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   670
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   671
lemma disjunct_set_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   672
  assumes "M #\<inter> N = {#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   673
  shows "set_mset (M - N) = set_mset M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   674
proof (rule set_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   675
  fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   676
  from assms have "a \<notin># M \<or> a \<notin># N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   677
    by (simp add: disjunct_not_in)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   678
  then show "a \<in># M - N \<longleftrightarrow> a \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   679
    by (auto dest: in_diffD) (simp add: in_diff_count not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   680
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   681
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   682
lemma at_most_one_mset_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   683
  assumes "a \<notin># M - {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   684
  shows "set_mset (M - {#a#}) = set_mset M - {a}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   685
  using assms by (auto simp add: not_in_iff in_diff_count set_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   686
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   687
lemma more_than_one_mset_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   688
  assumes "a \<in># M - {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   689
  shows "set_mset (M - {#a#}) = set_mset M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   690
proof (rule set_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   691
  fix b
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   692
  have "Suc 0 < count M b \<Longrightarrow> count M b > 0" by arith
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   693
  then show "b \<in># M - {#a#} \<longleftrightarrow> b \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   694
    using assms by (auto simp add: in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   695
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   696
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   697
lemma inter_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   698
  "a \<in># A #\<inter> B \<longleftrightarrow> a \<in># A \<and> a \<in># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   699
  by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   700
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   701
lemma inter_union_distrib_left:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   702
  "A #\<inter> B + C = (A + C) #\<inter> (B + C)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   703
  by (simp add: multiset_eq_iff min_add_distrib_left)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   704
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   705
lemma inter_union_distrib_right:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   706
  "C + A #\<inter> B = (C + A) #\<inter> (C + B)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   707
  using inter_union_distrib_left [of A B C] by (simp add: ac_simps)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   708
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   709
lemma inter_subset_eq_union:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   710
  "A #\<inter> B \<subseteq># A + B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   711
  by (auto simp add: subseteq_mset_def)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   712
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   713
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   714
subsubsection \<open>Bounded union\<close>
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   715
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   716
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "#\<union>" 70)
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   717
  where "sup_subset_mset A B = A + (B - A)" \<comment> \<open>FIXME irregular fact name\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   718
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   719
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<subseteq>#" "op \<subset>#"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   720
proof -
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   721
  have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   722
    by arith
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   723
  show "class.semilattice_sup op #\<union> op \<subseteq># op \<subset>#"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   724
    by standard (auto simp add: sup_subset_mset_def subseteq_mset_def)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   725
qed
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   726
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   727
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   728
lemma sup_subset_mset_count [simp]: \<comment> \<open>FIXME irregular fact name\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   729
  "count (A #\<union> B) x = max (count A x) (count B x)"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   730
  by (simp add: sup_subset_mset_def)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   731
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   732
lemma set_mset_sup [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   733
  "set_mset (A #\<union> B) = set_mset A \<union> set_mset B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   734
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] sup_subset_mset_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   735
    (auto simp add: not_in_iff elim: mset_add)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   736
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   737
lemma empty_sup [simp]: "{#} #\<union> M = M"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   738
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   739
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   740
lemma sup_empty [simp]: "M #\<union> {#} = M"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   741
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   742
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   743
lemma sup_union_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   744
  by (simp add: multiset_eq_iff not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   745
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   746
lemma sup_union_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   747
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   748
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   749
lemma sup_union_right1: "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   750
  by (simp add: multiset_eq_iff not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   751
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   752
lemma sup_union_right2: "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   753
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   754
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   755
lemma sup_union_distrib_left:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   756
  "A #\<union> B + C = (A + C) #\<union> (B + C)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   757
  by (simp add: multiset_eq_iff max_add_distrib_left)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   758
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   759
lemma union_sup_distrib_right:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   760
  "C + A #\<union> B = (C + A) #\<union> (C + B)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   761
  using sup_union_distrib_left [of A B C] by (simp add: ac_simps)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   762
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   763
lemma union_diff_inter_eq_sup:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   764
  "A + B - A #\<inter> B = A #\<union> B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   765
  by (auto simp add: multiset_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   766
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   767
lemma union_diff_sup_eq_inter:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   768
  "A + B - A #\<union> B = A #\<inter> B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   769
  by (auto simp add: multiset_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   770
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   771
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   772
subsubsection \<open>Subset is an order\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   773
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   774
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   775
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   776
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   777
subsubsection \<open>Filter (with comprehension syntax)\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   778
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   779
text \<open>Multiset comprehension\<close>
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   780
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   781
lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   782
is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   783
by (rule filter_preserves_multiset)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   784
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   785
syntax (ASCII)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   786
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   787
syntax
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   788
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   789
translations
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   790
  "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   791
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   792
lemma count_filter_mset [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   793
  "count (filter_mset P M) a = (if P a then count M a else 0)"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   794
  by (simp add: filter_mset.rep_eq)
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   795
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   796
lemma set_mset_filter [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   797
  "set_mset (filter_mset P M) = {a \<in> set_mset M. P a}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   798
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_filter_mset) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   799
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   800
lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   801
  by (rule multiset_eqI) simp
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   802
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   803
lemma filter_single_mset [simp]: "filter_mset P {#x#} = (if P x then {#x#} else {#})"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   804
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   805
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   806
lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N"
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   807
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   808
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   809
lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   810
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   811
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   812
lemma filter_inter_mset [simp]: "filter_mset P (M #\<inter> N) = filter_mset P M #\<inter> filter_mset P N"
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   813
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   814
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   815
lemma multiset_filter_subset[simp]: "filter_mset f M \<subseteq># M"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   816
  by (simp add: mset_less_eqI)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   817
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   818
lemma multiset_filter_mono:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   819
  assumes "A \<subseteq># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   820
  shows "filter_mset f A \<subseteq># filter_mset f B"
58035
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   821
proof -
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   822
  from assms[unfolded mset_le_exists_conv]
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   823
  obtain C where B: "B = A + C" by auto
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   824
  show ?thesis unfolding B by auto
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   825
qed
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   826
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   827
lemma filter_mset_eq_conv:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   828
  "filter_mset P M = N \<longleftrightarrow> N \<subseteq># M \<and> (\<forall>b\<in>#N. P b) \<and> (\<forall>a\<in>#M - N. \<not> P a)" (is "?P \<longleftrightarrow> ?Q")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   829
proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   830
  assume ?P then show ?Q by auto (simp add: multiset_eq_iff in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   831
next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   832
  assume ?Q
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   833
  then obtain Q where M: "M = N + Q"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   834
    by (auto simp add: mset_le_exists_conv)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   835
  then have MN: "M - N = Q" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   836
  show ?P
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   837
  proof (rule multiset_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   838
    fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   839
    from \<open>?Q\<close> MN have *: "\<not> P a \<Longrightarrow> a \<notin># N" "P a \<Longrightarrow> a \<notin># Q"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   840
      by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   841
    show "count (filter_mset P M) a = count N a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   842
    proof (cases "a \<in># M")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   843
      case True
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   844
      with * show ?thesis
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   845
        by (simp add: not_in_iff M)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   846
    next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   847
      case False then have "count M a = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   848
        by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   849
      with M show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   850
    qed 
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   851
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   852
qed
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   853
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   854
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   855
subsubsection \<open>Size\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   856
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   857
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   858
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   859
lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   860
  by (auto simp: wcount_def add_mult_distrib)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   861
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   862
definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   863
  "size_multiset f M = setsum (wcount f M) (set_mset M)"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   864
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   865
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   866
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   867
instantiation multiset :: (type) size
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   868
begin
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   869
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   870
definition size_multiset where
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   871
  size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   872
instance ..
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   873
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   874
end
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   875
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   876
lemmas size_multiset_overloaded_eq =
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   877
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   878
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   879
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   880
by (simp add: size_multiset_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   881
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   882
lemma size_empty [simp]: "size {#} = 0"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   883
by (simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   884
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   885
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   886
by (simp add: size_multiset_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   887
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   888
lemma size_single [simp]: "size {#b#} = 1"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   889
by (simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   890
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   891
lemma setsum_wcount_Int:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   892
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   893
  by (induct rule: finite_induct)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   894
    (simp_all add: Int_insert_left wcount_def count_eq_zero_iff)
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   895
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   896
lemma size_multiset_union [simp]:
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   897
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
57418
6ab1c7cb0b8d fact consolidation
haftmann
parents: 56656
diff changeset
   898
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union)
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   899
apply (subst Int_commute)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   900
apply (simp add: setsum_wcount_Int)
26178
nipkow
parents: 26176
diff changeset
   901
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   902
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   903
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   904
by (auto simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   905
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   906
lemma size_multiset_eq_0_iff_empty [iff]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   907
  "size_multiset f M = 0 \<longleftrightarrow> M = {#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   908
  by (auto simp add: size_multiset_eq count_eq_zero_iff)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   909
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   910
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   911
by (auto simp add: size_multiset_overloaded_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   912
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   913
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
26178
nipkow
parents: 26176
diff changeset
   914
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   915
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
   916
lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   917
apply (unfold size_multiset_overloaded_eq)
26178
nipkow
parents: 26176
diff changeset
   918
apply (drule setsum_SucD)
nipkow
parents: 26176
diff changeset
   919
apply auto
nipkow
parents: 26176
diff changeset
   920
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   921
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   922
lemma size_eq_Suc_imp_eq_union:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   923
  assumes "size M = Suc n"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   924
  shows "\<exists>a N. M = N + {#a#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   925
proof -
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   926
  from assms obtain a where "a \<in># M"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   927
    by (erule size_eq_Suc_imp_elem [THEN exE])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   928
  then have "M = M - {#a#} + {#a#}" by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   929
  then show ?thesis by blast
23611
65b168646309 more interpretations
nipkow
parents: 23373
diff changeset
   930
qed
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   931
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   932
lemma size_mset_mono:
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   933
  fixes A B :: "'a multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   934
  assumes "A \<subseteq># B"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   935
  shows "size A \<le> size B"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   936
proof -
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   937
  from assms[unfolded mset_le_exists_conv]
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   938
  obtain C where B: "B = A + C" by auto
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   939
  show ?thesis unfolding B by (induct C) auto
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   940
qed
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   941
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   942
lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   943
by (rule size_mset_mono[OF multiset_filter_subset])
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   944
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   945
lemma size_Diff_submset:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   946
  "M \<subseteq># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   947
by (metis add_diff_cancel_left' size_union mset_le_exists_conv)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   948
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   949
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   950
subsection \<open>Induction and case splits\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   951
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
   952
theorem multiset_induct [case_names empty add, induct type: multiset]:
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   953
  assumes empty: "P {#}"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   954
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   955
  shows "P M"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   956
proof (induct n \<equiv> "size M" arbitrary: M)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   957
  case 0 thus "P M" by (simp add: empty)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   958
next
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   959
  case (Suc k)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   960
  obtain N x where "M = N + {#x#}"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   961
    using \<open>Suc k = size M\<close> [symmetric]
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   962
    using size_eq_Suc_imp_eq_union by fast
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   963
  with Suc add show "P M" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   964
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   965
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   966
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
26178
nipkow
parents: 26176
diff changeset
   967
by (induct M) auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   968
55913
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   969
lemma multiset_cases [cases type]:
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   970
  obtains (empty) "M = {#}"
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   971
    | (add) N x where "M = N + {#x#}"
63092
a949b2a5f51d eliminated use of empty "assms";
wenzelm
parents: 63089
diff changeset
   972
  by (induct M) simp_all
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   973
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   974
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   975
by (cases "B = {#}") (auto dest: multi_member_split)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   976
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
   977
lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   978
apply (subst multiset_eq_iff)
26178
nipkow
parents: 26176
diff changeset
   979
apply auto
nipkow
parents: 26176
diff changeset
   980
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   981
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   982
lemma mset_less_size: "(A::'a multiset) \<subset># B \<Longrightarrow> size A < size B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   983
proof (induct A arbitrary: B)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   984
  case (empty M)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   985
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   986
  then obtain M' x where "M = M' + {#x#}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   987
    by (blast dest: multi_nonempty_split)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   988
  then show ?case by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   989
next
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   990
  case (add S x T)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   991
  have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   992
  have SxsubT: "S + {#x#} \<subset># T" by fact
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   993
  then have "x \<in># T" and "S \<subset># T"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   994
    by (auto dest: mset_less_insertD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   995
  then obtain T' where T: "T = T' + {#x#}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   996
    by (blast dest: multi_member_split)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   997
  then have "S \<subset># T'" using SxsubT
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   998
    by (blast intro: mset_less_add_bothsides)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   999
  then have "size S < size T'" using IH by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1000
  then show ?case using T by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1001
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1002
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1003
lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}"
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1004
by (cases M) auto
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1005
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1006
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1007
subsubsection \<open>Strong induction and subset induction for multisets\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1008
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1009
text \<open>Well-foundedness of strict subset relation\<close>
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
  1010
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1011
lemma wf_less_mset_rel: "wf {(M, N :: 'a multiset). M \<subset># N}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1012
apply (rule wf_measure [THEN wf_subset, where f1=size])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1013
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1014
done
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1015
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1016
lemma full_multiset_induct [case_names less]:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1017
assumes ih: "\<And>B. \<forall>(A::'a multiset). A \<subset># B \<longrightarrow> P A \<Longrightarrow> P B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1018
shows "P B"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
  1019
apply (rule wf_less_mset_rel [THEN wf_induct])
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
  1020
apply (rule ih, auto)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1021
done
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1022
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1023
lemma multi_subset_induct [consumes 2, case_names empty add]:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1024
  assumes "F \<subseteq># A"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1025
    and empty: "P {#}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1026
    and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1027
  shows "P F"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1028
proof -
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1029
  from \<open>F \<subseteq># A\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1030
  show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1031
  proof (induct F)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1032
    show "P {#}" by fact
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1033
  next
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1034
    fix x F
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1035
    assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "F + {#x#} \<subseteq># A"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1036
    show "P (F + {#x#})"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1037
    proof (rule insert)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1038
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1039
      from i have "F \<subseteq># A" by (auto dest: mset_le_insertD)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1040
      with P show "P F" .
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1041
    qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1042
  qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1043
qed
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1044
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1045
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1046
subsection \<open>The fold combinator\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1047
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1048
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1049
where
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1050
  "fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1051
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1052
lemma fold_mset_empty [simp]: "fold_mset f s {#} = s"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1053
  by (simp add: fold_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1054
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1055
context comp_fun_commute
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1056
begin
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1057
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1058
lemma fold_mset_insert: "fold_mset f s (M + {#x#}) = f x (fold_mset f s M)"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1059
proof -
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1060
  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1061
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1062
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1063
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1064
  show ?thesis
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1065
  proof (cases "x \<in> set_mset M")
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1066
    case False
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1067
    then have *: "count (M + {#x#}) x = 1"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1068
      by (simp add: not_in_iff)
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1069
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_mset M) =
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1070
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1071
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1072
    with False * show ?thesis
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1073
      by (simp add: fold_mset_def del: count_union)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1074
  next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1075
    case True
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62837
diff changeset
  1076
    define N where "N = set_mset M - {x}"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1077
    from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1078
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1079
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1080
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1081
    with * show ?thesis by (simp add: fold_mset_def del: count_union) simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1082
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1083
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1084
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1085
corollary fold_mset_single [simp]: "fold_mset f s {#x#} = f x s"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1086
proof -
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1087
  have "fold_mset f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1088
  then show ?thesis by simp
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1089
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1090
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1091
lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1092
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1093
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1094
lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1095
proof (induct M)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1096
  case empty then show ?case by simp
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1097
next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1098
  case (add M x)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1099
  have "M + {#x#} + N = (M + N) + {#x#}"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1100
    by (simp add: ac_simps)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1101
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1102
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1103
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1104
lemma fold_mset_fusion:
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1105
  assumes "comp_fun_commute g"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1106
    and *: "\<And>x y. h (g x y) = f x (h y)"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1107
  shows "h (fold_mset g w A) = fold_mset f (h w) A"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1108
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1109
  interpret comp_fun_commute g by (fact assms)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1110
  from * show ?thesis by (induct A) auto
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1111
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1112
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1113
end
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1114
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1115
text \<open>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1116
  A note on code generation: When defining some function containing a
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1117
  subterm @{term "fold_mset F"}, code generation is not automatic. When
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1118
  interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1119
  would be code thms for @{const fold_mset} become thms like
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1120
  @{term "fold_mset F z {#} = z"} where \<open>F\<close> is not a pattern but
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1121
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1122
  constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1123
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1124
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1125
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1126
subsection \<open>Image\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1127
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1128
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1129
  "image_mset f = fold_mset (plus \<circ> single \<circ> f) {#}"
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1130
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1131
lemma comp_fun_commute_mset_image: "comp_fun_commute (plus \<circ> single \<circ> f)"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1132
proof
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1133
qed (simp add: ac_simps fun_eq_iff)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1134
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1135
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1136
  by (simp add: image_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1137
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1138
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1139
proof -
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1140
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1141
    by (fact comp_fun_commute_mset_image)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1142
  show ?thesis by (simp add: image_mset_def)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1143
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1144
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1145
lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1146
proof -
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1147
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1148
    by (fact comp_fun_commute_mset_image)
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1149
  show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps)
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1150
qed
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1151
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1152
corollary image_mset_insert: "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1153
  by simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1154
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1155
lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1156
  by (induct M) simp_all
48040
4caf6cd063be add lemma set_of_image_mset
huffman
parents: 48023
diff changeset
  1157
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1158
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1159
  by (induct M) simp_all
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1160
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1161
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1162
  by (cases M) auto
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1163
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1164
lemma image_mset_If:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1165
  "image_mset (\<lambda>x. if P x then f x else g x) A = 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1166
     image_mset f (filter_mset P A) + image_mset g (filter_mset (\<lambda>x. \<not>P x) A)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1167
  by (induction A) (auto simp: add_ac)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1168
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1169
lemma image_mset_Diff: 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1170
  assumes "B \<subseteq># A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1171
  shows   "image_mset f (A - B) = image_mset f A - image_mset f B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1172
proof -
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1173
  have "image_mset f (A - B + B) = image_mset f (A - B) + image_mset f B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1174
    by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1175
  also from assms have "A - B + B = A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1176
    by (simp add: subset_mset.diff_add) 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1177
  finally show ?thesis by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1178
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1179
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1180
lemma count_image_mset: 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1181
  "count (image_mset f A) x = (\<Sum>y\<in>f -` {x} \<inter> set_mset A. count A y)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1182
  by (induction A)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1183
     (auto simp: setsum.distrib setsum.delta' intro!: setsum.mono_neutral_left)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1184
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1185
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1186
syntax (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1187
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1188
syntax
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1189
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1190
translations
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1191
  "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1192
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1193
syntax (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1194
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1195
syntax
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1196
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1197
translations
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1198
  "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1199
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1200
text \<open>
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1201
  This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"}
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1202
  but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source]
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1203
  "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1204
  @{term "{#x+x|x\<in>#M. x<c#}"}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1205
\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1206
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1207
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1208
by (metis set_image_mset)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1209
55467
a5c9002bc54d renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
blanchet
parents: 55417
diff changeset
  1210
functor image_mset: image_mset
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1211
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1212
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1213
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1214
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1215
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1216
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1217
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1218
  show "image_mset id = id"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1219
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1220
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1221
    show "image_mset id A = id A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1222
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1223
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1224
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1225
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1226
declare
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1227
  image_mset.id [simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1228
  image_mset.identity [simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1229
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1230
lemma image_mset_id[simp]: "image_mset id x = x"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1231
  unfolding id_def by auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1232
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1233
lemma image_mset_cong: "(\<And>x. x \<in># M \<Longrightarrow> f x = g x) \<Longrightarrow> {#f x. x \<in># M#} = {#g x. x \<in># M#}"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1234
  by (induct M) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1235
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1236
lemma image_mset_cong_pair:
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1237
  "(\<forall>x y. (x, y) \<in># M \<longrightarrow> f x y = g x y) \<Longrightarrow> {#f x y. (x, y) \<in># M#} = {#g x y. (x, y) \<in># M#}"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1238
  by (metis image_mset_cong split_cong)
49717
56494eedf493 default simp rule for image under identity
haftmann
parents: 49394
diff changeset
  1239
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1240
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1241
subsection \<open>Further conversions\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1242
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1243
primrec mset :: "'a list \<Rightarrow> 'a multiset" where
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1244
  "mset [] = {#}" |
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1245
  "mset (a # x) = mset x + {# a #}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1246
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1247
lemma in_multiset_in_set:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1248
  "x \<in># mset xs \<longleftrightarrow> x \<in> set xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1249
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1250
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1251
lemma count_mset:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1252
  "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1253
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1254
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1255
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1256
  by (induct x) auto
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1257
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1258
lemma mset_zero_iff_right[simp]: "({#} = mset x) = (x = [])"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1259
by (induct x) auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1260
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1261
lemma set_mset_mset[simp]: "set_mset (mset x) = set x"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1262
by (induct x) auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1263
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1264
lemma set_mset_comp_mset [simp]: "set_mset \<circ> mset = set"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1265
  by (simp add: fun_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1266
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1267
lemma size_mset [simp]: "size (mset xs) = length xs"
48012
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
  1268
  by (induct xs) simp_all
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
  1269
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1270
lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1271
  by (induct xs arbitrary: ys) (auto simp: ac_simps)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1272
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1273
lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1274
  by (induct xs) simp_all
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1275
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1276
lemma mset_rev [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1277
  "mset (rev xs) = mset xs"
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
  1278
  by (induct xs) simp_all
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
  1279
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1280
lemma surj_mset: "surj mset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1281
apply (unfold surj_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1282
apply (rule allI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1283
apply (rule_tac M = y in multiset_induct)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1284
 apply auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1285
apply (rule_tac x = "x # xa" in exI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1286
apply auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1287
done
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1288
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1289
lemma distinct_count_atmost_1:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1290
  "distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1291
proof (induct x)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1292
  case Nil then show ?case by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1293
next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1294
  case (Cons x xs) show ?case (is "?lhs \<longleftrightarrow> ?rhs")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1295
  proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1296
    assume ?lhs then show ?rhs using Cons by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1297
  next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1298
    assume ?rhs then have "x \<notin> set xs"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1299
      by (simp split: if_splits)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1300
    moreover from \<open>?rhs\<close> have "(\<forall>a. count (mset xs) a =
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1301
       (if a \<in> set xs then 1 else 0))"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1302
      by (auto split: if_splits simp add: count_eq_zero_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1303
    ultimately show ?lhs using Cons by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1304
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1305
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1306
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1307
lemma mset_eq_setD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1308
  assumes "mset xs = mset ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1309
  shows "set xs = set ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1310
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1311
  from assms have "set_mset (mset xs) = set_mset (mset ys)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1312
    by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1313
  then show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1314
qed
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1315
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1316
lemma set_eq_iff_mset_eq_distinct:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1317
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1318
    (set x = set y) = (mset x = mset y)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1319
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1320
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1321
lemma set_eq_iff_mset_remdups_eq:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1322
   "(set x = set y) = (mset (remdups x) = mset (remdups y))"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1323
apply (rule iffI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1324
apply (simp add: set_eq_iff_mset_eq_distinct[THEN iffD1])
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1325
apply (drule distinct_remdups [THEN distinct_remdups
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1326
      [THEN set_eq_iff_mset_eq_distinct [THEN iffD2]]])
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1327
apply simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1328
done
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1329
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1330
lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1331
  by (induct xs) (auto simp: ac_simps)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1332
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1333
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1334
proof (induct ls arbitrary: i)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1335
  case Nil
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1336
  then show ?case by simp
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1337
next
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1338
  case Cons
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1339
  then show ?case by (cases i) auto
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1340
qed
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1341
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1342
lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1343
  by (induct xs) (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1344
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1345
lemma mset_eq_length:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1346
  assumes "mset xs = mset ys"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1347
  shows "length xs = length ys"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1348
  using assms by (metis size_mset)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1349
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1350
lemma mset_eq_length_filter:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1351
  assumes "mset xs = mset ys"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1352
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1353
  using assms by (metis count_mset)
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1354
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1355
lemma fold_multiset_equiv:
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1356
  assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1357
    and equiv: "mset xs = mset ys"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1358
  shows "List.fold f xs = List.fold f ys"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1359
  using f equiv [symmetric]
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1360
proof (induct xs arbitrary: ys)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1361
  case Nil
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1362
  then show ?case by simp
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1363
next
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1364
  case (Cons x xs)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1365
  then have *: "set ys = set (x # xs)"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1366
    by (blast dest: mset_eq_setD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1367
  have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1368
    by (rule Cons.prems(1)) (simp_all add: *)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1369
  moreover from * have "x \<in> set ys"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1370
    by simp
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1371
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1372
    by (fact fold_remove1_split)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1373
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1374
    by (auto intro: Cons.hyps)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1375
  ultimately show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1376
qed
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1377
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1378
lemma mset_insort [simp]: "mset (insort x xs) = mset xs + {#x#}"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1379
  by (induct xs) (simp_all add: ac_simps)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1380
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1381
lemma mset_map: "mset (map f xs) = image_mset f (mset xs)"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1382
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1383
61890
f6ded81f5690 abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents: 61832
diff changeset
  1384
global_interpretation mset_set: folding "\<lambda>x M. {#x#} + M" "{#}"
61832
e15880ba58ac modernized
haftmann
parents: 61605
diff changeset
  1385
  defines mset_set = "folding.F (\<lambda>x M. {#x#} + M) {#}"
e15880ba58ac modernized
haftmann
parents: 61605
diff changeset
  1386
  by standard (simp add: fun_eq_iff ac_simps)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1387
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1388
lemma count_mset_set [simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1389
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P")
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1390
  "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1391
  "x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R")
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1392
proof -
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1393
  have *: "count (mset_set A) x = 0" if "x \<notin> A" for A
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1394
  proof (cases "finite A")
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1395
    case False then show ?thesis by simp
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1396
  next
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1397
    case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1398
  qed
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1399
  then show "PROP ?P" "PROP ?Q" "PROP ?R"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1400
  by (auto elim!: Set.set_insert)
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1401
qed \<comment> \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1402
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1403
lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1404
  by (induct A rule: finite_induct) simp_all
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1405
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1406
lemma mset_set_Union: 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1407
  "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> mset_set (A \<union> B) = mset_set A + mset_set B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1408
  by (induction A rule: finite_induct) (auto simp: add_ac)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1409
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1410
lemma filter_mset_mset_set [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1411
  "finite A \<Longrightarrow> filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1412
proof (induction A rule: finite_induct)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1413
  case (insert x A)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1414
  from insert.hyps have "filter_mset P (mset_set (insert x A)) = 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1415
      filter_mset P (mset_set A) + mset_set (if P x then {x} else {})"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1416
    by (simp add: add_ac)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1417
  also have "filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1418
    by (rule insert.IH)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1419
  also from insert.hyps 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1420
    have "\<dots> + mset_set (if P x then {x} else {}) =
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1421
            mset_set ({x \<in> A. P x} \<union> (if P x then {x} else {}))" (is "_ = mset_set ?A")
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1422
     by (intro mset_set_Union [symmetric]) simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1423
  also from insert.hyps have "?A = {y\<in>insert x A. P y}" by auto
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1424
  finally show ?case .
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1425
qed simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1426
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1427
lemma mset_set_Diff:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1428
  assumes "finite A" "B \<subseteq> A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1429
  shows  "mset_set (A - B) = mset_set A - mset_set B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1430
proof -
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1431
  from assms have "mset_set ((A - B) \<union> B) = mset_set (A - B) + mset_set B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1432
    by (intro mset_set_Union) (auto dest: finite_subset)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1433
  also from assms have "A - B \<union> B = A" by blast
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1434
  finally show ?thesis by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1435
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1436
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1437
lemma mset_set_set: "distinct xs \<Longrightarrow> mset_set (set xs) = mset xs"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1438
  by (induction xs) (simp_all add: add_ac)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1439
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1440
context linorder
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1441
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1442
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1443
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1444
where
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1445
  "sorted_list_of_multiset M = fold_mset insort [] M"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1446
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1447
lemma sorted_list_of_multiset_empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1448
  "sorted_list_of_multiset {#} = []"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1449
  by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1450
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1451
lemma sorted_list_of_multiset_singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1452
  "sorted_list_of_multiset {#x#} = [x]"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1453
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1454
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1455
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1456
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1457
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1458
lemma sorted_list_of_multiset_insert [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1459
  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1460
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1461
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1462
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1463
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1464
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1465
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1466
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1467
lemma mset_sorted_list_of_multiset [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1468
  "mset (sorted_list_of_multiset M) = M"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1469
by (induct M) simp_all
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1470
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1471
lemma sorted_list_of_multiset_mset [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1472
  "sorted_list_of_multiset (mset xs) = sort xs"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1473
by (induct xs) simp_all
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1474
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1475
lemma finite_set_mset_mset_set[simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1476
  "finite A \<Longrightarrow> set_mset (mset_set A) = A"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1477
by (induct A rule: finite_induct) simp_all
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1478
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1479
lemma mset_set_empty_iff: "mset_set A = {#} \<longleftrightarrow> A = {} \<or> infinite A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1480
  using finite_set_mset_mset_set by fastforce
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1481
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1482
lemma infinite_set_mset_mset_set:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1483
  "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1484
by simp
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1485
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1486
lemma set_sorted_list_of_multiset [simp]:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1487
  "set (sorted_list_of_multiset M) = set_mset M"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1488
by (induct M) (simp_all add: set_insort)
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1489
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1490
lemma sorted_list_of_mset_set [simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1491
  "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1492
by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1493
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1494
lemma mset_upt [simp]: "mset [m..<n] = mset_set {m..<n}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1495
  by (induction n) (simp_all add: atLeastLessThanSuc add_ac)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1496
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1497
lemma image_mset_map_of: 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1498
  "distinct (map fst xs) \<Longrightarrow> {#the (map_of xs i). i \<in># mset (map fst xs)#} = mset (map snd xs)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1499
proof (induction xs)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1500
  case (Cons x xs)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1501
  have "{#the (map_of (x # xs) i). i \<in># mset (map fst (x # xs))#} = 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1502
          {#the (if i = fst x then Some (snd x) else map_of xs i). 
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1503
             i \<in># mset (map fst xs)#} + {#snd x#}" (is "_ = ?A + _") by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1504
  also from Cons.prems have "?A = {#the (map_of xs i). i :# mset (map fst xs)#}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1505
    by (cases x, intro image_mset_cong) (auto simp: in_multiset_in_set)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1506
  also from Cons.prems have "\<dots> = mset (map snd xs)" by (intro Cons.IH) simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1507
  finally show ?case by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1508
qed simp_all  
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1509
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1510
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1511
subsection \<open>Replicate operation\<close>
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1512
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1513
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1514
  "replicate_mset n x = ((op + {#x#}) ^^ n) {#}"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1515
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1516
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1517
  unfolding replicate_mset_def by simp
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1518
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1519
lemma replicate_mset_Suc[simp]: "replicate_mset (Suc n) x = replicate_mset n x + {#x#}"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1520
  unfolding replicate_mset_def by (induct n) (auto intro: add.commute)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1521
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1522
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1523
  unfolding replicate_mset_def by (induct n) auto
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1524
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1525
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1526
  unfolding replicate_mset_def by (induct n) simp_all
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1527
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1528
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1529
  by (auto split: if_splits)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1530
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1531
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1532
  by (induct n, simp_all)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1533
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1534
lemma count_le_replicate_mset_le: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<subseteq># M"
63092
a949b2a5f51d eliminated use of empty "assms";
wenzelm
parents: 63089
diff changeset
  1535
  by (auto simp add: mset_less_eqI) (metis count_replicate_mset subseteq_mset_def)
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1536
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1537
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1538
  by (induct D) simp_all
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1539
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1540
lemma replicate_count_mset_eq_filter_eq:
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1541
  "replicate (count (mset xs) k) k = filter (HOL.eq k) xs"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1542
  by (induct xs) auto
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1543
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1544
lemma replicate_mset_eq_empty_iff [simp]:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1545
  "replicate_mset n a = {#} \<longleftrightarrow> n = 0"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1546
  by (induct n) simp_all
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1547
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1548
lemma replicate_mset_eq_iff:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1549
  "replicate_mset m a = replicate_mset n b \<longleftrightarrow>
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1550
    m = 0 \<and> n = 0 \<or> m = n \<and> a = b"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1551
  by (auto simp add: multiset_eq_iff)
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1552
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1553
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1554
subsection \<open>Big operators\<close>
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1555
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1556
no_notation times (infixl "*" 70)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1557
no_notation Groups.one ("1")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1558
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1559
locale comm_monoid_mset = comm_monoid
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1560
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1561
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1562
definition F :: "'a multiset \<Rightarrow> 'a"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1563
  where eq_fold: "F M = fold_mset f 1 M"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1564
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1565
lemma empty [simp]: "F {#} = 1"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1566
  by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1567
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1568
lemma singleton [simp]: "F {#x#} = x"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1569
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1570
  interpret comp_fun_commute
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1571
    by standard (simp add: fun_eq_iff left_commute)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1572
  show ?thesis by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1573
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1574
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1575
lemma union [simp]: "F (M + N) = F M * F N"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1576
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1577
  interpret comp_fun_commute f
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1578
    by standard (simp add: fun_eq_iff left_commute)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1579
  show ?thesis
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1580
    by (induct N) (simp_all add: left_commute eq_fold)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1581
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1582
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1583
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1584
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 61031
diff changeset
  1585
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1586
  by standard (simp add: add_ac comp_def)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1587
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1588
declare comp_fun_commute.fold_mset_insert[OF comp_fun_commute_plus_mset, simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1589
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1590
lemma in_mset_fold_plus_iff[iff]: "x \<in># fold_mset (op +) M NN \<longleftrightarrow> x \<in># M \<or> (\<exists>N. N \<in># NN \<and> x \<in># N)"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1591
  by (induct NN) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1592
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1593
notation times (infixl "*" 70)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1594
notation Groups.one ("1")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1595
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1596
context comm_monoid_add
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1597
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1598
61605
1bf7b186542e qualifier is mandatory by default;
wenzelm
parents: 61585
diff changeset
  1599
sublocale msetsum: comm_monoid_mset plus 0
61832
e15880ba58ac modernized
haftmann
parents: 61605
diff changeset
  1600
  defines msetsum = msetsum.F ..
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1601
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1602
lemma (in semiring_1) msetsum_replicate_mset [simp]:
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1603
  "msetsum (replicate_mset n a) = of_nat n * a"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1604
  by (induct n) (simp_all add: algebra_simps)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1605
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1606
lemma setsum_unfold_msetsum:
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1607
  "setsum f A = msetsum (image_mset f (mset_set A))"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1608
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1609
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1610
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1611
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1612
lemma msetsum_diff:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 61031
diff changeset
  1613
  fixes M N :: "('a :: ordered_cancel_comm_monoid_diff) multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1614
  shows "N \<subseteq># M \<Longrightarrow> msetsum (M - N) = msetsum M - msetsum N"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1615
  by (metis add_diff_cancel_right' msetsum.union subset_mset.diff_add)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1616
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1617
lemma size_eq_msetsum: "size M = msetsum (image_mset (\<lambda>_. 1) M)"
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1618
proof (induct M)
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1619
  case empty then show ?case by simp
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1620
next
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1621
  case (add M x) then show ?case
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1622
    by (cases "x \<in> set_mset M")
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1623
      (simp_all add: size_multiset_overloaded_eq setsum.distrib setsum.delta' insert_absorb not_in_iff)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1624
qed
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1625
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1626
lemma size_mset_set [simp]: "size (mset_set A) = card A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1627
  by (simp only: size_eq_msetsum card_eq_setsum setsum_unfold_msetsum)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1628
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1629
syntax (ASCII)
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1630
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1631
syntax
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1632
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1633
translations
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1634
  "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST msetsum (CONST image_mset (\<lambda>i. b) A)"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1635
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1636
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
  1637
  where "\<Union># MM \<equiv> msetsum MM" \<comment> \<open>FIXME ambiguous notation --
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
  1638
    could likewise refer to \<open>\<Squnion>#\<close>\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1639
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1640
lemma set_mset_Union_mset[simp]: "set_mset (\<Union># MM) = (\<Union>M \<in> set_mset MM. set_mset M)"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1641
  by (induct MM) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1642
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1643
lemma in_Union_mset_iff[iff]: "x \<in># \<Union># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1644
  by (induct MM) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1645
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1646
lemma count_setsum:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1647
  "count (setsum f A) x = setsum (\<lambda>a. count (f a) x) A"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1648
  by (induct A rule: infinite_finite_induct) simp_all
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1649
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1650
lemma setsum_eq_empty_iff:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1651
  assumes "finite A"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1652
  shows "setsum f A = {#} \<longleftrightarrow> (\<forall>a\<in>A. f a = {#})"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1653
  using assms by induct simp_all
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1654
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1655
context comm_monoid_mult
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1656
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1657
61605
1bf7b186542e qualifier is mandatory by default;
wenzelm
parents: 61585
diff changeset
  1658
sublocale msetprod: comm_monoid_mset times 1
61832
e15880ba58ac modernized
haftmann
parents: 61605
diff changeset
  1659
  defines msetprod = msetprod.F ..
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1660
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1661
lemma msetprod_empty:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1662
  "msetprod {#} = 1"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1663
  by (fact msetprod.empty)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1664
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1665
lemma msetprod_singleton:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1666
  "msetprod {#x#} = x"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1667
  by (fact msetprod.singleton)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1668
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1669
lemma msetprod_Un:
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1670
  "msetprod (A + B) = msetprod A * msetprod B"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1671
  by (fact msetprod.union)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1672
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1673
lemma msetprod_replicate_mset [simp]:
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1674
  "msetprod (replicate_mset n a) = a ^ n"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1675
  by (induct n) (simp_all add: ac_simps)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  1676
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1677
lemma setprod_unfold_msetprod:
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1678
  "setprod f A = msetprod (image_mset f (mset_set A))"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1679
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1680
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1681
lemma msetprod_multiplicity:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1682
  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1683
  by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1684
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1685
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1686
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1687
syntax (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1688
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3PROD _:#_. _)" [0, 51, 10] 10)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1689
syntax
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1690
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1691
translations
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1692
  "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST msetprod (CONST image_mset (\<lambda>i. b) A)"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1693
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1694
lemma (in comm_semiring_1) dvd_msetprod:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1695
  assumes "x \<in># A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1696
  shows "x dvd msetprod A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1697
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1698
  from assms have "A = (A - {#x#}) + {#x#}" by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1699
  then obtain B where "A = B + {#x#}" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1700
  then show ?thesis by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1701
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1702
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1703
lemma (in semidom) msetprod_zero_iff [iff]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1704
  "msetprod A = 0 \<longleftrightarrow> 0 \<in># A"
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1705
  by (induct A) auto
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  1706
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1707
lemma (in semidom_divide) msetprod_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1708
  assumes "B \<subseteq># A" and "0 \<notin># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1709
  shows "msetprod (A - B) = msetprod A div msetprod B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1710
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1711
  from assms obtain C where "A = B + C"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1712
    by (metis subset_mset.add_diff_inverse)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1713
  with assms show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1714
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1715
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1716
lemma (in semidom_divide) msetprod_minus:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1717
  assumes "a \<in># A" and "a \<noteq> 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1718
  shows "msetprod (A - {#a#}) = msetprod A div a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1719
  using assms msetprod_diff [of "{#a#}" A]
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1720
    by (auto simp add: single_subset_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1721
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1722
lemma (in normalization_semidom) normalized_msetprodI:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1723
  assumes "\<And>a. a \<in># A \<Longrightarrow> normalize a = a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1724
  shows "normalize (msetprod A) = msetprod A"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1725
  using assms by (induct A) (simp_all add: normalize_mult)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1726
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1727
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1728
subsection \<open>Alternative representations\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1729
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1730
subsubsection \<open>Lists\<close>
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1731
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1732
context linorder
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1733
begin
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1734
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1735
lemma mset_insort [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1736
  "mset (insort_key k x xs) = {#x#} + mset xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1737
  by (induct xs) (simp_all add: ac_simps)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1738
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1739
lemma mset_sort [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1740
  "mset (sort_key k xs) = mset xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1741
  by (induct xs) (simp_all add: ac_simps)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1742
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1743
text \<open>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1744
  This lemma shows which properties suffice to show that a function
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1745
  \<open>f\<close> with \<open>f xs = ys\<close> behaves like sort.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1746
\<close>
37074
322d065ebef7 localized properties_for_sort
haftmann
parents: 36903
diff changeset
  1747
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1748
lemma properties_for_sort_key:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1749
  assumes "mset ys = mset xs"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1750
    and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1751
    and "sorted (map f ys)"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1752
  shows "sort_key f xs = ys"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1753
  using assms
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1754
proof (induct xs arbitrary: ys)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1755
  case Nil then show ?case by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1756
next
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1757
  case (Cons x xs)
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1758
  from Cons.prems(2) have
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1759
    "\<forall>k \<in> set ys. filter (\<lambda>x. f k = f x) (remove1 x ys) = filter (\<lambda>x. f k = f x) xs"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1760
    by (simp add: filter_remove1)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1761
  with Cons.prems have "sort_key f xs = remove1 x ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1762
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1763
  moreover from Cons.prems have "x \<in># mset ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1764
    by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1765
  then have "x \<in> set ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1766
    by simp
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1767
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1768
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1769
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1770
lemma properties_for_sort:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1771
  assumes multiset: "mset ys = mset xs"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1772
    and "sorted ys"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1773
  shows "sort xs = ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1774
proof (rule properties_for_sort_key)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1775
  from multiset show "mset ys = mset xs" .
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1776
  from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1777
  from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1778
    by (rule mset_eq_length_filter)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1779
  then have "replicate (length (filter (\<lambda>y. k = y) ys)) k =
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1780
    replicate (length (filter (\<lambda>x. k = x) xs)) k" for k
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1781
    by simp
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1782
  then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1783
    by (simp add: replicate_length_filter)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1784
qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1785
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1786
lemma sort_key_inj_key_eq:
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1787
  assumes mset_equal: "mset xs = mset ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1788
    and "inj_on f (set xs)"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1789
    and "sorted (map f ys)"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1790
  shows "sort_key f xs = ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1791
proof (rule properties_for_sort_key)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1792
  from mset_equal
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1793
  show "mset ys = mset xs" by simp
61188
b34551d94934 isabelle update_cartouches;
wenzelm
parents: 61076
diff changeset
  1794
  from \<open>sorted (map f ys)\<close>
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1795
  show "sorted (map f ys)" .
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1796
  show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1797
  proof -
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1798
    from mset_equal
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1799
    have set_equal: "set xs = set ys" by (rule mset_eq_setD)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1800
    with that have "insert k (set ys) = set ys" by auto
61188
b34551d94934 isabelle update_cartouches;
wenzelm
parents: 61076
diff changeset
  1801
    with \<open>inj_on f (set xs)\<close> have inj: "inj_on f (insert k (set ys))"
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1802
      by (simp add: set_equal)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1803
    from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1804
      by (auto intro!: inj_on_filter_key_eq)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1805
    also have "\<dots> = replicate (count (mset ys) k) k"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1806
      by (simp add: replicate_count_mset_eq_filter_eq)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1807
    also have "\<dots> = replicate (count (mset xs) k) k"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1808
      using mset_equal by simp
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1809
    also have "\<dots> = filter (HOL.eq k) xs"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1810
      by (simp add: replicate_count_mset_eq_filter_eq)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1811
    also have "\<dots> = [x\<leftarrow>xs . f k = f x]"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1812
      using inj by (auto intro!: inj_on_filter_key_eq [symmetric] simp add: set_equal)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1813
    finally show ?thesis .
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1814
  qed
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1815
qed
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1816
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1817
lemma sort_key_eq_sort_key:
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1818
  assumes "mset xs = mset ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1819
    and "inj_on f (set xs)"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1820
  shows "sort_key f xs = sort_key f ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1821
  by (rule sort_key_inj_key_eq) (simp_all add: assms)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  1822
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1823
lemma sort_key_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1824
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1825
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1826
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1827
proof (rule properties_for_sort_key)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1828
  show "mset ?rhs = mset ?lhs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1829
    by (rule multiset_eqI) (auto simp add: mset_filter)
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1830
  show "sorted (map f ?rhs)"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1831
    by (auto simp add: sorted_append intro: sorted_map_same)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1832
next
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1833
  fix l
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1834
  assume "l \<in> set ?rhs"
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1835
  let ?pivot = "f (xs ! (length xs div 2))"
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1836
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1837
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1838
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1839
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1840
  have "\<And>x P. P (f x) ?pivot \<and> f l = f x \<longleftrightarrow> P (f l) ?pivot \<and> f l = f x" by auto
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1841
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1842
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1843
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1844
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1845
  proof (cases "f l" ?pivot rule: linorder_cases)
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1846
    case less
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1847
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1848
    with less show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1849
      by (simp add: filter_sort [symmetric] ** ***)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1850
  next
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1851
    case equal then show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1852
      by (simp add: * less_le)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1853
  next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1854
    case greater
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1855
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1856
    with greater show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1857
      by (simp add: filter_sort [symmetric] ** ***)
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1858
  qed
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1859
qed
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1860
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1861
lemma sort_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1862
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1863
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1864
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1865
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1866
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1867
text \<open>A stable parametrized quicksort\<close>
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1868
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1869
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1870
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1871
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1872
lemma part_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1873
  "part f pivot [] = ([], [], [])"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1874
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1875
     if x' < pivot then (x # lts, eqs, gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1876
     else if x' > pivot then (lts, eqs, x # gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1877
     else (lts, x # eqs, gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1878
  by (auto simp add: part_def Let_def split_def)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1879
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1880
lemma sort_key_by_quicksort_code [code]:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1881
  "sort_key f xs =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1882
    (case xs of
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1883
      [] \<Rightarrow> []
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1884
    | [x] \<Rightarrow> xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1885
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1886
    | _ \<Rightarrow>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1887
        let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1888
        in sort_key f lts @ eqs @ sort_key f gts)"
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1889
proof (cases xs)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1890
  case Nil then show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1891
next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1892
  case (Cons _ ys) note hyps = Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1893
  proof (cases ys)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1894
    case Nil with hyps show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1895
  next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1896
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1897
    proof (cases zs)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1898
      case Nil with hyps show ?thesis by auto
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1899
    next
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1900
      case Cons
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1901
      from sort_key_by_quicksort [of f xs]
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1902
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1903
        in sort_key f lts @ eqs @ sort_key f gts)"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1904
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1905
      with hyps Cons show ?thesis by (simp only: list.cases)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1906
    qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1907
  qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1908
qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1909
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1910
end
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1911
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1912
hide_const (open) part
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1913
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1914
lemma mset_remdups_le: "mset (remdups xs) \<subseteq># mset xs"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1915
  by (induct xs) (auto intro: subset_mset.order_trans)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1916
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1917
lemma mset_update:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1918
  "i < length ls \<Longrightarrow> mset (ls[i := v]) = mset ls - {#ls ! i#} + {#v#}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1919
proof (induct ls arbitrary: i)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1920
  case Nil then show ?case by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1921
next
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1922
  case (Cons x xs)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1923
  show ?case
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1924
  proof (cases i)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1925
    case 0 then show ?thesis by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1926
  next
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1927
    case (Suc i')
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1928
    with Cons show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1929
      apply simp
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1930
      apply (subst add.assoc)
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1931
      apply (subst add.commute [of "{#v#}" "{#x#}"])
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1932
      apply (subst add.assoc [symmetric])
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1933
      apply simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1934
      apply (rule mset_le_multiset_union_diff_commute)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1935
      apply (simp add: mset_le_single nth_mem_mset)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1936
      done
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1937
  qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1938
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1939
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1940
lemma mset_swap:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1941
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1942
    mset (ls[j := ls ! i, i := ls ! j]) = mset ls"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1943
  by (cases "i = j") (simp_all add: mset_update nth_mem_mset)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1944
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1945
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1946
subsection \<open>The multiset order\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1947
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1948
subsubsection \<open>Well-foundedness\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1949
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1950
definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1951
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1952
      (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1953
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1954
definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1955
  "mult r = (mult1 r)\<^sup>+"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1956
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1957
lemma mult1I:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1958
  assumes "M = M0 + {#a#}" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1959
  shows "(N, M) \<in> mult1 r"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1960
  using assms unfolding mult1_def by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1961
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1962
lemma mult1E:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1963
  assumes "(N, M) \<in> mult1 r"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1964
  obtains a M0 K where "M = M0 + {#a#}" "N = M0 + K" "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1965
  using assms unfolding mult1_def by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1966
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1967
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1968
by (simp add: mult1_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1969
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1970
lemma less_add:
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1971
  assumes mult1: "(N, M0 + {#a#}) \<in> mult1 r"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1972
  shows
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1973
    "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1974
     (\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1975
proof -
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1976
  let ?r = "\<lambda>K a. \<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r"
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1977
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1978
  obtain a' M0' K where M0: "M0 + {#a#} = M0' + {#a'#}"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1979
    and N: "N = M0' + K"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1980
    and r: "?r K a'"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1981
    using mult1 unfolding mult1_def by auto
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1982
  show ?thesis (is "?case1 \<or> ?case2")
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1983
  proof -
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1984
    from M0 consider "M0 = M0'" "a = a'"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1985
      | K' where "M0 = K' + {#a'#}" "M0' = K' + {#a#}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1986
      by atomize_elim (simp only: add_eq_conv_ex)
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1987
    then show ?thesis
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1988
    proof cases
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1989
      case 1
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1990
      with N r have "?r K a \<and> N = M0 + K" by simp
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1991
      then have ?case2 ..
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1992
      then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1993
    next
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1994
      case 2
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1995
      from N 2(2) have n: "N = K' + K + {#a#}" by (simp add: ac_simps)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1996
      with r 2(1) have "?R (K' + K) M0" by blast
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  1997
      with n have ?case1 by (simp add: mult1_def)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1998
      then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1999
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2000
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2001
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2002
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2003
lemma all_accessible:
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2004
  assumes "wf r"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2005
  shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2006
proof
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2007
  let ?R = "mult1 r"
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 52289
diff changeset
  2008
  let ?W = "Wellfounded.acc ?R"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2009
  {
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2010
    fix M M0 a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2011
    assume M0: "M0 \<in> ?W"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2012
      and wf_hyp: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2013
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2014
    have "M0 + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2015
    proof (rule accI [of "M0 + {#a#}"])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2016
      fix N
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2017
      assume "(N, M0 + {#a#}) \<in> ?R"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2018
      then consider M where "(M, M0) \<in> ?R" "N = M + {#a#}"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2019
        | K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2020
        by atomize_elim (rule less_add)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2021
      then show "N \<in> ?W"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2022
      proof cases
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2023
        case 1
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2024
        from acc_hyp have "(M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W" ..
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2025
        from this and \<open>(M, M0) \<in> ?R\<close> have "M + {#a#} \<in> ?W" ..
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2026
        then show "N \<in> ?W" by (simp only: \<open>N = M + {#a#}\<close>)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2027
      next
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2028
        case 2
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2029
        from this(1) have "M0 + K \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2030
        proof (induct K)
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  2031
          case empty
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2032
          from M0 show "M0 + {#} \<in> ?W" by simp
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  2033
        next
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  2034
          case (add K x)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2035
          from add.prems have "(x, a) \<in> r" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2036
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2037
          moreover from add have "M0 + K \<in> ?W" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2038
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2039
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add.assoc)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2040
        qed
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2041
        then show "N \<in> ?W" by (simp only: 2(2))
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2042
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2043
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2044
  } note tedious_reasoning = this
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2045
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2046
  show "M \<in> ?W" for M
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2047
  proof (induct M)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2048
    show "{#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2049
    proof (rule accI)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2050
      fix b assume "(b, {#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2051
      with not_less_empty show "b \<in> ?W" by contradiction
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2052
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2053
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2054
    fix M a assume "M \<in> ?W"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2055
    from \<open>wf r\<close> have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2056
    proof induct
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2057
      fix a
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2058
      assume r: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2059
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2060
      proof
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2061
        fix M assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2062
        then show "M + {#a#} \<in> ?W"
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 23281
diff changeset
  2063
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2064
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2065
    qed
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2066
    from this and \<open>M \<in> ?W\<close> show "M + {#a#} \<in> ?W" ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2067
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2068
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2069
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2070
theorem wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)"
26178
nipkow
parents: 26176
diff changeset
  2071
by (rule acc_wfI) (rule all_accessible)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2072
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2073
theorem wf_mult: "wf r \<Longrightarrow> wf (mult r)"
26178
nipkow
parents: 26176
diff changeset
  2074
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2075
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2076
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2077
subsubsection \<open>Closure-free presentation\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2078
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2079
text \<open>One direction.\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2080
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2081
lemma mult_implies_one_step:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2082
  "trans r \<Longrightarrow> (M, N) \<in> mult r \<Longrightarrow>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  2083
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2084
    (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2085
apply (unfold mult_def mult1_def)
26178
nipkow
parents: 26176
diff changeset
  2086
apply (erule converse_trancl_induct, clarify)
nipkow
parents: 26176
diff changeset
  2087
 apply (rule_tac x = M0 in exI, simp, clarify)
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  2088
apply (case_tac "a \<in># K")
26178
nipkow
parents: 26176
diff changeset
  2089
 apply (rule_tac x = I in exI)
nipkow
parents: 26176
diff changeset
  2090
 apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  2091
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2092
 apply (simp (no_asm_simp) add: add.assoc [symmetric])
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59625
diff changeset
  2093
 apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong)
26178
nipkow
parents: 26176
diff changeset
  2094
 apply (simp add: diff_union_single_conv)
nipkow
parents: 26176
diff changeset
  2095
 apply (simp (no_asm_use) add: trans_def)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2096
 apply (metis (no_types, hide_lams) Multiset.diff_right_commute Un_iff diff_single_trivial multi_drop_mem_not_eq)
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  2097
apply (subgoal_tac "a \<in># I")
26178
nipkow
parents: 26176
diff changeset
  2098
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  2099
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  2100
 apply (rule_tac x = "K + Ka" in exI)
nipkow
parents: 26176
diff changeset
  2101
 apply (rule conjI)
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  2102
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  2103
 apply (rule conjI)
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59625
diff changeset
  2104
  apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong, simp)
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  2105
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  2106
 apply (simp (no_asm_use) add: trans_def)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2107
apply (subgoal_tac "a \<in># (M0 + {#a#})")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2108
 apply (simp_all add: not_in_iff)
26178
nipkow
parents: 26176
diff changeset
  2109
 apply blast
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2110
 apply (metis add.comm_neutral add_diff_cancel_right' count_eq_zero_iff diff_single_trivial multi_self_add_other_not_self plus_multiset.rep_eq)
26178
nipkow
parents: 26176
diff changeset
  2111
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2112
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2113
lemma one_step_implies_mult_aux:
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2114
  "\<forall>I J K. size J = n \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2115
    \<longrightarrow> (I + K, I + J) \<in> mult r"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2116
apply (induct n)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2117
 apply auto
26178
nipkow
parents: 26176
diff changeset
  2118
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow
parents: 26176
diff changeset
  2119
apply (rename_tac "J'", simp)
nipkow
parents: 26176
diff changeset
  2120
apply (erule notE, auto)
nipkow
parents: 26176
diff changeset
  2121
apply (case_tac "J' = {#}")
nipkow
parents: 26176
diff changeset
  2122
 apply (simp add: mult_def)
nipkow
parents: 26176
diff changeset
  2123
 apply (rule r_into_trancl)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2124
 apply (simp add: mult1_def, blast)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2125
txt \<open>Now we know @{term "J' \<noteq> {#}"}.\<close>
26178
nipkow
parents: 26176
diff changeset
  2126
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2127
apply (erule_tac P = "\<forall>k \<in> set_mset K. P k" for P in rev_mp)
26178
nipkow
parents: 26176
diff changeset
  2128
apply (erule ssubst)
nipkow
parents: 26176
diff changeset
  2129
apply (simp add: Ball_def, auto)
nipkow
parents: 26176
diff changeset
  2130
apply (subgoal_tac
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  2131
  "((I + {# x \<in># K. (x, a) \<in> r #}) + {# x \<in># K. (x, a) \<notin> r #},
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  2132
    (I + {# x \<in># K. (x, a) \<in> r #}) + J') \<in> mult r")
26178
nipkow
parents: 26176
diff changeset
  2133
 prefer 2
nipkow
parents: 26176
diff changeset
  2134
 apply force
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2135
apply (simp (no_asm_use) add: add.assoc [symmetric] mult_def)
26178
nipkow
parents: 26176
diff changeset
  2136
apply (erule trancl_trans)
nipkow
parents: 26176
diff changeset
  2137
apply (rule r_into_trancl)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2138
apply (simp add: mult1_def)
26178
nipkow
parents: 26176
diff changeset
  2139
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  2140
apply (rule_tac x = "I + J'" in exI)
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2141
apply (simp add: ac_simps)
26178
nipkow
parents: 26176
diff changeset
  2142
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2143
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  2144
lemma one_step_implies_mult:
62651
66568c9b8216 superfluous premise (noticed by Julian Nagele)
nipkow
parents: 62537
diff changeset
  2145
  "J \<noteq> {#} \<Longrightarrow> \<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2146
    \<Longrightarrow> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  2147
using one_step_implies_mult_aux by blast
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2148
63089
40134ddec3bf clarified heading
haftmann
parents: 63088
diff changeset
  2149
subsection \<open>A quasi-executable characterization\<close>
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2150
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2151
text \<open>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2152
  The decreasing parts \<open>A\<close> and \<open>B\<close> of multisets in a multiset-comparison
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2153
  \<open>(I + B, I + A) \<in> mult r\<close>, can always be made disjoint.
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2154
\<close>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2155
lemma decreasing_parts_disj:
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2156
  assumes "irrefl r" and "trans r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2157
    and "A \<noteq> {#}" and *: "\<forall>b\<in>#B. \<exists>a\<in>#A. (b, a) \<in> r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2158
  defines "Z \<equiv> A #\<inter> B"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2159
  defines "X \<equiv> A - Z"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2160
  defines "Y \<equiv> B - Z"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2161
  shows "X \<noteq> {#} \<and> X #\<inter> Y = {#} \<and>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2162
    A = X + Z \<and> B = Y + Z \<and> (\<forall>y\<in>#Y. \<exists>x\<in>#X. (y, x) \<in> r)"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2163
proof -
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2164
  define D
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2165
    where "D = set_mset A \<union> set_mset B"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2166
  let ?r = "r \<inter> D \<times> D"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2167
  have "irrefl ?r" and "trans ?r" and "finite ?r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2168
    using \<open>irrefl r\<close> and \<open>trans r\<close> by (auto simp: D_def irrefl_def trans_Restr)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2169
  note wf_converse_induct = wf_induct [OF wf_converse [OF this]]
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2170
  { fix b assume "b \<in># B"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2171
    then have "\<exists>x. x \<in># X \<and> (b, x) \<in> r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2172
    proof (induction rule: wf_converse_induct)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2173
      case (1 b)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2174
      then obtain a where "b \<in># B" and "a \<in># A" and "(b, a) \<in> r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2175
        using * by blast
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2176
      then show ?case
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2177
      proof (cases "a \<in># X")
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2178
        case False
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2179
        then have "a \<in># B" using \<open>a \<in># A\<close>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2180
          by (simp add: X_def Z_def not_in_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2181
            (metis le_zero_eq not_in_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2182
        moreover have "(a, b) \<in> (r \<inter> D \<times> D)\<inverse>"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2183
          using \<open>(b, a) \<in> r\<close> using \<open>b \<in># B\<close> and \<open>a \<in># B\<close>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2184
          by (auto simp: D_def)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2185
        ultimately obtain x where "x \<in># X" and "(a, x) \<in> r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2186
          using "1.IH" by blast
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2187
        moreover then have "(b, x) \<in> r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2188
          using \<open>trans r\<close> and \<open>(b, a) \<in> r\<close> by (auto dest: transD)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2189
        ultimately show ?thesis by blast
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2190
      qed blast
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2191
    qed }
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2192
  note B_less = this
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2193
  then have "\<forall>y\<in>#Y. \<exists>x\<in>#X. (y, x) \<in> r"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2194
    by (auto simp: Y_def Z_def dest: in_diffD)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2195
  moreover have "X \<noteq> {#}"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2196
  proof -
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2197
    obtain a where "a \<in># A" using \<open>A \<noteq> {#}\<close>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2198
      by (auto simp: multiset_eq_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2199
    show ?thesis
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2200
    proof (cases "a \<in># X")
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2201
      case False
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2202
      then have "a \<in># B" using \<open>a \<in># A\<close>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2203
        by (simp add: X_def Z_def not_in_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2204
          (metis le_zero_eq not_in_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2205
      then show ?thesis by (auto dest: B_less)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2206
    qed auto
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2207
  qed
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2208
  moreover have "A = X + Z" and "B = Y + Z" and "X #\<inter> Y = {#}"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2209
    by (auto simp: X_def Y_def Z_def multiset_eq_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2210
  ultimately show ?thesis by blast
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2211
qed
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2212
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2213
text \<open>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2214
  A predicate variant of the reflexive closure of \<open>mult\<close>, which is
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2215
  executable whenever the given predicate \<open>P\<close> is. Together with the
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2216
  standard code equations for \<open>op #\<inter>\<close> and \<open>op -\<close> this should yield
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2217
  a quadratic (with respect to calls to \<open>P\<close>) implementation of \<open>multeqp\<close>.
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2218
\<close>
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2219
definition multeqp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2220
  "multeqp P N M =
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2221
    (let Z = M #\<inter> N; X = M - Z; Y = N - Z in
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2222
    (\<forall>y \<in> set_mset Y. \<exists>x \<in> set_mset X. P y x))"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2223
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2224
lemma multeqp_iff:
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2225
  assumes "irrefl R" and "trans R"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2226
    and [simp]: "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2227
  shows "multeqp P N M \<longleftrightarrow> (N, M) \<in> (mult R)\<^sup>="
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2228
proof
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2229
  assume "(N, M) \<in> (mult R)\<^sup>="
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2230
  then show "multeqp P N M"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2231
  proof
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2232
    assume "(N, M) \<in> mult R"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2233
    from mult_implies_one_step [OF \<open>trans R\<close> this] obtain I J K
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2234
      where *: "I \<noteq> {#}" "\<forall>j\<in>#J. \<exists>i\<in>#I. (j, i) \<in> R"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2235
      and [simp]: "M = K + I" "N = K + J" by blast
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2236
    from decreasing_parts_disj [OF \<open>irrefl R\<close> \<open>trans R\<close> *]
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2237
      show "multeqp P N M"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2238
      by (auto simp: multeqp_def split: if_splits)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2239
  next
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2240
    assume "(N, M) \<in> Id" then show "multeqp P N M" by (auto simp: multeqp_def)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2241
  qed
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2242
next
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2243
  assume "multeqp P N M"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2244
  then obtain X Y Z where *: "Z = M #\<inter> N" "X = M - Z" "Y = N - Z"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2245
    and **: "\<forall>y\<in>#Y. \<exists>x\<in>#X. (y, x) \<in> R" by (auto simp: multeqp_def Let_def)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2246
  then have M: "M = Z + X" and N: "N = Z + Y" by (auto simp: multiset_eq_iff)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2247
  show "(N, M) \<in> (mult R)\<^sup>="
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2248
  proof (cases "X \<noteq> {#}")
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2249
    case True
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2250
    with * and ** have "(Z + Y, Z + X) \<in> mult R"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2251
      by (auto intro: one_step_implies_mult)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2252
    then show ?thesis by (simp add: M N)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2253
  next
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2254
    case False
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2255
    then show ?thesis using **
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2256
      by (cases "Y = {#}") (auto simp: M N)
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2257
  qed
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2258
qed
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2259
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2260
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2261
subsubsection \<open>Partial-order properties\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2262
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2263
lemma (in order) mult1_lessE:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2264
  assumes "(N, M) \<in> mult1 {(a, b). a < b}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2265
  obtains a M0 K where "M = M0 + {#a#}" "N = M0 + K"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2266
    "a \<notin># K" "\<And>b. b \<in># K \<Longrightarrow> b < a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2267
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2268
  from assms obtain a M0 K where "M = M0 + {#a#}" "N = M0 + K"
63060
293ede07b775 some uses of 'obtain' with structure statement;
wenzelm
parents: 63040
diff changeset
  2269
    "b \<in># K \<Longrightarrow> b < a" for b by (blast elim: mult1E)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2270
  moreover from this(3) [of a] have "a \<notin># K" by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2271
  ultimately show thesis by (auto intro: that)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2272
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2273
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2274
definition less_multiset :: "'a::order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "#\<subset>#" 50)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2275
  where "M' #\<subset># M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2276
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2277
definition le_multiset :: "'a::order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "#\<subseteq>#" 50)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2278
  where "M' #\<subseteq># M \<longleftrightarrow> M' #\<subset># M \<or> M' = M"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2279
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2280
notation (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2281
  less_multiset (infix "#<#" 50) and
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2282
  le_multiset (infix "#<=#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2283
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2284
interpretation multiset_order: order le_multiset less_multiset
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2285
proof -
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2286
  have irrefl: "\<not> M #\<subset># M" for M :: "'a multiset"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2287
  proof
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  2288
    assume "M #\<subset># M"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2289
    then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2290
    have "trans {(x'::'a, x). x' < x}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2291
      by (rule transI) simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2292
    moreover note MM
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2293
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2294
      \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2295
      by (rule mult_implies_one_step)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2296
    then obtain I J K where "M = I + J" and "M = I + K"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2297
      and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" by blast
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2298
    then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2299
    have "finite (set_mset K)" by simp
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2300
    moreover note **
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2301
    ultimately have "set_mset K = {}"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2302
      by (induct rule: finite_induct) (auto intro: order_less_trans)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2303
    with * show False by simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2304
  qed
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2305
  have trans: "K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># N" for K M N :: "'a multiset"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2306
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  2307
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2308
    by standard (auto simp add: le_multiset_def irrefl dest: trans)
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
  2309
qed \<comment> \<open>FIXME avoid junk stemming from type class interpretation\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2310
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2311
lemma mult_less_irrefl [elim!]:
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2312
  fixes M :: "'a::order multiset"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2313
  shows "M #\<subset># M \<Longrightarrow> R"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2314
  by simp
26567
7bcebb8c2d33 instantiation replacing primitive instance plus overloaded defs; more conservative type arities
haftmann
parents: 26178
diff changeset
  2315
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2316
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2317
subsubsection \<open>Monotonicity of multiset union\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2318
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2319
lemma mult1_union: "(B, D) \<in> mult1 r \<Longrightarrow> (C + B, C + D) \<in> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  2320
apply (unfold mult1_def)
nipkow
parents: 26176
diff changeset
  2321
apply auto
nipkow
parents: 26176
diff changeset
  2322
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  2323
apply (rule_tac x = "C + M0" in exI)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2324
apply (simp add: add.assoc)
26178
nipkow
parents: 26176
diff changeset
  2325
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2326
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2327
lemma union_less_mono2: "B #\<subset># D \<Longrightarrow> C + B #\<subset># C + (D::'a::order multiset)"
26178
nipkow
parents: 26176
diff changeset
  2328
apply (unfold less_multiset_def mult_def)
nipkow
parents: 26176
diff changeset
  2329
apply (erule trancl_induct)
40249
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  2330
 apply (blast intro: mult1_union)
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  2331
apply (blast intro: mult1_union trancl_trans)
26178
nipkow
parents: 26176
diff changeset
  2332
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2333
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2334
lemma union_less_mono1: "B #\<subset># D \<Longrightarrow> B + C #\<subset># D + (C::'a::order multiset)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2335
apply (subst add.commute [of B C])
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2336
apply (subst add.commute [of D C])
26178
nipkow
parents: 26176
diff changeset
  2337
apply (erule union_less_mono2)
nipkow
parents: 26176
diff changeset
  2338
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2339
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  2340
lemma union_less_mono:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2341
  fixes A B C D :: "'a::order multiset"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2342
  shows "A #\<subset># C \<Longrightarrow> B #\<subset># D \<Longrightarrow> A + B #\<subset># C + D"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2343
  by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2344
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2345
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2346
  by standard (auto simp add: le_multiset_def intro: union_less_mono2)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  2347
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
  2348
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2349
subsubsection \<open>Termination proofs with multiset orders\<close>
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2350
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2351
lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2352
  and multi_member_this: "x \<in># {# x #} + XS"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2353
  and multi_member_last: "x \<in># {# x #}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2354
  by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2355
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2356
definition "ms_strict = mult pair_less"
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  2357
definition "ms_weak = ms_strict \<union> Id"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2358
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2359
lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2360
unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2361
by (auto intro: wf_mult1 wf_trancl simp: mult_def)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2362
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2363
lemma smsI:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2364
  "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2365
  unfolding ms_strict_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2366
by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2367
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2368
lemma wmsI:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2369
  "(set_mset A, set_mset B) \<in> max_strict \<or> A = {#} \<and> B = {#}
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2370
  \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2371
unfolding ms_weak_def ms_strict_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2372
by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2373
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2374
inductive pw_leq
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2375
where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2376
  pw_leq_empty: "pw_leq {#} {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2377
| pw_leq_step:  "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2378
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2379
lemma pw_leq_lstep:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2380
  "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2381
by (drule pw_leq_step) (rule pw_leq_empty, simp)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2382
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2383
lemma pw_leq_split:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2384
  assumes "pw_leq X Y"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2385
  shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2386
  using assms
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2387
proof induct
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2388
  case pw_leq_empty thus ?case by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2389
next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2390
  case (pw_leq_step x y X Y)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2391
  then obtain A B Z where
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2392
    [simp]: "X = A + Z" "Y = B + Z"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2393
      and 1[simp]: "(set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#})"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2394
    by auto
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2395
  from pw_leq_step consider "x = y" | "(x, y) \<in> pair_less"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2396
    unfolding pair_leq_def by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2397
  thus ?case
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2398
  proof cases
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2399
    case [simp]: 1
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2400
    have "{#x#} + X = A + ({#y#}+Z) \<and> {#y#} + Y = B + ({#y#}+Z) \<and>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2401
      ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2402
      by (auto simp: ac_simps)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2403
    thus ?thesis by blast
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2404
  next
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2405
    case 2
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2406
    let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2407
    have "{#x#} + X = ?A' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2408
      "{#y#} + Y = ?B' + Z"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2409
      by (auto simp add: ac_simps)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2410
    moreover have
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2411
      "(set_mset ?A', set_mset ?B') \<in> max_strict"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2412
      using 1 2 unfolding max_strict_def
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2413
      by (auto elim!: max_ext.cases)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2414
    ultimately show ?thesis by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2415
  qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2416
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2417
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2418
lemma
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2419
  assumes pwleq: "pw_leq Z Z'"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2420
  shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2421
    and ms_weakI1:  "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2422
    and ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2423
proof -
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2424
  from pw_leq_split[OF pwleq]
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2425
  obtain A' B' Z''
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2426
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2427
    and mx_or_empty: "(set_mset A', set_mset B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2428
    by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2429
  {
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2430
    assume max: "(set_mset A, set_mset B) \<in> max_strict"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2431
    from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2432
    have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2433
    proof
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2434
      assume max': "(set_mset A', set_mset B') \<in> max_strict"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2435
      with max have "(set_mset (A + A'), set_mset (B + B')) \<in> max_strict"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2436
        by (auto simp: max_strict_def intro: max_ext_additive)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2437
      thus ?thesis by (rule smsI)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2438
    next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2439
      assume [simp]: "A' = {#} \<and> B' = {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2440
      show ?thesis by (rule smsI) (auto intro: max)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2441
    qed
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2442
    thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add: ac_simps)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2443
    thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2444
  }
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2445
  from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2446
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2447
  thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:ac_simps)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2448
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2449
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  2450
lemma empty_neutral: "{#} + x = x" "x + {#} = x"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2451
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2452
and nonempty_single: "{# x #} \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2453
by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2454
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2455
setup \<open>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2456
  let
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2457
    fun msetT T = Type (@{type_name multiset}, [T]);
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2458
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2459
    fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2460
      | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2461
      | mk_mset T (x :: xs) =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2462
            Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2463
                  mk_mset T [x] $ mk_mset T xs
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2464
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2465
    fun mset_member_tac ctxt m i =
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2466
      if m <= 0 then
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2467
        resolve_tac ctxt @{thms multi_member_this} i ORELSE
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2468
        resolve_tac ctxt @{thms multi_member_last} i
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2469
      else
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2470
        resolve_tac ctxt @{thms multi_member_skip} i THEN mset_member_tac ctxt (m - 1) i
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2471
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2472
    fun mset_nonempty_tac ctxt =
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2473
      resolve_tac ctxt @{thms nonempty_plus} ORELSE'
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2474
      resolve_tac ctxt @{thms nonempty_single}
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2475
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2476
    fun regroup_munion_conv ctxt =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2477
      Function_Lib.regroup_conv ctxt @{const_abbrev Mempty} @{const_name plus}
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2478
        (map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2479
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2480
    fun unfold_pwleq_tac ctxt i =
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2481
      (resolve_tac ctxt @{thms pw_leq_step} i THEN (fn st => unfold_pwleq_tac ctxt (i + 1) st))
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2482
        ORELSE (resolve_tac ctxt @{thms pw_leq_lstep} i)
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2483
        ORELSE (resolve_tac ctxt @{thms pw_leq_empty} i)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2484
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2485
    val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union},
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2486
                        @{thm Un_insert_left}, @{thm Un_empty_left}]
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2487
  in
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2488
    ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2489
    {
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2490
      msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2491
      mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2492
      mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps,
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2493
      smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  2494
      reduction_pair = @{thm ms_reduction_pair}
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2495
    })
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2496
  end
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2497
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2498
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2499
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2500
subsection \<open>Legacy theorem bindings\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2501
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  2502
lemmas multi_count_eq = multiset_eq_iff [symmetric]
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2503
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2504
lemma union_commute: "M + N = N + (M::'a multiset)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2505
  by (fact add.commute)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2506
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2507
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2508
  by (fact add.assoc)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2509
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2510
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2511
  by (fact add.left_commute)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2512
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2513
lemmas union_ac = union_assoc union_commute union_lcomm
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2514
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2515
lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2516
  by (fact add_right_cancel)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2517
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2518
lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2519
  by (fact add_left_cancel)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2520
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2521
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
59557
ebd8ecacfba6 establish unique preferred fact names
haftmann
parents: 58881
diff changeset
  2522
  by (fact add_left_imp_eq)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2523
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2524
lemma mset_less_trans: "(M::'a multiset) \<subset># K \<Longrightarrow> K \<subset># N \<Longrightarrow> M \<subset># N"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2525
  by (fact subset_mset.less_trans)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2526
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2527
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2528
  by (fact subset_mset.inf.commute)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2529
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2530
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2531
  by (fact subset_mset.inf.assoc [symmetric])
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2532
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2533
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2534
  by (fact subset_mset.inf.left_commute)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2535
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2536
lemmas multiset_inter_ac =
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2537
  multiset_inter_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2538
  multiset_inter_assoc
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2539
  multiset_inter_left_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2540
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2541
lemma mult_less_not_refl: "\<not> M #\<subset># (M::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2542
  by (fact multiset_order.less_irrefl)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2543
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2544
lemma mult_less_trans: "K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># (N::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2545
  by (fact multiset_order.less_trans)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2546
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2547
lemma mult_less_not_sym: "M #\<subset># N \<Longrightarrow> \<not> N #\<subset># (M::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2548
  by (fact multiset_order.less_not_sym)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2549
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2550
lemma mult_less_asym: "M #\<subset># N \<Longrightarrow> (\<not> P \<Longrightarrow> N #\<subset># (M::'a::order multiset)) \<Longrightarrow> P"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2551
  by (fact multiset_order.less_asym)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2552
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2553
declaration \<open>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2554
  let
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2555
    fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) (Const _ $ t') =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2556
          let
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2557
            val (maybe_opt, ps) =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2558
              Nitpick_Model.dest_plain_fun t'
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2559
              ||> op ~~
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2560
              ||> map (apsnd (snd o HOLogic.dest_number))
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2561
            fun elems_for t =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2562
              (case AList.lookup (op =) ps t of
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2563
                SOME n => replicate n t
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2564
              | NONE => [Const (maybe_name, elem_T --> elem_T) $ t])
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2565
          in
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2566
            (case maps elems_for (all_values elem_T) @
61333
24b5e7579fdd compile
blanchet
parents: 61188
diff changeset
  2567
                 (if maybe_opt then [Const (Nitpick_Model.unrep_mixfix (), elem_T)] else []) of
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2568
              [] => Const (@{const_name zero_class.zero}, T)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2569
            | ts =>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2570
                foldl1 (fn (t1, t2) =>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2571
                    Const (@{const_name plus_class.plus}, T --> T --> T) $ t1 $ t2)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2572
                  (map (curry (op $) (Const (@{const_name single}, elem_T --> T))) ts))
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2573
          end
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2574
      | multiset_postproc _ _ _ _ t = t
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2575
  in Nitpick_Model.register_term_postprocessor @{typ "'a multiset"} multiset_postproc end
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2576
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2577
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2578
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2579
subsection \<open>Naive implementation using lists\<close>
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2580
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2581
code_datatype mset
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2582
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2583
lemma [code]: "{#} = mset []"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2584
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2585
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2586
lemma [code]: "{#x#} = mset [x]"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2587
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2588
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2589
lemma union_code [code]: "mset xs + mset ys = mset (xs @ ys)"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2590
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2591
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2592
lemma [code]: "image_mset f (mset xs) = mset (map f xs)"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2593
  by (simp add: mset_map)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2594
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2595
lemma [code]: "filter_mset f (mset xs) = mset (filter f xs)"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2596
  by (simp add: mset_filter)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2597
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2598
lemma [code]: "mset xs - mset ys = mset (fold remove1 ys xs)"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2599
  by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2601
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2602
  "mset xs #\<inter> mset ys =
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2603
    mset (snd (fold (\<lambda>x (ys, zs).
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2604
      if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2605
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2606
  have "\<And>zs. mset (snd (fold (\<lambda>x (ys, zs).
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2607
    if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2608
      (mset xs #\<inter> mset ys) + mset zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2609
    by (induct xs arbitrary: ys)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2610
      (auto simp add: inter_add_right1 inter_add_right2 ac_simps)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2611
  then show ?thesis by simp
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2612
qed
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2613
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2614
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2615
  "mset xs #\<union> mset ys =
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 61378
diff changeset
  2616
    mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2617
proof -
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 61378
diff changeset
  2618
  have "\<And>zs. mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2619
      (mset xs #\<union> mset ys) + mset zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2620
    by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2621
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2622
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2623
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2624
declare in_multiset_in_set [code_unfold]
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2625
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2626
lemma [code]: "count (mset xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2627
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2628
  have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (mset xs) x + n"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2629
    by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2630
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2631
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2632
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2633
declare set_mset_mset [code]
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2634
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2635
declare sorted_list_of_multiset_mset [code]
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2636
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  2637
lemma [code]: \<comment> \<open>not very efficient, but representation-ignorant!\<close>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2638
  "mset_set A = mset (sorted_list_of_set A)"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2639
  apply (cases "finite A")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2640
  apply simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2641
  apply (induct A rule: finite_induct)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2642
  apply (simp_all add: add.commute)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2643
  done
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2644
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2645
declare size_mset [code]
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2646
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2647
fun ms_lesseq_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2648
  "ms_lesseq_impl [] ys = Some (ys \<noteq> [])"
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2649
| "ms_lesseq_impl (Cons x xs) ys = (case List.extract (op = x) ys of
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2650
     None \<Rightarrow> None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2651
   | Some (ys1,_,ys2) \<Rightarrow> ms_lesseq_impl xs (ys1 @ ys2))"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2652
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2653
lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None \<longleftrightarrow> \<not> mset xs \<subseteq># mset ys) \<and>
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2654
  (ms_lesseq_impl xs ys = Some True \<longleftrightarrow> mset xs \<subset># mset ys) \<and>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2655
  (ms_lesseq_impl xs ys = Some False \<longrightarrow> mset xs = mset ys)"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2656
proof (induct xs arbitrary: ys)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2657
  case (Nil ys)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2658
  show ?case by (auto simp: mset_less_empty_nonempty)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2659
next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2660
  case (Cons x xs ys)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2661
  show ?case
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2662
  proof (cases "List.extract (op = x) ys")
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2663
    case None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2664
    hence x: "x \<notin> set ys" by (simp add: extract_None_iff)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2665
    {
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2666
      assume "mset (x # xs) \<subseteq># mset ys"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2667
      from set_mset_mono[OF this] x have False by simp
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2668
    } note nle = this
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2669
    moreover
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2670
    {
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2671
      assume "mset (x # xs) \<subset># mset ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2672
      hence "mset (x # xs) \<subseteq># mset ys" by auto
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2673
      from nle[OF this] have False .
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2674
    }
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2675
    ultimately show ?thesis using None by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2676
  next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2677
    case (Some res)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2678
    obtain ys1 y ys2 where res: "res = (ys1,y,ys2)" by (cases res, auto)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2679
    note Some = Some[unfolded res]
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2680
    from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2681
    hence id: "mset ys = mset (ys1 @ ys2) + {#x#}"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2682
      by (auto simp: ac_simps)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2683
    show ?thesis unfolding ms_lesseq_impl.simps
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2684
      unfolding Some option.simps split
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2685
      unfolding id
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2686
      using Cons[of "ys1 @ ys2"]
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2687
      unfolding subset_mset_def subseteq_mset_def by auto
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2688
  qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2689
qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2690
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2691
lemma [code]: "mset xs \<subseteq># mset ys \<longleftrightarrow> ms_lesseq_impl xs ys \<noteq> None"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2692
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2693
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2694
lemma [code]: "mset xs \<subset># mset ys \<longleftrightarrow> ms_lesseq_impl xs ys = Some True"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2695
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2696
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2697
instantiation multiset :: (equal) equal
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2698
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2699
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2700
definition
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2701
  [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2702
lemma [code]: "HOL.equal (mset xs) (mset ys) \<longleftrightarrow> ms_lesseq_impl xs ys = Some False"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2703
  unfolding equal_multiset_def
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2704
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2705
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2706
instance
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2707
  by standard (simp add: equal_multiset_def)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2708
37169
f69efa106feb make Nitpick "show_all" option behave less surprisingly
blanchet
parents: 37107
diff changeset
  2709
end
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
  2710
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2711
lemma [code]: "msetsum (mset xs) = listsum xs"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2712
  by (induct xs) (simp_all add: add.commute)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2713
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2714
lemma [code]: "msetprod (mset xs) = fold times xs 1"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2715
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2716
  have "\<And>x. fold times xs x = msetprod (mset xs) * x"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2717
    by (induct xs) (simp_all add: mult.assoc)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2718
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2719
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2720
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2721
text \<open>
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2722
  Exercise for the casual reader: add implementations for @{const le_multiset}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2723
  and @{const less_multiset} (multiset order).
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2724
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2725
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2726
text \<open>Quickcheck generators\<close>
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2727
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2728
definition (in term_syntax)
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 61031
diff changeset
  2729
  msetify :: "'a::typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2730
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2731
  [code_unfold]: "msetify xs = Code_Evaluation.valtermify mset {\<cdot>} xs"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2732
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2733
notation fcomp (infixl "\<circ>>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2734
notation scomp (infixl "\<circ>\<rightarrow>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2735
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2736
instantiation multiset :: (random) random
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2737
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2738
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2739
definition
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2740
  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (msetify xs))"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2741
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2742
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2743
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2744
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2745
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2746
no_notation fcomp (infixl "\<circ>>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2747
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2748
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2749
instantiation multiset :: (full_exhaustive) full_exhaustive
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2750
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2751
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2752
definition full_exhaustive_multiset :: "('a multiset \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2753
where
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2754
  "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (\<lambda>xs. f (msetify xs)) i"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2755
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2756
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2757
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2758
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2759
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2760
hide_const (open) msetify
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2761
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2762
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2763
subsection \<open>BNF setup\<close>
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2764
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2765
definition rel_mset where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2766
  "rel_mset R X Y \<longleftrightarrow> (\<exists>xs ys. mset xs = X \<and> mset ys = Y \<and> list_all2 R xs ys)"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2767
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2768
lemma mset_zip_take_Cons_drop_twice:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2769
  assumes "length xs = length ys" "j \<le> length xs"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2770
  shows "mset (zip (take j xs @ x # drop j xs) (take j ys @ y # drop j ys)) =
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2771
    mset (zip xs ys) + {#(x, y)#}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2772
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2773
proof (induct xs ys arbitrary: x y j rule: list_induct2)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2774
  case Nil
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2775
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2776
    by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2777
next
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2778
  case (Cons x xs y ys)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2779
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2780
  proof (cases "j = 0")
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2781
    case True
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2782
    thus ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2783
      by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2784
  next
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2785
    case False
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2786
    then obtain k where k: "j = Suc k"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2787
      by (cases j) simp
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2788
    hence "k \<le> length xs"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2789
      using Cons.prems by auto
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2790
    hence "mset (zip (take k xs @ x # drop k xs) (take k ys @ y # drop k ys)) =
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2791
      mset (zip xs ys) + {#(x, y)#}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2792
      by (rule Cons.hyps(2))
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2793
    thus ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2794
      unfolding k by (auto simp: add.commute union_lcomm)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2795
  qed
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2796
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2797
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2798
lemma ex_mset_zip_left:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2799
  assumes "length xs = length ys" "mset xs' = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2800
  shows "\<exists>ys'. length ys' = length xs' \<and> mset (zip xs' ys') = mset (zip xs ys)"
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2801
using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2802
proof (induct xs ys arbitrary: xs' rule: list_induct2)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2803
  case Nil
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2804
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2805
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2806
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2807
  case (Cons x xs y ys xs')
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2808
  obtain j where j_len: "j < length xs'" and nth_j: "xs' ! j = x"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2809
    by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2810
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62837
diff changeset
  2811
  define xsa where "xsa = take j xs' @ drop (Suc j) xs'"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2812
  have "mset xs' = {#x#} + mset xsa"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2813
    unfolding xsa_def using j_len nth_j
58247
98d0f85d247f enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
nipkow
parents: 58098
diff changeset
  2814
    by (metis (no_types) ab_semigroup_add_class.add_ac(1) append_take_drop_id Cons_nth_drop_Suc
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2815
      mset.simps(2) union_code add.commute)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2816
  hence ms_x: "mset xsa = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2817
    by (metis Cons.prems add.commute add_right_imp_eq mset.simps(2))
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2818
  then obtain ysa where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2819
    len_a: "length ysa = length xsa" and ms_a: "mset (zip xsa ysa) = mset (zip xs ys)"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2820
    using Cons.hyps(2) by blast
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2821
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62837
diff changeset
  2822
  define ys' where "ys' = take j ysa @ y # drop j ysa"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2823
  have xs': "xs' = take j xsa @ x # drop j xsa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2824
    using ms_x j_len nth_j Cons.prems xsa_def
58247
98d0f85d247f enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
nipkow
parents: 58098
diff changeset
  2825
    by (metis append_eq_append_conv append_take_drop_id diff_Suc_Suc Cons_nth_drop_Suc length_Cons
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2826
      length_drop size_mset)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2827
  have j_len': "j \<le> length xsa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2828
    using j_len xs' xsa_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2829
    by (metis add_Suc_right append_take_drop_id length_Cons length_append less_eq_Suc_le not_less)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2830
  have "length ys' = length xs'"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2831
    unfolding ys'_def using Cons.prems len_a ms_x
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2832
    by (metis add_Suc_right append_take_drop_id length_Cons length_append mset_eq_length)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2833
  moreover have "mset (zip xs' ys') = mset (zip (x # xs) (y # ys))"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2834
    unfolding xs' ys'_def
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2835
    by (rule trans[OF mset_zip_take_Cons_drop_twice])
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2836
      (auto simp: len_a ms_a j_len' add.commute)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2837
  ultimately show ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2838
    by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2839
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2840
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2841
lemma list_all2_reorder_left_invariance:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2842
  assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2843
  shows "\<exists>ys'. list_all2 R xs' ys' \<and> mset ys' = mset ys"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2844
proof -
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2845
  have len: "length xs = length ys"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2846
    using rel list_all2_conv_all_nth by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2847
  obtain ys' where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2848
    len': "length xs' = length ys'" and ms_xy: "mset (zip xs' ys') = mset (zip xs ys)"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2849
    using len ms_x by (metis ex_mset_zip_left)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2850
  have "list_all2 R xs' ys'"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2851
    using assms(1) len' ms_xy unfolding list_all2_iff by (blast dest: mset_eq_setD)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2852
  moreover have "mset ys' = mset ys"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2853
    using len len' ms_xy map_snd_zip mset_map by metis
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2854
  ultimately show ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2855
    by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2856
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2857
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2858
lemma ex_mset: "\<exists>xs. mset xs = X"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2859
  by (induct X) (simp, metis mset.simps(2))
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2860
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2861
inductive pred_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> bool"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2862
where
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2863
  "pred_mset P {#}"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2864
| "\<lbrakk>P a; pred_mset P M\<rbrakk> \<Longrightarrow> pred_mset P (M + {#a#})"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2865
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2866
bnf "'a multiset"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2867
  map: image_mset
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2868
  sets: set_mset
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2869
  bd: natLeq
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2870
  wits: "{#}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2871
  rel: rel_mset
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2872
  pred: pred_mset
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2873
proof -
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2874
  show "image_mset id = id"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2875
    by (rule image_mset.id)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2876
  show "image_mset (g \<circ> f) = image_mset g \<circ> image_mset f" for f g
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2877
    unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2878
  show "(\<And>z. z \<in> set_mset X \<Longrightarrow> f z = g z) \<Longrightarrow> image_mset f X = image_mset g X" for f g X
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2879
    by (induct X) simp_all
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2880
  show "set_mset \<circ> image_mset f = op ` f \<circ> set_mset" for f
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2881
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2882
  show "card_order natLeq"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2883
    by (rule natLeq_card_order)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2884
  show "BNF_Cardinal_Arithmetic.cinfinite natLeq"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2885
    by (rule natLeq_cinfinite)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2886
  show "ordLeq3 (card_of (set_mset X)) natLeq" for X
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2887
    by transfer
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2888
      (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2889
  show "rel_mset R OO rel_mset S \<le> rel_mset (R OO S)" for R S
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2890
    unfolding rel_mset_def[abs_def] OO_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2891
    apply clarify
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2892
    subgoal for X Z Y xs ys' ys zs
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2893
      apply (drule list_all2_reorder_left_invariance [where xs = ys' and ys = zs and xs' = ys])
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2894
      apply (auto intro: list_all2_trans)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2895
      done
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2896
    done
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2897
  show "rel_mset R =
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2898
    (\<lambda>x y. \<exists>z. set_mset z \<subseteq> {(x, y). R x y} \<and>
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2899
    image_mset fst z = x \<and> image_mset snd z = y)" for R
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2900
    unfolding rel_mset_def[abs_def]
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2901
    apply (rule ext)+
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2902
    apply safe
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2903
     apply (rule_tac x = "mset (zip xs ys)" in exI;
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2904
       auto simp: in_set_zip list_all2_iff mset_map[symmetric])
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2905
    apply (rename_tac XY)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2906
    apply (cut_tac X = XY in ex_mset)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2907
    apply (erule exE)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2908
    apply (rename_tac xys)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2909
    apply (rule_tac x = "map fst xys" in exI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2910
    apply (auto simp: mset_map)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2911
    apply (rule_tac x = "map snd xys" in exI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2912
    apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd)
59997
90fb391a15c1 tuned proofs;
wenzelm
parents: 59986
diff changeset
  2913
    done
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2914
  show "z \<in> set_mset {#} \<Longrightarrow> False" for z
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2915
    by auto
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2916
  show "pred_mset P = (\<lambda>x. Ball (set_mset x) P)" for P
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2917
  proof (intro ext iffI)
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2918
    fix x
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2919
    assume "pred_mset P x"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2920
    then show "Ball (set_mset x) P" by (induct pred: pred_mset; simp)
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2921
  next
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2922
    fix x
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2923
    assume "Ball (set_mset x) P"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2924
    then show "pred_mset P x" by (induct x; auto intro: pred_mset.intros)
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  2925
  qed
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2926
qed
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2927
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2928
inductive rel_mset'
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2929
where
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2930
  Zero[intro]: "rel_mset' R {#} {#}"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2931
| Plus[intro]: "\<lbrakk>R a b; rel_mset' R M N\<rbrakk> \<Longrightarrow> rel_mset' R (M + {#a#}) (N + {#b#})"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2932
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2933
lemma rel_mset_Zero: "rel_mset R {#} {#}"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2934
unfolding rel_mset_def Grp_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2935
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2936
declare multiset.count[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2937
declare Abs_multiset_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2938
declare multiset.count_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2939
declare union_preserves_multiset[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2940
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2941
lemma rel_mset_Plus:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2942
  assumes ab: "R a b"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2943
    and MN: "rel_mset R M N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2944
  shows "rel_mset R (M + {#a#}) (N + {#b#})"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2945
proof -
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2946
  have "\<exists>ya. image_mset fst y + {#a#} = image_mset fst ya \<and>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2947
    image_mset snd y + {#b#} = image_mset snd ya \<and>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2948
    set_mset ya \<subseteq> {(x, y). R x y}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2949
    if "R a b" and "set_mset y \<subseteq> {(x, y). R x y}" for y
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2950
    using that by (intro exI[of _ "y + {#(a,b)#}"]) auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2951
  thus ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2952
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2953
  unfolding multiset.rel_compp_Grp Grp_def by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2954
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2955
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2956
lemma rel_mset'_imp_rel_mset: "rel_mset' R M N \<Longrightarrow> rel_mset R M N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2957
  by (induct rule: rel_mset'.induct) (auto simp: rel_mset_Zero rel_mset_Plus)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2958
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2959
lemma rel_mset_size: "rel_mset R M N \<Longrightarrow> size M = size N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2960
  unfolding multiset.rel_compp_Grp Grp_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2961
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2962
lemma multiset_induct2[case_names empty addL addR]:
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2963
  assumes empty: "P {#} {#}"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2964
    and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2965
    and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2966
  shows "P M N"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2967
apply(induct N rule: multiset_induct)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2968
  apply(induct M rule: multiset_induct, rule empty, erule addL)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2969
  apply(induct M rule: multiset_induct, erule addR, erule addR)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2970
done
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2971
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2972
lemma multiset_induct2_size[consumes 1, case_names empty add]:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2973
  assumes c: "size M = size N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2974
    and empty: "P {#} {#}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2975
    and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2976
  shows "P M N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2977
  using c
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2978
proof (induct M arbitrary: N rule: measure_induct_rule[of size])
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2979
  case (less M)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2980
  show ?case
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2981
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2982
    case True hence "N = {#}" using less.prems by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2983
    thus ?thesis using True empty by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2984
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2985
    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2986
    have "N \<noteq> {#}" using False less.prems by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2987
    then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2988
    have "size M1 = size N1" using less.prems unfolding M N by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2989
    thus ?thesis using M N less.hyps add by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2990
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2991
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2992
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2993
lemma msed_map_invL:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2994
  assumes "image_mset f (M + {#a#}) = N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2995
  shows "\<exists>N1. N = N1 + {#f a#} \<and> image_mset f M = N1"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2996
proof -
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2997
  have "f a \<in># N"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2998
    using assms multiset.set_map[of f "M + {#a#}"] by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2999
  then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3000
  have "image_mset f M = N1" using assms unfolding N by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3001
  thus ?thesis using N by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3002
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3003
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3004
lemma msed_map_invR:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3005
  assumes "image_mset f M = N + {#b#}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3006
  shows "\<exists>M1 a. M = M1 + {#a#} \<and> f a = b \<and> image_mset f M1 = N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3007
proof -
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3008
  obtain a where a: "a \<in># M" and fa: "f a = b"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3009
    using multiset.set_map[of f M] unfolding assms
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3010
    by (metis image_iff union_single_eq_member)
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3011
  then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3012
  have "image_mset f M1 = N" using assms unfolding M fa[symmetric] by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3013
  thus ?thesis using M fa by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3014
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3015
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3016
lemma msed_rel_invL:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3017
  assumes "rel_mset R (M + {#a#}) N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3018
  shows "\<exists>N1 b. N = N1 + {#b#} \<and> R a b \<and> rel_mset R M N1"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3019
proof -
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3020
  obtain K where KM: "image_mset fst K = M + {#a#}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3021
    and KN: "image_mset snd K = N" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3022
    using assms
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3023
    unfolding multiset.rel_compp_Grp Grp_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3024
  obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3025
    and K1M: "image_mset fst K1 = M" using msed_map_invR[OF KM] by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3026
  obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "image_mset snd K1 = N1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3027
    using msed_map_invL[OF KN[unfolded K]] by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3028
  have Rab: "R a (snd ab)" using sK a unfolding K by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3029
  have "rel_mset R M N1" using sK K1M K1N1
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3030
    unfolding K multiset.rel_compp_Grp Grp_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3031
  thus ?thesis using N Rab by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3032
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3033
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3034
lemma msed_rel_invR:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3035
  assumes "rel_mset R M (N + {#b#})"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3036
  shows "\<exists>M1 a. M = M1 + {#a#} \<and> R a b \<and> rel_mset R M1 N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3037
proof -
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3038
  obtain K where KN: "image_mset snd K = N + {#b#}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3039
    and KM: "image_mset fst K = M" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3040
    using assms
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3041
    unfolding multiset.rel_compp_Grp Grp_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3042
  obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3043
    and K1N: "image_mset snd K1 = N" using msed_map_invR[OF KN] by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3044
  obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "image_mset fst K1 = M1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3045
    using msed_map_invL[OF KM[unfolded K]] by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3046
  have Rab: "R (fst ab) b" using sK b unfolding K by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3047
  have "rel_mset R M1 N" using sK K1N K1M1
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3048
    unfolding K multiset.rel_compp_Grp Grp_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3049
  thus ?thesis using M Rab by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3050
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3051
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3052
lemma rel_mset_imp_rel_mset':
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3053
  assumes "rel_mset R M N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3054
  shows "rel_mset' R M N"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  3055
using assms proof(induct M arbitrary: N rule: measure_induct_rule[of size])
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3056
  case (less M)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  3057
  have c: "size M = size N" using rel_mset_size[OF less.prems] .
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3058
  show ?case
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3059
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3060
    case True hence "N = {#}" using c by simp
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3061
    thus ?thesis using True rel_mset'.Zero by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3062
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3063
    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3064
    obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_mset R M1 N1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3065
      using msed_rel_invL[OF less.prems[unfolded M]] by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3066
    have "rel_mset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3067
    thus ?thesis using rel_mset'.Plus[of R a b, OF R] unfolding M N by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3068
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3069
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3070
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3071
lemma rel_mset_rel_mset': "rel_mset R M N = rel_mset' R M N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3072
  using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3073
60613
f11e9fd70b7d fix tex-output for rel_mset
hoelzl
parents: 60608
diff changeset
  3074
text \<open>The main end product for @{const rel_mset}: inductive characterization:\<close>
61337
4645502c3c64 fewer aliases for toplevel theorem statements;
wenzelm
parents: 61188
diff changeset
  3075
lemmas rel_mset_induct[case_names empty add, induct pred: rel_mset] =
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3076
  rel_mset'.induct[unfolded rel_mset_rel_mset'[symmetric]]
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3077
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3078
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3079
subsection \<open>Size setup\<close>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3080
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3081
lemma multiset_size_o_map: "size_multiset g \<circ> image_mset f = size_multiset (g \<circ> f)"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3082
  apply (rule ext)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3083
  subgoal for x by (induct x) auto
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3084
  done
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3085
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3086
setup \<open>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3087
  BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset}
62082
614ef6d7a6b6 nicer 'Spec_Rules' for size function
blanchet
parents: 61955
diff changeset
  3088
    @{thm size_multiset_overloaded_def}
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3089
    @{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3090
      size_union}
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3091
    @{thms multiset_size_o_map}
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3092
\<close>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3093
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3094
hide_const (open) wcount
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3095
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3096
end