src/HOL/Library/Multiset.thy
author nipkow
Fri, 19 Jun 2015 15:55:22 +0200
changeset 60515 484559628038
parent 60513 55c7316f76d6
child 60606 e5cb9271e339
permissions -rw-r--r--
renamed multiset_of -> mset
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
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    17
definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    18
49834
b27bbb021df1 discontinued obsolete typedef (open) syntax;
wenzelm
parents: 49823
diff changeset
    19
typedef 'a multiset = "multiset :: ('a => 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
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
    28
abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
    29
  "a :# M == 0 < count M a"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
    30
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    31
notation (xsymbols)
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    32
  Melem (infix "\<in>#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    33
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    34
lemma 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
    35
  "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
    36
  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
    37
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    38
lemma multiset_eqI:
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
    39
  "(\<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
    40
  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
    41
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    42
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
    43
 \medskip Preservation of the representing set @{term multiset}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    44
\<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
    45
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
lemma const0_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
  "(\<lambda>a. 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
    48
  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
    49
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
lemma only1_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
    51
  "(\<lambda>b. if b = a then n 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
    52
  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
    53
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
lemma union_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
    55
  "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<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
    56
  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
    57
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
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
    59
  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
    60
  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
    61
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
    62
  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
    63
    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
    64
  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
    65
    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
    66
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
    67
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    68
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
    69
  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
    70
  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
    71
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
    72
  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
    73
    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
    74
  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
    75
    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
    76
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
    77
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
    78
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
    79
  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
    80
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
    81
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    82
subsection \<open>Representing multisets\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    83
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    84
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
    85
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    86
instantiation multiset :: (type) cancel_comm_monoid_add
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    87
begin
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    88
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    89
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    90
by (rule const0_in_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    91
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
    92
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
    93
  "Mempty \<equiv> 0"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    94
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    95
lift_definition plus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    96
by (rule union_preserves_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    97
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
    98
lift_definition minus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
    99
by (rule diff_preserves_multiset)
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
   100
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
   101
instance
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
   102
  by default (transfer, simp add: fun_eq_iff)+
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
   103
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
   104
end
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   105
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   106
lift_definition single :: "'a => 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   107
by (rule only1_in_multiset)
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   108
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   109
syntax
26176
038baad81209 tuned syntax declaration;
wenzelm
parents: 26145
diff changeset
   110
  "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
25507
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   111
translations
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   112
  "{#x, xs#}" == "{#x#} + {#xs#}"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   113
  "{#x#}" == "CONST single x"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   114
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
   115
lemma count_empty [simp]: "count {#} a = 0"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   116
  by (simp add: zero_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   117
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
   118
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
   119
  by (simp add: single.rep_eq)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29509
diff changeset
   120
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   121
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   122
subsection \<open>Basic operations\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   123
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   124
subsubsection \<open>Union\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   125
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
   126
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   127
  by (simp add: plus_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   128
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   129
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   130
subsubsection \<open>Difference\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   131
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
   132
instantiation multiset :: (type) comm_monoid_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
   133
begin
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
   134
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
   135
instance
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
   136
by default (transfer, 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
   137
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
   138
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
   139
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
   140
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   141
  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
   142
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   143
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   144
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   145
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   146
lemma diff_cancel[simp]: "A - A = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   147
  by (fact Groups.diff_cancel)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   148
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   149
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   150
  by (fact add_diff_cancel_right')
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   151
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   152
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   153
  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
   154
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   155
lemma diff_right_commute:
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   156
  "(M::'a multiset) - N - Q = M - Q - N"
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   157
  by (fact diff_right_commute)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   158
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   159
lemma diff_add:
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   160
  "(M::'a multiset) - (N + Q) = M - N - Q"
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   161
  by (rule sym) (fact diff_diff_add)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   162
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
   163
lemma insert_DiffM:
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
   164
  "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
   165
  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
   166
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
   167
lemma insert_DiffM2 [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
   168
  "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
   169
  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
   170
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
   171
lemma 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
   172
  "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
   173
  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
   174
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
   175
lemma diff_union_single_conv:
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
   176
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#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
   177
  by (simp add: multiset_eq_iff)
26143
314c0bcb7df7 Added useful general lemmas from the work with the HeapMonad
bulwahn
parents: 26033
diff changeset
   178
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   179
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   180
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
   181
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
   182
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
   183
  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
   184
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
   185
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
   186
  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
   187
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
   188
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
   189
  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
   190
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
   191
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
   192
  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
   193
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
   194
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
   195
  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
   196
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
   197
lemma diff_single_trivial:
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
   198
  "\<not> x \<in># M \<Longrightarrow> 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
   199
  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
   200
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
   201
lemma diff_single_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
   202
  "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#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
   203
  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
   204
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
   205
lemma union_single_eq_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
   206
  "M + {#x#} = N \<Longrightarrow> M = N - {#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
   207
  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
   208
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
   209
lemma union_single_eq_member:
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
   210
  "M + {#x#} = N \<Longrightarrow> x \<in># 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
   211
  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
   212
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
   213
lemma union_is_single:
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   214
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   215
proof
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
   216
  assume ?rhs then show ?lhs 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
   217
next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   218
  assume ?lhs then show ?rhs
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   219
    by (simp add: multiset_eq_iff split:if_splits) (metis add_is_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
   220
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
   221
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
   222
lemma single_is_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
   223
  "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = 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
   224
  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
   225
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
   226
lemma 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
   227
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"  (is "?lhs = ?rhs")
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44339
diff changeset
   228
(* 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
   229
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
   230
  assume ?rhs then show ?lhs
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
   231
  by (auto simp add: add.assoc add.commute [of "{#b#}"])
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
   232
    (drule sym, simp add: 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
   233
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
   234
  assume ?lhs
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
  show ?rhs
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
   236
  proof (cases "a = b")
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   237
    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
   238
  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
   239
    case False
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   240
    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
   241
    with False have "a \<in># N" by auto
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   242
    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
   243
    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
   244
    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
   245
  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
   246
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
   247
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   248
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
   249
  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
   250
   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
   251
  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
   252
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
   253
  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
   254
  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
   255
  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
   256
  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
   257
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
   258
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
   259
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
   260
  "(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
   261
    (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
   262
  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
   263
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   264
lemma multi_member_split:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   265
  "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   266
  by (rule_tac x = "M - {#x#}" in exI, simp)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   267
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   268
lemma multiset_add_sub_el_shuffle:
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   269
  assumes "c \<in># B" and "b \<noteq> c"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   270
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   271
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   272
  from \<open>c \<in># B\<close> obtain A where B: "B = A + {#c#}"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   273
    by (blast dest: multi_member_split)
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   274
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   275
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   276
    by (simp add: ac_simps)
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   277
  then show ?thesis using B by simp
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   278
qed
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   279
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
   280
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   281
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
   282
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   283
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
60400
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 60397
diff changeset
   284
"subseteq_mset A B = (\<forall>a. count A a \<le> count B a)"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   285
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   286
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
60400
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 60397
diff changeset
   287
"subset_mset A B = (A <=# B \<and> A \<noteq> B)"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   288
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   289
notation subseteq_mset (infix "\<le>#" 50)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   290
notation (xsymbols) subseteq_mset (infix "\<subseteq>#" 50)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   291
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   292
notation (xsymbols) subset_mset (infix "\<subset>#" 50)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   293
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   294
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op <=#" "op <#"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   295
  by default (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym)
59986
f38b94549dc8 introduced new abbreviations for multiset operations (in the hope of getting rid of the old names <, <=, etc.)
blanchet
parents: 59958
diff changeset
   296
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
   297
lemma mset_less_eqI:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   298
  "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   299
  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
   300
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   301
lemma mset_le_exists_conv:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   302
  "(A::'a multiset) \<le># B \<longleftrightarrow> (\<exists>C. B = A + C)"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   303
apply (unfold subseteq_mset_def, rule iffI, rule_tac x = "B - A" in exI)
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
apply (auto intro: multiset_eq_iff [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
   305
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
   306
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   307
interpretation subset_mset: ordered_cancel_comm_monoid_diff  "op +" "op -" 0 "op \<le>#" "op <#"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   308
  by default (simp, fact mset_le_exists_conv)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   309
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   310
lemma mset_le_mono_add_right_cancel [simp]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   311
  "(A::'a multiset) + C \<le># B + C \<longleftrightarrow> A \<le># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   312
  by (fact subset_mset.add_le_cancel_right)
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
   313
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   314
lemma mset_le_mono_add_left_cancel [simp]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   315
  "C + (A::'a multiset) \<le># C + B \<longleftrightarrow> A \<le># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   316
  by (fact subset_mset.add_le_cancel_left)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   317
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   318
lemma mset_le_mono_add:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   319
  "(A::'a multiset) \<le># B \<Longrightarrow> C \<le># D \<Longrightarrow> A + C \<le># B + D"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   320
  by (fact subset_mset.add_mono)
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
   321
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   322
lemma mset_le_add_left [simp]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   323
  "(A::'a multiset) \<le># A + B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   324
  unfolding subseteq_mset_def by auto
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   325
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   326
lemma mset_le_add_right [simp]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   327
  "B \<le># (A::'a multiset) + B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   328
  unfolding subseteq_mset_def 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
   329
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   330
lemma mset_le_single:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   331
  "a :# B \<Longrightarrow> {#a#} \<le># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   332
  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
   333
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   334
lemma multiset_diff_union_assoc:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   335
  "C \<le># B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   336
  by (simp add: subset_mset.diff_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
   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 mset_le_multiset_union_diff_commute:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   339
  "B \<le># A \<Longrightarrow> (A::'a multiset) - B + C = A + C - B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   340
by (simp add: subset_mset.add_diff_assoc2)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   341
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   342
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le># M"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   343
by(simp add: subseteq_mset_def)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   344
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   345
lemma mset_lessD: "A <# B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   346
apply (clarsimp simp: subset_mset_def 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
   347
apply (erule_tac x=x in allE)
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
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
   349
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
   350
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   351
lemma mset_leD: "A \<le># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   352
apply (clarsimp simp: subset_mset_def 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
   353
apply (erule_tac x = x in allE)
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
   354
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
   355
done
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   356
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   357
lemma mset_less_insertD: "(A + {#x#} <# B) \<Longrightarrow> (x \<in># B \<and> A <# 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
   358
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
   359
 apply (simp add: mset_lessD)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   360
apply (clarsimp simp: subset_mset_def 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
   361
apply safe
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
   362
 apply (erule_tac x = a in allE)
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
 apply (auto split: split_if_asm)
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
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
   365
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   366
lemma mset_le_insertD: "(A + {#x#} \<le># B) \<Longrightarrow> (x \<in># B \<and> A \<le># 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
   367
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
   368
 apply (simp add: mset_leD)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   369
apply (force simp: subset_mset_def subseteq_mset_def split: split_if_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
   370
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
   371
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   372
lemma mset_less_of_empty[simp]: "A <# {#} \<longleftrightarrow> False"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   373
  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
   374
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   375
lemma empty_le[simp]: "{#} \<le># A"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   376
  unfolding mset_le_exists_conv by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   377
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   378
lemma le_empty[simp]: "(M \<le># {#}) = (M = {#})"
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   379
  unfolding mset_le_exists_conv by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   380
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   381
lemma multi_psub_of_add_self[simp]: "A <# A + {#x#}"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   382
  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
   383
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   384
lemma multi_psub_self[simp]: "(A::'a multiset) <# A = False"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   385
  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
   386
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   387
lemma mset_less_add_bothsides: "N + {#x#} <# M + {#x#} \<Longrightarrow> N <# M"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   388
  by (fact subset_mset.add_less_imp_less_right)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   389
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   390
lemma mset_less_empty_nonempty:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   391
  "{#} <# S \<longleftrightarrow> S \<noteq> {#}"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   392
  by (auto simp: subset_mset_def subseteq_mset_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   393
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   394
lemma mset_less_diff_self:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   395
  "c \<in># B \<Longrightarrow> B - {#c#} <# B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   396
  by (auto simp: subset_mset_def subseteq_mset_def multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   397
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   398
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   399
subsubsection \<open>Intersection\<close>
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   400
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   401
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
   402
  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
   403
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   404
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<le>#" "op <#"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   405
proof -
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   406
   have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   407
   show "class.semilattice_inf op #\<inter> op \<le># op <#"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   408
     by default (auto simp add: multiset_inter_def subseteq_mset_def aux)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   409
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   410
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
   411
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   412
lemma multiset_inter_count [simp]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   413
  "count ((A::'a multiset) #\<inter> B) x = min (count A x) (count B x)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   414
  by (simp add: multiset_inter_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   415
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   416
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   417
  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
   418
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   419
lemma multiset_union_diff_commute:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   420
  assumes "B #\<inter> C = {#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   421
  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
   422
proof (rule multiset_eqI)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   423
  fix x
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   424
  from assms have "min (count B x) (count C x) = 0"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   425
    by (auto simp add: multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   426
  then have "count B x = 0 \<or> count C x = 0"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   427
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   428
  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
   429
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   430
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   431
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   432
lemma empty_inter [simp]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   433
  "{#} #\<inter> M = {#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   434
  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
   435
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   436
lemma inter_empty [simp]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   437
  "M #\<inter> {#} = {#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   438
  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
   439
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   440
lemma inter_add_left1:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   441
  "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   442
  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
   443
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   444
lemma inter_add_left2:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   445
  "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   446
  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
   447
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   448
lemma inter_add_right1:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   449
  "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   450
  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
   451
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   452
lemma inter_add_right2:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   453
  "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   454
  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
   455
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   456
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   457
subsubsection \<open>Bounded union\<close>
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   458
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "#\<union>" 70)  where
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   459
  "sup_subset_mset A B = A + (B - A)"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   460
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   461
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<le>#" "op <#"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   462
proof -
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   463
  have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" by arith
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   464
  show "class.semilattice_sup op #\<union> op \<le># op <#"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   465
    by default (auto simp add: sup_subset_mset_def subseteq_mset_def aux)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   466
qed
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   467
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   468
lemma sup_subset_mset_count [simp]:
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   469
  "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
   470
  by (simp add: sup_subset_mset_def)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   471
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   472
lemma empty_sup [simp]:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   473
  "{#} #\<union> M = M"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   474
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   475
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   476
lemma sup_empty [simp]:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   477
  "M #\<union> {#} = M"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   478
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   479
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   480
lemma sup_add_left1:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   481
  "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   482
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   483
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   484
lemma sup_add_left2:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   485
  "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   486
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   487
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   488
lemma sup_add_right1:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   489
  "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   490
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   491
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   492
lemma sup_add_right2:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   493
  "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   494
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   495
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   496
subsubsection \<open>Subset is an order\<close>
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   497
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   498
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   499
subsubsection \<open>Filter (with comprehension syntax)\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   501
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
   502
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   503
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
   504
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
   505
by (rule filter_preserves_multiset)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   506
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   507
lemma count_filter_mset [simp]:
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   508
  "count (filter_mset P M) a = (if P a then count M a else 0)"
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   509
  by (simp add: filter_mset.rep_eq)
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   510
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   511
lemma filter_empty_mset [simp]:
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   512
  "filter_mset P {#} = {#}"
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   513
  by (rule multiset_eqI) simp
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   514
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   515
lemma filter_single_mset [simp]:
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   516
  "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
   517
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   518
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   519
lemma filter_union_mset [simp]:
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   520
  "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
   521
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   522
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   523
lemma filter_diff_mset [simp]:
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   524
  "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
   525
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   526
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   527
lemma filter_inter_mset [simp]:
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   528
  "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
   529
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   530
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   531
lemma multiset_filter_subset[simp]: "filter_mset f M \<le># M"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   532
  by (simp add: mset_less_eqI)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   533
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   534
lemma multiset_filter_mono: assumes "A \<le># B"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   535
  shows "filter_mset f A \<le># filter_mset f B"
58035
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   536
proof -
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   537
  from assms[unfolded mset_le_exists_conv]
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   538
  obtain C where B: "B = A + C" by auto
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   539
  show ?thesis unfolding B by auto
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   540
qed
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
   541
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   542
syntax
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   543
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   544
syntax (xsymbol)
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   545
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   546
translations
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   547
  "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   548
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   549
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   550
subsubsection \<open>Set of elements\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   551
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   552
definition set_mset :: "'a multiset => 'a set" where
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   553
  "set_mset M = {x. x :# M}"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   554
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   555
lemma set_mset_empty [simp]: "set_mset {#} = {}"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   556
by (simp add: set_mset_def)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   557
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   558
lemma set_mset_single [simp]: "set_mset {#b#} = {b}"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   559
by (simp add: set_mset_def)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   560
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   561
lemma set_mset_union [simp]: "set_mset (M + N) = set_mset M \<union> set_mset N"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   562
by (auto simp add: set_mset_def)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   563
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   564
lemma set_mset_eq_empty_iff [simp]: "(set_mset M = {}) = (M = {#})"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   565
by (auto simp add: set_mset_def multiset_eq_iff)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   566
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   567
lemma mem_set_mset_iff [simp]: "(x \<in> set_mset M) = (x :# M)"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   568
by (auto simp add: set_mset_def)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   569
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   570
lemma set_mset_filter [simp]: "set_mset {# x:#M. P x #} = set_mset M \<inter> {x. P x}"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   571
by (auto simp add: set_mset_def)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   572
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   573
lemma finite_set_mset [iff]: "finite (set_mset M)"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   574
  using count [of M] by (simp add: multiset_def set_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
   575
46756
faf62905cd53 adding finiteness of intervals on integer sets; adding another finiteness theorem for multisets
bulwahn
parents: 46730
diff changeset
   576
lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   577
  unfolding set_mset_def[symmetric] by simp
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   578
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   579
lemma set_mset_mono: "A \<le># B \<Longrightarrow> set_mset A \<subseteq> set_mset B"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   580
  by (metis mset_leD subsetI mem_set_mset_iff)
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   581
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   582
lemma ball_set_mset_iff: "(\<forall>x \<in> set_mset M. P x) \<longleftrightarrow> (\<forall>x. x \<in># M \<longrightarrow> P x)"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   583
  by auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   584
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   585
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   586
subsubsection \<open>Size\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   587
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   588
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   589
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   590
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
   591
  by (auto simp: wcount_def add_mult_distrib)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   592
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   593
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
   594
  "size_multiset f M = setsum (wcount f M) (set_mset M)"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   595
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   596
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   597
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   598
instantiation multiset :: (type) size begin
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   599
definition size_multiset where
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   600
  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
   601
instance ..
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
   602
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
   603
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   604
lemmas size_multiset_overloaded_eq =
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   605
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   606
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   607
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   608
by (simp add: size_multiset_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   609
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   610
lemma size_empty [simp]: "size {#} = 0"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   611
by (simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   612
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   613
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   614
by (simp add: size_multiset_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   615
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   616
lemma size_single [simp]: "size {#b#} = 1"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   617
by (simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   618
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   619
lemma setsum_wcount_Int:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   620
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A"
26178
nipkow
parents: 26176
diff changeset
   621
apply (induct rule: finite_induct)
nipkow
parents: 26176
diff changeset
   622
 apply simp
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   623
apply (simp add: Int_insert_left set_mset_def wcount_def)
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   624
done
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   625
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   626
lemma size_multiset_union [simp]:
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   627
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
57418
6ab1c7cb0b8d fact consolidation
haftmann
parents: 56656
diff changeset
   628
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
   629
apply (subst Int_commute)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   630
apply (simp add: setsum_wcount_Int)
26178
nipkow
parents: 26176
diff changeset
   631
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   632
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   633
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   634
by (auto simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   635
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   636
lemma size_multiset_eq_0_iff_empty [iff]: "(size_multiset f M = 0) = (M = {#})"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   637
by (auto simp add: size_multiset_eq multiset_eq_iff)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   638
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   639
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   640
by (auto simp add: size_multiset_overloaded_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   641
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   642
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
26178
nipkow
parents: 26176
diff changeset
   643
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
   644
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   645
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
   646
apply (unfold size_multiset_overloaded_eq)
26178
nipkow
parents: 26176
diff changeset
   647
apply (drule setsum_SucD)
nipkow
parents: 26176
diff changeset
   648
apply auto
nipkow
parents: 26176
diff changeset
   649
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   650
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
   651
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
   652
  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
   653
  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
   654
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
   655
  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
   656
    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
   657
  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
   658
  then show ?thesis by blast
23611
65b168646309 more interpretations
nipkow
parents: 23373
diff changeset
   659
qed
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   660
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   661
lemma size_mset_mono: assumes "A \<le># B"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   662
  shows "size A \<le> size(B::_ multiset)"
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   663
proof -
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   664
  from assms[unfolded mset_le_exists_conv]
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   665
  obtain C where B: "B = A + C" by auto
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   666
  show ?thesis unfolding B by (induct C, auto)
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   667
qed
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   668
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   669
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
   670
by (rule size_mset_mono[OF multiset_filter_subset])
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   671
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   672
lemma size_Diff_submset:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   673
  "M \<le># 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
   674
by (metis add_diff_cancel_left' size_union mset_le_exists_conv)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   675
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   676
subsection \<open>Induction and case splits\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   677
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
   678
theorem multiset_induct [case_names empty add, induct type: multiset]:
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   679
  assumes empty: "P {#}"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   680
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   681
  shows "P M"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   682
proof (induct n \<equiv> "size M" arbitrary: M)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   683
  case 0 thus "P M" by (simp add: empty)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   684
next
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   685
  case (Suc k)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   686
  obtain N x where "M = N + {#x#}"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   687
    using \<open>Suc k = size M\<close> [symmetric]
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   688
    using size_eq_Suc_imp_eq_union by fast
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   689
  with Suc add show "P M" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   690
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   691
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   692
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
26178
nipkow
parents: 26176
diff changeset
   693
by (induct M) auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   694
55913
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   695
lemma multiset_cases [cases type]:
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   696
  obtains (empty) "M = {#}"
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   697
    | (add) N x where "M = N + {#x#}"
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   698
  using assms by (induct M) simp_all
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   699
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
   700
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
   701
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
   702
26033
278025d5282d modified MCollect syntax
nipkow
parents: 26016
diff changeset
   703
lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#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
   704
apply (subst multiset_eq_iff)
26178
nipkow
parents: 26176
diff changeset
   705
apply auto
nipkow
parents: 26176
diff changeset
   706
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   707
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   708
lemma mset_less_size: "(A::'a multiset) <# 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
   709
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
   710
  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
   711
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   712
  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
   713
    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
   714
  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
   715
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
   716
  case (add S x T)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   717
  have IH: "\<And>B. S <# B \<Longrightarrow> size S < size B" by fact
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   718
  have SxsubT: "S + {#x#} <# T" by fact
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   719
  then have "x \<in># T" and "S <# T" by (auto dest: mset_less_insertD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   720
  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
   721
    by (blast dest: multi_member_split)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   722
  then have "S <# 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
   723
    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
   724
  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
   725
  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
   726
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
   727
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
   728
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   729
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
   730
by (cases M) auto
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
   731
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   732
subsubsection \<open>Strong induction and subset induction for multisets\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   733
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   734
text \<open>Well-foundedness of strict subset relation\<close>
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   735
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   736
lemma wf_less_mset_rel: "wf {(M, N :: 'a multiset). M <# 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
   737
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
   738
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
   739
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
   740
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
   741
lemma full_multiset_induct [case_names less]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   742
assumes ih: "\<And>B. \<forall>(A::'a multiset). A <# 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
   743
shows "P B"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   744
apply (rule wf_less_mset_rel [THEN wf_induct])
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   745
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
   746
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
   747
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
   748
lemma multi_subset_induct [consumes 2, case_names empty add]:
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   749
assumes "F \<le># 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
   750
  and empty: "P {#}"
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
   751
  and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#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
   752
shows "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
   753
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   754
  from \<open>F \<le># 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
   755
  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
   756
  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
   757
    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
   758
  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
   759
    fix x F
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   760
    assume P: "F \<le># A \<Longrightarrow> P F" and i: "F + {#x#} \<le># 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
   761
    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
   762
    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
   763
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   764
      from i have "F \<le># 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
   765
      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
   766
    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
   767
  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
   768
qed
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   769
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   770
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   771
subsection \<open>The fold combinator\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   772
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   773
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   774
where
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   775
  "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
   776
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   777
lemma fold_mset_empty [simp]:
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   778
  "fold_mset f s {#} = s"
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   779
  by (simp add: fold_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   780
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   781
context comp_fun_commute
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   782
begin
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   783
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   784
lemma fold_mset_insert:
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   785
  "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
   786
proof -
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   787
  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
   788
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   789
  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
   790
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   791
  show ?thesis
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   792
  proof (cases "x \<in> set_mset M")
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   793
    case False
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   794
    then have *: "count (M + {#x#}) x = 1" by simp
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   795
    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
   796
      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
   797
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   798
    with False * show ?thesis
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   799
      by (simp add: fold_mset_def del: count_union)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   800
  next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   801
    case True
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   802
    def N \<equiv> "set_mset M - {x}"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   803
    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
   804
    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
   805
      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
   806
      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
   807
    with * show ?thesis by (simp add: fold_mset_def del: count_union) simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   808
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   809
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   810
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   811
corollary fold_mset_single [simp]:
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   812
  "fold_mset f s {#x#} = f x s"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   813
proof -
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   814
  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
   815
  then show ?thesis by simp
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   816
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   817
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
   818
lemma fold_mset_fun_left_comm:
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   819
  "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
   820
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   821
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   822
lemma fold_mset_union [simp]:
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   823
  "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
   824
proof (induct M)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   825
  case empty then show ?case by simp
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   826
next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   827
  case (add M x)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   828
  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
   829
    by (simp add: ac_simps)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
   830
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   831
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   832
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   833
lemma fold_mset_fusion:
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   834
  assumes "comp_fun_commute g"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   835
  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold_mset g w A) = fold_mset f (h w) A" (is "PROP ?P")
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   836
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   837
  interpret comp_fun_commute g by (fact assms)
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   838
  show "PROP ?P" by (induct A) auto
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   839
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   840
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   841
end
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   842
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   843
text \<open>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   844
  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
   845
  subterm @{term "fold_mset F"}, code generation is not automatic. When
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   846
  interpreting locale @{text left_commutative} with @{text F}, the
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   847
  would be code thms for @{const fold_mset} become thms like
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   848
  @{term "fold_mset F z {#} = z"} where @{text F} is not a pattern but
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   849
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   850
  constant with its own code thms needs to be introduced for @{text
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   851
  F}. See the image operator below.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   852
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   853
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   854
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   855
subsection \<open>Image\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   856
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   857
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
   858
  "image_mset f = fold_mset (plus o single o f) {#}"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   859
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   860
lemma comp_fun_commute_mset_image:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   861
  "comp_fun_commute (plus o single o f)"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   862
proof
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
   863
qed (simp add: ac_simps fun_eq_iff)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   864
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   865
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   866
  by (simp add: image_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   867
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   868
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   869
proof -
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   870
  interpret comp_fun_commute "plus o single o f"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   871
    by (fact comp_fun_commute_mset_image)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   872
  show ?thesis by (simp add: image_mset_def)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   873
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   874
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   875
lemma image_mset_union [simp]:
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   876
  "image_mset f (M + N) = image_mset f M + image_mset f N"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   877
proof -
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   878
  interpret comp_fun_commute "plus o single o f"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   879
    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
   880
  show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps)
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   881
qed
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   882
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   883
corollary image_mset_insert:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   884
  "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   885
  by simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   886
60498
c8141ac6f03f more compact name
nipkow
parents: 60495
diff changeset
   887
lemma set_image_mset [simp]:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   888
  "set_mset (image_mset f M) = image f (set_mset M)"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   889
  by (induct M) simp_all
48040
4caf6cd063be add lemma set_of_image_mset
huffman
parents: 48023
diff changeset
   890
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   891
lemma size_image_mset [simp]:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   892
  "size (image_mset f M) = size M"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   893
  by (induct M) simp_all
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   894
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   895
lemma image_mset_is_empty_iff [simp]:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   896
  "image_mset f M = {#} \<longleftrightarrow> M = {#}"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   897
  by (cases M) auto
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   898
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   899
syntax
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   900
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   901
      ("({#_/. _ :# _#})")
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   902
translations
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   903
  "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   904
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   905
syntax (xsymbols)
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   906
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   907
      ("({#_/. _ \<in># _#})")
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   908
translations
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   909
  "{#e. x \<in># M#}" == "CONST image_mset (\<lambda>x. e) M"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   910
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   911
syntax
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   912
  "_comprehension3_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   913
      ("({#_/ | _ :# _./ _#})")
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   914
translations
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   915
  "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   916
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   917
syntax
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   918
  "_comprehension4_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   919
      ("({#_/ | _ \<in># _./ _#})")
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   920
translations
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   921
  "{#e | x\<in>#M. P#}" => "{#e. x \<in># {# x\<in>#M. P#}#}"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   922
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   923
text \<open>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   924
  This allows to write not just filters like @{term "{#x:#M. x<c#}"}
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   925
  but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   926
  "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   927
  @{term "{#x+x|x:#M. x<c#}"}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   928
\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   929
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
   930
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
60498
c8141ac6f03f more compact name
nipkow
parents: 60495
diff changeset
   931
by (metis mem_set_mset_iff set_image_mset)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   932
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
   933
functor image_mset: image_mset
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   934
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   935
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   936
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   937
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   938
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   939
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   940
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   941
  show "image_mset id = id"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   942
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   943
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   944
    show "image_mset id A = id A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   945
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   946
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   947
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   948
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   949
declare
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   950
  image_mset.id [simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   951
  image_mset.identity [simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   952
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   953
lemma image_mset_id[simp]: "image_mset id x = x"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   954
  unfolding id_def by auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   955
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   956
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
   957
  by (induct M) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   958
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   959
lemma image_mset_cong_pair:
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   960
  "(\<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
   961
  by (metis image_mset_cong split_cong)
49717
56494eedf493 default simp rule for image under identity
haftmann
parents: 49394
diff changeset
   962
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   963
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   964
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
   965
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   966
primrec mset :: "'a list \<Rightarrow> 'a multiset" where
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   967
  "mset [] = {#}" |
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   968
  "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
   969
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   970
lemma in_multiset_in_set:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   971
  "x \<in># mset xs \<longleftrightarrow> x \<in> set xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   972
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   973
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   974
lemma count_mset:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   975
  "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   976
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   977
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   978
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
   979
  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
   980
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   981
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
   982
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
   983
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   984
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
   985
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
   986
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   987
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# mset xs)"
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
   988
by (induct xs) 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
   989
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   990
lemma size_mset [simp]: "size (mset xs) = length xs"
48012
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
   991
  by (induct xs) simp_all
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
   992
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   993
lemma mset_append [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   994
  "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
   995
  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
   996
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   997
lemma mset_filter:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
   998
  "mset (filter P xs) = {#x :# mset xs. P x #}"
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   999
  by (induct xs) simp_all
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1000
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1001
lemma mset_rev [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1002
  "mset (rev xs) = mset xs"
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
  1003
  by (induct xs) simp_all
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
  1004
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1005
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
  1006
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
  1007
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
  1008
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
  1009
 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
  1010
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
  1011
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
  1012
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
  1013
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1014
lemma set_count_greater_0: "set x = {a. count (mset x) a > 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
  1015
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
  1016
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
  1017
lemma distinct_count_atmost_1:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1018
  "distinct x = (! a. count (mset x) a = (if a \<in> set x then 1 else 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
  1019
apply (induct x, simp, rule iffI, simp_all)
55417
01fbfb60c33e adapted to 'xxx_{case,rec}' renaming, to new theorem names, and to new variable names in theorems
blanchet
parents: 55129
diff changeset
  1020
apply (rename_tac a 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
  1021
apply (rule conjI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1022
apply (simp_all add: set_mset_mset [THEN sym] del: set_mset_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
  1023
apply (erule_tac x = a in allE, simp, clarify)
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
  1024
apply (erule_tac x = aa in allE, 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
  1025
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
  1026
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1027
lemma mset_eq_setD:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1028
  "mset xs = mset ys \<Longrightarrow> set xs = set ys"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1029
by (rule) (auto simp add:multiset_eq_iff set_count_greater_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
  1030
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1031
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
  1032
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1033
    (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
  1034
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
  1035
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1036
lemma set_eq_iff_mset_remdups_eq:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1037
   "(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
  1038
apply (rule iffI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1039
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
  1040
apply (drule distinct_remdups [THEN distinct_remdups
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1041
      [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
  1042
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
  1043
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
  1044
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1045
lemma mset_compl_union [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1046
  "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
  1047
  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
  1048
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1049
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) :# mset ls"
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
  1050
apply (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
  1051
 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
  1052
apply (case_tac 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
  1053
 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
  1054
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
  1055
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1056
lemma mset_remove1[simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1057
  "mset (remove1 a xs) = mset xs - {#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
  1058
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
  1059
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1060
lemma mset_eq_length:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1061
  assumes "mset xs = mset ys"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1062
  shows "length xs = length ys"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1063
  using assms by (metis size_mset)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1064
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1065
lemma mset_eq_length_filter:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1066
  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
  1067
  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
  1068
  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
  1069
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1070
lemma fold_multiset_equiv:
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1071
  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
  1072
    and equiv: "mset xs = mset ys"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1073
  shows "List.fold f xs = List.fold f ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1074
using f equiv [symmetric]
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1075
proof (induct xs arbitrary: ys)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1076
  case Nil then show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1077
next
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1078
  case (Cons x xs)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1079
  then have *: "set ys = set (x # xs)" by (blast dest: mset_eq_setD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1080
  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
  1081
    by (rule Cons.prems(1)) (simp_all add: *)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1082
  moreover from * have "x \<in> set ys" by simp
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1083
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1084
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1085
  ultimately show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1086
qed
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1087
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1088
lemma mset_insort [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1089
  "mset (insort x xs) = mset xs + {#x#}"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1090
  by (induct xs) (simp_all add: ac_simps)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1091
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1092
lemma mset_map:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1093
  "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
  1094
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1095
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1096
definition mset_set :: "'a set \<Rightarrow> 'a multiset"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1097
where
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1098
  "mset_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1099
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1100
interpretation mset_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1101
where
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1102
  "folding.F (\<lambda>x M. {#x#} + M) {#} = mset_set"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1103
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1104
  interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1105
  show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1106
  from mset_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = mset_set" ..
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1107
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1108
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1109
lemma count_mset_set [simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1110
  "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
  1111
  "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1112
  "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
  1113
proof -
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1114
  { fix A
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1115
    assume "x \<notin> A"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1116
    have "count (mset_set A) x = 0"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1117
    proof (cases "finite A")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1118
      case False then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1119
    next
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1120
      case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1121
    qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1122
  } note * = this
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1123
  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
  1124
  by (auto elim!: Set.set_insert)
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1125
qed -- \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1126
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1127
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
  1128
  by (induct A rule: finite_induct) simp_all
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1129
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1130
context linorder
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1131
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1132
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1133
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1134
where
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1135
  "sorted_list_of_multiset M = fold_mset insort [] M"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1136
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1137
lemma sorted_list_of_multiset_empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1138
  "sorted_list_of_multiset {#} = []"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1139
  by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1140
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1141
lemma sorted_list_of_multiset_singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1142
  "sorted_list_of_multiset {#x#} = [x]"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1143
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1144
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1145
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1146
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1147
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1148
lemma sorted_list_of_multiset_insert [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1149
  "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
  1150
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1151
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1152
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1153
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1154
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1155
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1156
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1157
lemma mset_sorted_list_of_multiset [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1158
  "mset (sorted_list_of_multiset M) = M"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1159
by (induct M) simp_all
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1160
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1161
lemma sorted_list_of_multiset_mset [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1162
  "sorted_list_of_multiset (mset xs) = sort xs"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1163
by (induct xs) simp_all
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1164
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1165
lemma finite_set_mset_mset_set[simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1166
  "finite A \<Longrightarrow> set_mset (mset_set A) = A"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1167
by (induct A rule: finite_induct) simp_all
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1168
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1169
lemma infinite_set_mset_mset_set:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1170
  "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1171
by simp
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1172
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1173
lemma set_sorted_list_of_multiset [simp]:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1174
  "set (sorted_list_of_multiset M) = set_mset M"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1175
by (induct M) (simp_all add: set_insort)
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1176
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1177
lemma sorted_list_of_mset_set [simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1178
  "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1179
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
  1180
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1181
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1182
subsection \<open>Big operators\<close>
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1183
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1184
no_notation times (infixl "*" 70)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1185
no_notation Groups.one ("1")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1186
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1187
locale comm_monoid_mset = comm_monoid
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1188
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1189
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1190
definition F :: "'a multiset \<Rightarrow> 'a"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1191
where
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1192
  eq_fold: "F M = fold_mset f 1 M"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1193
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1194
lemma empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1195
  "F {#} = 1"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1196
  by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1197
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1198
lemma singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1199
  "F {#x#} = x"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1200
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1201
  interpret comp_fun_commute
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1202
    by default (simp add: fun_eq_iff left_commute)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1203
  show ?thesis by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1204
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1205
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1206
lemma union [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1207
  "F (M + N) = F M * F N"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1208
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1209
  interpret comp_fun_commute f
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1210
    by default (simp add: fun_eq_iff left_commute)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1211
  show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1212
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1213
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1214
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1215
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1216
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + \<Colon> 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1217
  by default (simp add: add_ac comp_def)
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1218
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1219
declare comp_fun_commute.fold_mset_insert[OF comp_fun_commute_plus_mset, simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1220
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1221
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
  1222
  by (induct NN) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1223
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1224
notation times (infixl "*" 70)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1225
notation Groups.one ("1")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1226
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1227
context comm_monoid_add
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1228
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1229
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1230
definition msetsum :: "'a multiset \<Rightarrow> 'a"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1231
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1232
  "msetsum = comm_monoid_mset.F plus 0"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1233
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1234
sublocale msetsum!: comm_monoid_mset plus 0
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1235
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1236
  "comm_monoid_mset.F plus 0 = msetsum"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1237
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1238
  show "comm_monoid_mset plus 0" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1239
  from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1240
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1241
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1242
lemma setsum_unfold_msetsum:
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1243
  "setsum f A = msetsum (image_mset f (mset_set A))"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1244
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1245
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1246
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1247
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1248
lemma msetsum_diff:
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1249
  fixes M N :: "('a \<Colon> ordered_cancel_comm_monoid_diff) multiset"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1250
  shows "N \<le># M \<Longrightarrow> msetsum (M - N) = msetsum M - msetsum N"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1251
  by (metis add_diff_cancel_right' msetsum.union subset_mset.diff_add)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1252
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1253
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
  1254
proof (induct M)
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1255
  case empty then show ?case by simp
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1256
next
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1257
  case (add M x) then show ?case
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1258
    by (cases "x \<in> set_mset M")
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1259
      (simp_all del: mem_set_mset_iff add: size_multiset_overloaded_eq setsum.distrib setsum.delta' insert_absorb, simp)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1260
qed
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1261
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1262
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1263
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset" where
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1264
  "Union_mset MM \<equiv> msetsum MM"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1265
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1266
notation (xsymbols) Union_mset ("\<Union>#_" [900] 900)
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1267
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1268
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
  1269
  by (induct MM) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1270
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1271
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
  1272
  by (induct MM) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1273
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1274
syntax
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1275
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1276
      ("(3SUM _:#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1277
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1278
syntax (xsymbols)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1279
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
57518
2f640245fc6d refrain from auxiliary abbreviation: be more explicit to the reader in situations where syntax translation does not apply;
haftmann
parents: 57514
diff changeset
  1280
      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1281
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1282
syntax (HTML output)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1283
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1284
      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1285
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1286
translations
57518
2f640245fc6d refrain from auxiliary abbreviation: be more explicit to the reader in situations where syntax translation does not apply;
haftmann
parents: 57514
diff changeset
  1287
  "SUM i :# A. b" == "CONST msetsum (CONST image_mset (\<lambda>i. b) A)"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1288
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1289
context comm_monoid_mult
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1290
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1291
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1292
definition msetprod :: "'a multiset \<Rightarrow> 'a"
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1293
where
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1294
  "msetprod = comm_monoid_mset.F times 1"
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1295
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1296
sublocale msetprod!: comm_monoid_mset times 1
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1297
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1298
  "comm_monoid_mset.F times 1 = msetprod"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1299
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1300
  show "comm_monoid_mset times 1" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1301
  from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1302
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1303
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1304
lemma msetprod_empty:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1305
  "msetprod {#} = 1"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1306
  by (fact msetprod.empty)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1307
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1308
lemma msetprod_singleton:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1309
  "msetprod {#x#} = x"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1310
  by (fact msetprod.singleton)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1311
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1312
lemma msetprod_Un:
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1313
  "msetprod (A + B) = msetprod A * msetprod B"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1314
  by (fact msetprod.union)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1315
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1316
lemma setprod_unfold_msetprod:
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1317
  "setprod f A = msetprod (image_mset f (mset_set A))"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1318
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1319
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1320
lemma msetprod_multiplicity:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1321
  "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
  1322
  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
  1323
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1324
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1325
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1326
syntax
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1327
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1328
      ("(3PROD _:#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1329
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1330
syntax (xsymbols)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1331
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1332
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1333
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1334
syntax (HTML output)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1335
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1336
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1337
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1338
translations
57518
2f640245fc6d refrain from auxiliary abbreviation: be more explicit to the reader in situations where syntax translation does not apply;
haftmann
parents: 57514
diff changeset
  1339
  "PROD i :# A. b" == "CONST msetprod (CONST image_mset (\<lambda>i. b) A)"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1340
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1341
lemma (in comm_semiring_1) dvd_msetprod:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1342
  assumes "x \<in># A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1343
  shows "x dvd msetprod A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1344
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1345
  from assms have "A = (A - {#x#}) + {#x#}" by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1346
  then obtain B where "A = B + {#x#}" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1347
  then show ?thesis by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1348
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1349
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1350
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1351
subsection \<open>Replicate operation\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1352
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1353
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1354
  "replicate_mset n x = ((op + {#x#}) ^^ n) {#}"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1355
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1356
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1357
  unfolding replicate_mset_def by simp
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1358
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1359
lemma replicate_mset_Suc[simp]: "replicate_mset (Suc n) x = replicate_mset n x + {#x#}"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1360
  unfolding replicate_mset_def by (induct n) (auto intro: add.commute)
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1361
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1362
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1363
  unfolding replicate_mset_def by (induct n) simp_all
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1364
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1365
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1366
  unfolding replicate_mset_def by (induct n) simp_all
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1367
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1368
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1369
  by (auto split: if_splits)
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1370
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1371
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1372
  by (induct n, simp_all)
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1373
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1374
lemma count_le_replicate_mset_le: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<le># M"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1375
  by (auto simp add: assms mset_less_eqI) (metis count_replicate_mset subseteq_mset_def)
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1376
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1377
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1378
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1379
  by (induct D) simp_all
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1380
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1381
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1382
subsection \<open>Alternative representations\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1383
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1384
subsubsection \<open>Lists\<close>
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1385
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1386
context linorder
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1387
begin
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1388
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1389
lemma mset_insort [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1390
  "mset (insort_key k x xs) = {#x#} + mset xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1391
  by (induct xs) (simp_all add: ac_simps)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1392
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1393
lemma mset_sort [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1394
  "mset (sort_key k xs) = mset xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1395
  by (induct xs) (simp_all add: ac_simps)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1396
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1397
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
  1398
  This lemma shows which properties suffice to show that a function
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
  1399
  @{text "f"} with @{text "f xs = ys"} behaves like sort.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1400
\<close>
37074
322d065ebef7 localized properties_for_sort
haftmann
parents: 36903
diff changeset
  1401
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1402
lemma properties_for_sort_key:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1403
  assumes "mset ys = mset xs"
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1404
  and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f 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
  1405
  and "sorted (map f ys)"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1406
  shows "sort_key f xs = ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1407
using assms
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1408
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
  1409
  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
  1410
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
  1411
  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
  1412
  from Cons.prems(2) have
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1413
    "\<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
  1414
    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
  1415
  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
  1416
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1417
  moreover from Cons.prems have "x \<in> set ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1418
    by (auto simp add: mem_set_multiset_eq intro!: ccontr)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1419
  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
  1420
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
  1421
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1422
lemma properties_for_sort:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1423
  assumes multiset: "mset ys = mset xs"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1424
  and "sorted ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1425
  shows "sort xs = ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1426
proof (rule properties_for_sort_key)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1427
  from multiset show "mset ys = mset xs" .
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1428
  from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1429
  from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1430
    by (rule mset_eq_length_filter)
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1431
  then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1432
    by simp
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1433
  then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1434
    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
  1435
qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1436
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1437
lemma sort_key_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1438
  "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
  1439
    @ [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
  1440
    @ 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
  1441
proof (rule properties_for_sort_key)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1442
  show "mset ?rhs = mset ?lhs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1443
    by (rule multiset_eqI) (auto simp add: mset_filter)
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1444
next
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1445
  show "sorted (map f ?rhs)"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1446
    by (auto simp add: sorted_append intro: sorted_map_same)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1447
next
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1448
  fix l
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1449
  assume "l \<in> set ?rhs"
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1450
  let ?pivot = "f (xs ! (length xs div 2))"
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1451
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1452
  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
  1453
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1454
  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
  1455
  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
  1456
  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
  1457
    [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
  1458
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1459
  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
  1460
  proof (cases "f l" ?pivot rule: linorder_cases)
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1461
    case less
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1462
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1463
    with less show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1464
      by (simp add: filter_sort [symmetric] ** ***)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1465
  next
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1466
    case equal then show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1467
      by (simp add: * less_le)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1468
  next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1469
    case greater
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1470
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1471
    with greater show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1472
      by (simp add: filter_sort [symmetric] ** ***)
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1473
  qed
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1474
qed
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1475
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1476
lemma sort_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1477
  "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
  1478
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1479
    @ 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
  1480
  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
  1481
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1482
text \<open>A stable parametrized quicksort\<close>
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1483
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1484
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
  1485
  "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
  1486
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1487
lemma part_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1488
  "part f pivot [] = ([], [], [])"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1489
  "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
  1490
     if x' < pivot then (x # lts, eqs, gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1491
     else if x' > pivot then (lts, eqs, x # gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1492
     else (lts, x # eqs, gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1493
  by (auto simp add: part_def Let_def split_def)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1494
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1495
lemma sort_key_by_quicksort_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1496
  "sort_key f xs = (case xs of [] \<Rightarrow> []
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1497
    | [x] \<Rightarrow> xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1498
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1499
    | _ \<Rightarrow> (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
  1500
       in sort_key f lts @ eqs @ sort_key f gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1501
proof (cases xs)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1502
  case Nil then show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1503
next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1504
  case (Cons _ ys) note hyps = Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1505
  proof (cases ys)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1506
    case Nil with hyps show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1507
  next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1508
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1509
    proof (cases zs)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1510
      case Nil with hyps show ?thesis by auto
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1511
    next
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1512
      case Cons
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1513
      from sort_key_by_quicksort [of f xs]
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1514
      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
  1515
        in sort_key f lts @ eqs @ sort_key f gts)"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1516
      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
  1517
      with hyps Cons show ?thesis by (simp only: list.cases)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1518
    qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1519
  qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1520
qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1521
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1522
end
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1523
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1524
hide_const (open) part
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1525
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1526
lemma mset_remdups_le: "mset (remdups xs) \<le># mset xs"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1527
  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
  1528
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1529
lemma mset_update:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1530
  "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
  1531
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
  1532
  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
  1533
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
  1534
  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
  1535
  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
  1536
  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
  1537
    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
  1538
  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
  1539
    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
  1540
    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
  1541
      apply simp
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1542
      apply (subst add.assoc)
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1543
      apply (subst add.commute [of "{#v#}" "{#x#}"])
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1544
      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
  1545
      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
  1546
      apply (rule mset_le_multiset_union_diff_commute)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1547
      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
  1548
      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
  1549
  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
  1550
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
  1551
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1552
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
  1553
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1554
    mset (ls[j := ls ! i, i := ls ! j]) = mset ls"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1555
  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
  1556
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
  1557
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1558
subsection \<open>The multiset order\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1559
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1560
subsubsection \<open>Well-foundedness\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1561
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1562
definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1563
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1564
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1565
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1566
definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1567
  "mult r = (mult1 r)\<^sup>+"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1568
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1569
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1570
by (simp add: mult1_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1571
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1572
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1573
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1574
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
19582
a669c98b9c24 get rid of 'concl is';
wenzelm
parents: 19564
diff changeset
  1575
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1576
proof (unfold mult1_def)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1577
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1578
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1579
  let ?case1 = "?case1 {(N, M). ?R N M}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1580
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1581
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1582
  then have "\<exists>a' M0' K.
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1583
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1584
  then show "?case1 \<or> ?case2"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1585
  proof (elim exE conjE)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1586
    fix a' M0' K
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1587
    assume N: "N = M0' + K" and r: "?r K a'"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1588
    assume "M0 + {#a#} = M0' + {#a'#}"
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1589
    then have "M0 = M0' \<and> a = a' \<or>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1590
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1591
      by (simp only: add_eq_conv_ex)
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1592
    then show ?thesis
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1593
    proof (elim disjE conjE exE)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1594
      assume "M0 = M0'" "a = a'"
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1595
      with N r have "?r K a \<and> N = M0 + K" by simp
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1596
      then have ?case2 .. then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1597
    next
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1598
      fix K'
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1599
      assume "M0' = K' + {#a#}"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1600
      with N have n: "N = K' + K + {#a#}" by (simp add: ac_simps)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1601
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1602
      assume "M0 = K' + {#a'#}"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1603
      with r have "?R (K' + K) M0" by blast
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1604
      with n have ?case1 by simp then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1605
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1606
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1607
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1608
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 52289
diff changeset
  1609
lemma all_accessible: "wf r ==> \<forall>M. M \<in> Wellfounded.acc (mult1 r)"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1610
proof
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1611
  let ?R = "mult1 r"
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 52289
diff changeset
  1612
  let ?W = "Wellfounded.acc ?R"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1613
  {
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1614
    fix M M0 a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1615
    assume M0: "M0 \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1616
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1617
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1618
    have "M0 + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1619
    proof (rule accI [of "M0 + {#a#}"])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1620
      fix N
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1621
      assume "(N, M0 + {#a#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1622
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1623
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1624
        by (rule less_add)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1625
      then show "N \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1626
      proof (elim exE disjE conjE)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1627
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1628
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1629
        from this and \<open>(M, M0) \<in> ?R\<close> have "M + {#a#} \<in> ?W" ..
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1630
        then show "N \<in> ?W" by (simp only: N)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1631
      next
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1632
        fix K
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1633
        assume N: "N = M0 + K"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1634
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1635
        then have "M0 + K \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1636
        proof (induct K)
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1637
          case empty
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1638
          from M0 show "M0 + {#} \<in> ?W" by simp
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1639
        next
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1640
          case (add K x)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1641
          from add.prems have "(x, a) \<in> r" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1642
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1643
          moreover from add have "M0 + K \<in> ?W" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1644
          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
  1645
          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
  1646
        qed
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1647
        then show "N \<in> ?W" by (simp only: N)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1648
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1649
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1650
  } note tedious_reasoning = this
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1651
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1652
  assume wf: "wf r"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1653
  fix M
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1654
  show "M \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1655
  proof (induct M)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1656
    show "{#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1657
    proof (rule accI)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1658
      fix b assume "(b, {#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1659
      with not_less_empty show "b \<in> ?W" by contradiction
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1660
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1661
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1662
    fix M a assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1663
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1664
    proof induct
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1665
      fix a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1666
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1667
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1668
      proof
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1669
        fix M assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1670
        then show "M + {#a#} \<in> ?W"
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 23281
diff changeset
  1671
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1672
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1673
    qed
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1674
    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
  1675
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1676
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1677
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1678
theorem wf_mult1: "wf r ==> wf (mult1 r)"
26178
nipkow
parents: 26176
diff changeset
  1679
by (rule acc_wfI) (rule all_accessible)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1680
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1681
theorem wf_mult: "wf r ==> wf (mult r)"
26178
nipkow
parents: 26176
diff changeset
  1682
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1683
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1684
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1685
subsubsection \<open>Closure-free presentation\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1686
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1687
text \<open>One direction.\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1688
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1689
lemma mult_implies_one_step:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1690
  "trans r ==> (M, N) \<in> mult r ==>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1691
    \<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
  1692
    (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1693
apply (unfold mult_def mult1_def set_mset_def)
26178
nipkow
parents: 26176
diff changeset
  1694
apply (erule converse_trancl_induct, clarify)
nipkow
parents: 26176
diff changeset
  1695
 apply (rule_tac x = M0 in exI, simp, clarify)
nipkow
parents: 26176
diff changeset
  1696
apply (case_tac "a :# K")
nipkow
parents: 26176
diff changeset
  1697
 apply (rule_tac x = I in exI)
nipkow
parents: 26176
diff changeset
  1698
 apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  1699
 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
  1700
 apply (simp (no_asm_simp) add: add.assoc [symmetric])
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59625
diff changeset
  1701
 apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong)
26178
nipkow
parents: 26176
diff changeset
  1702
 apply (simp add: diff_union_single_conv)
nipkow
parents: 26176
diff changeset
  1703
 apply (simp (no_asm_use) add: trans_def)
nipkow
parents: 26176
diff changeset
  1704
 apply blast
nipkow
parents: 26176
diff changeset
  1705
apply (subgoal_tac "a :# I")
nipkow
parents: 26176
diff changeset
  1706
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  1707
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  1708
 apply (rule_tac x = "K + Ka" in exI)
nipkow
parents: 26176
diff changeset
  1709
 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
  1710
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  1711
 apply (rule conjI)
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59625
diff changeset
  1712
  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
  1713
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  1714
 apply (simp (no_asm_use) add: trans_def)
nipkow
parents: 26176
diff changeset
  1715
 apply blast
nipkow
parents: 26176
diff changeset
  1716
apply (subgoal_tac "a :# (M0 + {#a#})")
nipkow
parents: 26176
diff changeset
  1717
 apply simp
nipkow
parents: 26176
diff changeset
  1718
apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  1719
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1720
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1721
lemma one_step_implies_mult_aux:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1722
  "trans r ==>
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1723
    \<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))
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1724
      --> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  1725
apply (induct_tac n, auto)
nipkow
parents: 26176
diff changeset
  1726
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow
parents: 26176
diff changeset
  1727
apply (rename_tac "J'", simp)
nipkow
parents: 26176
diff changeset
  1728
apply (erule notE, auto)
nipkow
parents: 26176
diff changeset
  1729
apply (case_tac "J' = {#}")
nipkow
parents: 26176
diff changeset
  1730
 apply (simp add: mult_def)
nipkow
parents: 26176
diff changeset
  1731
 apply (rule r_into_trancl)
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1732
 apply (simp add: mult1_def set_mset_def, blast)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1733
txt \<open>Now we know @{term "J' \<noteq> {#}"}.\<close>
26178
nipkow
parents: 26176
diff changeset
  1734
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
  1735
apply (erule_tac P = "\<forall>k \<in> set_mset K. P k" for P in rev_mp)
26178
nipkow
parents: 26176
diff changeset
  1736
apply (erule ssubst)
nipkow
parents: 26176
diff changeset
  1737
apply (simp add: Ball_def, auto)
nipkow
parents: 26176
diff changeset
  1738
apply (subgoal_tac
nipkow
parents: 26176
diff changeset
  1739
  "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
nipkow
parents: 26176
diff changeset
  1740
    (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow
parents: 26176
diff changeset
  1741
 prefer 2
nipkow
parents: 26176
diff changeset
  1742
 apply force
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1743
apply (simp (no_asm_use) add: add.assoc [symmetric] mult_def)
26178
nipkow
parents: 26176
diff changeset
  1744
apply (erule trancl_trans)
nipkow
parents: 26176
diff changeset
  1745
apply (rule r_into_trancl)
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1746
apply (simp add: mult1_def set_mset_def)
26178
nipkow
parents: 26176
diff changeset
  1747
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  1748
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
  1749
apply (simp add: ac_simps)
26178
nipkow
parents: 26176
diff changeset
  1750
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1751
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1752
lemma one_step_implies_mult:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1753
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1754
    ==> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  1755
using one_step_implies_mult_aux by blast
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1756
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1757
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1758
subsubsection \<open>Partial-order properties\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1759
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1760
definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "#<#" 50) where
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1761
  "M' #<# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1762
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1763
definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "#<=#" 50) where
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1764
  "M' #<=# M \<longleftrightarrow> M' #<# M \<or> M' = M"
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1765
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1766
notation (xsymbols) less_multiset (infix "#\<subset>#" 50)
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1767
notation (xsymbols) le_multiset (infix "#\<subseteq>#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1768
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1769
interpretation multiset_order: order le_multiset less_multiset
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1770
proof -
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1771
  have irrefl: "\<And>M :: 'a multiset. \<not> M #\<subset># M"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1772
  proof
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1773
    fix M :: "'a multiset"
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1774
    assume "M #\<subset># M"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1775
    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
  1776
    have "trans {(x'::'a, x). x' < x}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1777
      by (rule transI) simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1778
    moreover note MM
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1779
    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
  1780
      \<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
  1781
      by (rule mult_implies_one_step)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1782
    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
  1783
      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
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1784
    then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1785
    have "finite (set_mset K)" by simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1786
    moreover note aux2
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1787
    ultimately have "set_mset K = {}"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1788
      by (induct rule: finite_induct) (auto intro: order_less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1789
    with aux1 show False by simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1790
  qed
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1791
  have trans: "\<And>K M N :: 'a multiset. K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># N"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1792
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1793
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1794
    by default (auto simp add: le_multiset_def irrefl dest: trans)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1795
qed
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1796
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1797
lemma mult_less_irrefl [elim!]: "M #\<subset># (M::'a::order multiset) ==> R"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1798
  by simp
26567
7bcebb8c2d33 instantiation replacing primitive instance plus overloaded defs; more conservative type arities
haftmann
parents: 26178
diff changeset
  1799
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1800
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1801
subsubsection \<open>Monotonicity of multiset union\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1802
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1803
lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1804
apply (unfold mult1_def)
nipkow
parents: 26176
diff changeset
  1805
apply auto
nipkow
parents: 26176
diff changeset
  1806
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  1807
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
  1808
apply (simp add: add.assoc)
26178
nipkow
parents: 26176
diff changeset
  1809
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1810
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1811
lemma union_less_mono2: "B #\<subset># D ==> C + B #\<subset># C + (D::'a::order multiset)"
26178
nipkow
parents: 26176
diff changeset
  1812
apply (unfold less_multiset_def mult_def)
nipkow
parents: 26176
diff changeset
  1813
apply (erule trancl_induct)
40249
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  1814
 apply (blast intro: mult1_union)
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  1815
apply (blast intro: mult1_union trancl_trans)
26178
nipkow
parents: 26176
diff changeset
  1816
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1817
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1818
lemma union_less_mono1: "B #\<subset># D ==> 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
  1819
apply (subst add.commute [of B C])
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  1820
apply (subst add.commute [of D C])
26178
nipkow
parents: 26176
diff changeset
  1821
apply (erule union_less_mono2)
nipkow
parents: 26176
diff changeset
  1822
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1823
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1824
lemma union_less_mono:
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  1825
  "A #\<subset># C ==> B #\<subset># D ==> A + B #\<subset># C + (D::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1826
  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
  1827
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1828
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1829
proof
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1830
qed (auto simp add: le_multiset_def intro: union_less_mono2)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1831
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
  1832
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1833
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
  1834
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1835
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
  1836
  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
  1837
  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
  1838
  by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1839
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1840
definition "ms_strict = mult pair_less"
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1841
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
  1842
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1843
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
  1844
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
  1845
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
  1846
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1847
lemma smsI:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1848
  "(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
  1849
  unfolding ms_strict_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1850
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
  1851
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1852
lemma wmsI:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1853
  "(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
  1854
  \<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
  1855
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
  1856
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
  1857
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1858
inductive pw_leq
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1859
where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1860
  pw_leq_empty: "pw_leq {#} {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1861
| 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
  1862
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1863
lemma pw_leq_lstep:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1864
  "(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
  1865
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
  1866
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1867
lemma pw_leq_split:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1868
  assumes "pw_leq X Y"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1869
  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
  1870
  using assms
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1871
proof (induct)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1872
  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
  1873
next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1874
  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
  1875
  then obtain A B Z where
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1876
    [simp]: "X = A + Z" "Y = B + Z"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1877
      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
  1878
    by auto
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1879
  from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1880
    unfolding pair_leq_def by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1881
  thus ?case
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1882
  proof
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1883
    assume [simp]: "x = y"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1884
    have
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1885
      "{#x#} + X = A + ({#y#}+Z)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1886
      \<and> {#y#} + Y = B + ({#y#}+Z)
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1887
      \<and> ((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
  1888
      by (auto simp: ac_simps)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1889
    thus ?case by (intro exI)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1890
  next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1891
    assume A: "(x, y) \<in> pair_less"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1892
    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
  1893
    have "{#x#} + X = ?A' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1894
      "{#y#} + Y = ?B' + Z"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1895
      by (auto simp add: ac_simps)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1896
    moreover have
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1897
      "(set_mset ?A', set_mset ?B') \<in> max_strict"
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1898
      using 1 A unfolding max_strict_def
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1899
      by (auto elim!: max_ext.cases)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1900
    ultimately show ?thesis by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1901
  qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1902
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1903
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1904
lemma
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1905
  assumes pwleq: "pw_leq Z Z'"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1906
  shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1907
  and   ms_weakI1:  "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1908
  and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1909
proof -
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1910
  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
  1911
  obtain A' B' Z''
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1912
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1913
    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
  1914
    by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1915
  {
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1916
    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
  1917
    from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1918
    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
  1919
    proof
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1920
      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
  1921
      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
  1922
        by (auto simp: max_strict_def intro: max_ext_additive)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1923
      thus ?thesis by (rule smsI)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1924
    next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1925
      assume [simp]: "A' = {#} \<and> B' = {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1926
      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
  1927
    qed
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  1928
    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
  1929
    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
  1930
  }
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1931
  from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1932
  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
  1933
  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
  1934
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1935
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  1936
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
  1937
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1938
and nonempty_single: "{# x #} \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1939
by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1940
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1941
setup \<open>
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1942
let
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1943
  fun msetT T = Type (@{type_name multiset}, [T]);
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1944
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1945
  fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1946
    | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1947
    | mk_mset T (x :: xs) =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1948
          Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1949
                mk_mset T [x] $ mk_mset T xs
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1950
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1951
  fun mset_member_tac m i =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1952
      (if m <= 0 then
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1953
           rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1954
       else
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1955
           rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1956
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1957
  val mset_nonempty_tac =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1958
      rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1959
59625
aacdce52b2fc proper context;
wenzelm
parents: 59557
diff changeset
  1960
  fun regroup_munion_conv ctxt =
aacdce52b2fc proper context;
wenzelm
parents: 59557
diff changeset
  1961
    Function_Lib.regroup_conv ctxt @{const_abbrev Mempty} @{const_name plus}
aacdce52b2fc proper context;
wenzelm
parents: 59557
diff changeset
  1962
      (map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1963
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1964
  fun unfold_pwleq_tac i =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1965
    (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1966
      ORELSE (rtac @{thm pw_leq_lstep} i)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1967
      ORELSE (rtac @{thm pw_leq_empty} i)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1968
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1969
  val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union},
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1970
                      @{thm Un_insert_left}, @{thm Un_empty_left}]
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1971
in
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1972
  ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1973
  {
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1974
    msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1975
    mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1976
    mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps,
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30428
diff changeset
  1977
    smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30428
diff changeset
  1978
    reduction_pair= @{thm ms_reduction_pair}
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1979
  })
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1980
end
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1981
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1982
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1983
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1984
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
  1985
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1986
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
  1987
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
  1988
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
  1989
  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
  1990
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
  1991
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
  1992
  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
  1993
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
  1994
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
  1995
  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
  1996
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
  1997
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
  1998
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
  1999
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
  2000
  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
  2001
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
  2002
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
  2003
  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
  2004
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
  2005
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
  2006
  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
  2007
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2008
lemma mset_less_trans: "(M::'a multiset) <# K \<Longrightarrow> K <# N \<Longrightarrow> M <# N"
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2009
  by (fact subset_mset.less_trans)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2010
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2011
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
  2012
  by (fact subset_mset.inf.commute)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2013
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2014
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
  2015
  by (fact subset_mset.inf.assoc [symmetric])
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2016
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2017
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
  2018
  by (fact subset_mset.inf.left_commute)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2019
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2020
lemmas multiset_inter_ac =
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2021
  multiset_inter_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2022
  multiset_inter_assoc
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2023
  multiset_inter_left_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2024
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2025
lemma mult_less_not_refl:
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  2026
  "\<not> M #\<subset># (M::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2027
  by (fact multiset_order.less_irrefl)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2028
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2029
lemma mult_less_trans:
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  2030
  "K #\<subset># M ==> M #\<subset># N ==> K #\<subset># (N::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2031
  by (fact multiset_order.less_trans)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2032
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2033
lemma mult_less_not_sym:
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  2034
  "M #\<subset># N ==> \<not> N #\<subset># (M::'a::order multiset)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2035
  by (fact multiset_order.less_not_sym)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2036
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2037
lemma mult_less_asym:
59958
4538d41e8e54 renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents: 59949
diff changeset
  2038
  "M #\<subset># N ==> (\<not> P ==> N #\<subset># (M::'a::order multiset)) ==> P"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2039
  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
  2040
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2041
ML \<open>
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2042
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2043
                      (Const _ $ t') =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2044
    let
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2045
      val (maybe_opt, ps) =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2046
        Nitpick_Model.dest_plain_fun t' ||> op ~~
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2047
        ||> map (apsnd (snd o HOLogic.dest_number))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2048
      fun elems_for t =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2049
        case AList.lookup (op =) ps t of
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2050
          SOME n => replicate n t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2051
        | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2052
    in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2053
      case maps elems_for (all_values elem_T) @
37261
8a89fd40ed0b honor xsymbols in Nitpick
blanchet
parents: 37169
diff changeset
  2054
           (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
8a89fd40ed0b honor xsymbols in Nitpick
blanchet
parents: 37169
diff changeset
  2055
            else []) of
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2056
        [] => Const (@{const_name zero_class.zero}, T)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2057
      | ts => foldl1 (fn (t1, t2) =>
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2058
                         Const (@{const_name plus_class.plus}, T --> T --> T)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2059
                         $ t1 $ t2)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2060
                     (map (curry (op $) (Const (@{const_name single},
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2061
                                                elem_T --> T))) ts)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2062
    end
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2063
  | multiset_postproc _ _ _ _ t = t
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2064
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2065
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2066
declaration \<open>
38287
796302ca3611 replace "setup" with "declaration"
blanchet
parents: 38242
diff changeset
  2067
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
38242
f26d590dce0f adapt occurrences of renamed Nitpick functions
blanchet
parents: 37765
diff changeset
  2068
    multiset_postproc
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2069
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2070
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2071
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2072
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
  2073
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2074
code_datatype mset
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2075
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2076
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2077
  "{#} = mset []"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2078
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2079
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2080
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2081
  "{#x#} = mset [x]"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2082
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2083
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2084
lemma union_code [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2085
  "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
  2086
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2087
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2088
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2089
  "image_mset f (mset xs) = mset (map f xs)"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2090
  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
  2091
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2092
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2093
  "filter_mset f (mset xs) = mset (filter f xs)"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2094
  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
  2095
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2096
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2097
  "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
  2098
  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
  2099
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2100
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2101
  "mset xs #\<inter> mset ys =
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2102
    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
  2103
      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
  2104
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2105
  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
  2106
    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
  2107
      (mset xs #\<inter> mset ys) + mset zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2108
    by (induct xs arbitrary: ys)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2109
      (auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2110
  then show ?thesis by simp
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2111
qed
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2112
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2113
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2114
  "mset xs #\<union> mset ys =
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2115
    mset (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2116
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2117
  have "\<And>zs. mset (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2118
      (mset xs #\<union> mset ys) + mset zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2119
    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
  2120
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2121
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2122
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2123
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
  2124
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2125
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2126
  "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
  2127
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2128
  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
  2129
    by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2130
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2131
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2132
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2133
declare set_mset_mset [code]
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2134
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2135
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
  2136
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2137
lemma [code]: -- \<open>not very efficient, but representation-ignorant!\<close>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2138
  "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
  2139
  apply (cases "finite A")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2140
  apply simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2141
  apply (induct A rule: finite_induct)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2142
  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
  2143
  done
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2144
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2145
declare size_mset [code]
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2146
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2147
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
  2148
  "ms_lesseq_impl [] ys = Some (ys \<noteq> [])"
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2149
| "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
  2150
     None \<Rightarrow> None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2151
   | 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
  2152
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2153
lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None \<longleftrightarrow> \<not> mset xs \<le># mset ys) \<and>
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2154
  (ms_lesseq_impl xs ys = Some True \<longleftrightarrow> mset xs <# mset ys) \<and>
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2155
  (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
  2156
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
  2157
  case (Nil ys)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2158
  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
  2159
next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2160
  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
  2161
  show ?case
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2162
  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
  2163
    case None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2164
    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
  2165
    {
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2166
      assume "mset (x # xs) \<le># mset ys"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2167
      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
  2168
    } note nle = this
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2169
    moreover
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2170
    {
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2171
      assume "mset (x # xs) <# mset ys"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2172
      hence "mset (x # xs) \<le># 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
  2173
      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
  2174
    }
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2175
    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
  2176
  next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2177
    case (Some res)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2178
    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
  2179
    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
  2180
    from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2181
    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
  2182
      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
  2183
    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
  2184
      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
  2185
      unfolding id
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2186
      using Cons[of "ys1 @ ys2"]
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2187
      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
  2188
  qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2189
qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2190
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2191
lemma [code]: "mset xs \<le># 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
  2192
  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
  2193
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2194
lemma [code]: "mset xs <# 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
  2195
  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
  2196
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2197
instantiation multiset :: (equal) equal
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2198
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2199
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2200
definition
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2201
  [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2202
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
  2203
  unfolding equal_multiset_def
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2204
  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
  2205
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2206
instance
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2207
  by default (simp add: equal_multiset_def)
37169
f69efa106feb make Nitpick "show_all" option behave less surprisingly
blanchet
parents: 37107
diff changeset
  2208
end
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
  2209
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2210
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2211
  "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
  2212
  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
  2213
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2214
lemma [code]:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2215
  "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
  2216
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2217
  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
  2218
    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
  2219
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2220
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2221
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2222
text \<open>
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2223
  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
  2224
  and @{const less_multiset} (multiset order).
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2225
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2226
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2227
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
  2228
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2229
definition (in term_syntax)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2230
  msetify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2231
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2232
  [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
  2233
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2234
notation fcomp (infixl "\<circ>>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2235
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
  2236
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2237
instantiation multiset :: (random) random
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2238
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2239
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2240
definition
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2241
  "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
  2242
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2243
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2244
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2245
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2246
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2247
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
  2248
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
  2249
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2250
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
  2251
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2252
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2253
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
  2254
where
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2255
  "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
  2256
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2257
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2258
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2259
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2260
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2261
hide_const (open) msetify
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2262
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2263
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2264
subsection \<open>BNF setup\<close>
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2265
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2266
definition rel_mset where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2267
  "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
  2268
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2269
lemma mset_zip_take_Cons_drop_twice:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2270
  assumes "length xs = length ys" "j \<le> length xs"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2271
  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
  2272
    mset (zip xs ys) + {#(x, y)#}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2273
using assms
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2274
proof (induct xs ys arbitrary: x y j rule: list_induct2)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2275
  case Nil
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2276
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2277
    by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2278
next
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2279
  case (Cons x xs y ys)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2280
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2281
  proof (cases "j = 0")
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2282
    case True
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2283
    thus ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2284
      by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2285
  next
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2286
    case False
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2287
    then obtain k where k: "j = Suc k"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2288
      by (case_tac j) simp
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2289
    hence "k \<le> length xs"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2290
      using Cons.prems by auto
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2291
    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
  2292
      mset (zip xs ys) + {#(x, y)#}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2293
      by (rule Cons.hyps(2))
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2294
    thus ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2295
      unfolding k by (auto simp: add.commute union_lcomm)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2296
  qed
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2297
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2298
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2299
lemma ex_mset_zip_left:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2300
  assumes "length xs = length ys" "mset xs' = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2301
  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
  2302
using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2303
proof (induct xs ys arbitrary: xs' rule: list_induct2)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2304
  case Nil
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2305
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2306
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2307
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2308
  case (Cons x xs y ys xs')
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2309
  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
  2310
    by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2311
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2312
  def xsa \<equiv> "take j xs' @ drop (Suc j) xs'"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2313
  have "mset xs' = {#x#} + mset xsa"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2314
    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
  2315
    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
  2316
      mset.simps(2) union_code add.commute)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2317
  hence ms_x: "mset xsa = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2318
    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
  2319
  then obtain ysa where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2320
    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
  2321
    using Cons.hyps(2) by blast
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2322
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2323
  def ys' \<equiv> "take j ysa @ y # drop j ysa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2324
  have xs': "xs' = take j xsa @ x # drop j xsa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2325
    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
  2326
    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
  2327
      length_drop size_mset)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2328
  have j_len': "j \<le> length xsa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2329
    using j_len xs' xsa_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2330
    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
  2331
  have "length ys' = length xs'"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2332
    unfolding ys'_def using Cons.prems len_a ms_x
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2333
    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
  2334
  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
  2335
    unfolding xs' ys'_def
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2336
    by (rule trans[OF mset_zip_take_Cons_drop_twice])
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2337
      (auto simp: len_a ms_a j_len' add.commute)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2338
  ultimately show ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2339
    by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2340
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2341
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2342
lemma list_all2_reorder_left_invariance:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2343
  assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2344
  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
  2345
proof -
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2346
  have len: "length xs = length ys"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2347
    using rel list_all2_conv_all_nth by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2348
  obtain ys' where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2349
    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
  2350
    using len ms_x by (metis ex_mset_zip_left)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2351
  have "list_all2 R xs' ys'"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2352
    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
  2353
  moreover have "mset ys' = mset ys"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2354
    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
  2355
  ultimately show ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2356
    by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2357
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2358
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2359
lemma ex_mset: "\<exists>xs. mset xs = X"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2360
  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
  2361
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2362
bnf "'a multiset"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2363
  map: image_mset
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2364
  sets: set_mset
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2365
  bd: natLeq
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2366
  wits: "{#}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2367
  rel: rel_mset
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2368
proof -
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2369
  show "image_mset id = id"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2370
    by (rule image_mset.id)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2371
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2372
  show "\<And>f g. image_mset (g \<circ> f) = image_mset g \<circ> image_mset f"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2373
    unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2374
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2375
  fix X :: "'a multiset"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2376
  show "\<And>f g. (\<And>z. z \<in> set_mset X \<Longrightarrow> f z = g z) \<Longrightarrow> image_mset f X = image_mset g X"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2377
    by (induct X, (simp (no_asm))+,
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2378
      metis One_nat_def Un_iff count_single mem_set_mset_iff set_mset_union zero_less_Suc)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2379
next
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2380
  show "\<And>f. set_mset \<circ> image_mset f = op ` f \<circ> set_mset"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2381
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2382
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2383
  show "card_order natLeq"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2384
    by (rule natLeq_card_order)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2385
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2386
  show "BNF_Cardinal_Arithmetic.cinfinite natLeq"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2387
    by (rule natLeq_cinfinite)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2388
next
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2389
  show "\<And>X. ordLeq3 (card_of (set_mset X)) natLeq"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2390
    by transfer
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2391
      (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2392
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2393
  show "\<And>R S. rel_mset R OO rel_mset S \<le> rel_mset (R OO S)"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2394
    unfolding rel_mset_def[abs_def] OO_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2395
    apply clarify
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2396
    apply (rename_tac X Z Y xs ys' ys zs)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2397
    apply (drule_tac xs = ys' and ys = zs and xs' = ys in list_all2_reorder_left_invariance)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2398
    by (auto intro: list_all2_trans)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2399
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2400
  show "\<And>R. rel_mset R =
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2401
    (BNF_Def.Grp {x. set_mset x \<subseteq> {(x, y). R x y}} (image_mset fst))\<inverse>\<inverse> OO
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2402
    BNF_Def.Grp {x. set_mset x \<subseteq> {(x, y). R x y}} (image_mset snd)"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2403
    unfolding rel_mset_def[abs_def] BNF_Def.Grp_def OO_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2404
    apply (rule ext)+
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2405
    apply auto
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2406
     apply (rule_tac x = "mset (zip xs ys)" in exI; auto)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2407
        apply (metis list_all2_lengthD map_fst_zip mset_map)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2408
       apply (auto simp: list_all2_iff)[1]
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2409
      apply (metis list_all2_lengthD map_snd_zip mset_map)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2410
     apply (auto simp: list_all2_iff)[1]
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2411
    apply (rename_tac XY)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2412
    apply (cut_tac X = XY in ex_mset)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2413
    apply (erule exE)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2414
    apply (rename_tac xys)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2415
    apply (rule_tac x = "map fst xys" in exI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2416
    apply (auto simp: mset_map)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2417
    apply (rule_tac x = "map snd xys" in exI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2418
    apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd)
59997
90fb391a15c1 tuned proofs;
wenzelm
parents: 59986
diff changeset
  2419
    done
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2420
next
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2421
  show "\<And>z. z \<in> set_mset {#} \<Longrightarrow> False"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2422
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2423
qed
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2424
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2425
inductive rel_mset' where
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2426
  Zero[intro]: "rel_mset' R {#} {#}"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2427
| 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
  2428
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2429
lemma rel_mset_Zero: "rel_mset R {#} {#}"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2430
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
  2431
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2432
declare multiset.count[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2433
declare Abs_multiset_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2434
declare multiset.count_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2435
declare union_preserves_multiset[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2436
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2437
lemma rel_mset_Plus:
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2438
assumes ab: "R a b" and MN: "rel_mset R M N"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2439
shows "rel_mset R (M + {#a#}) (N + {#b#})"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2440
proof-
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2441
  {fix y assume "R a b" and "set_mset y \<subseteq> {(x, y). R x y}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2442
   hence "\<exists>ya. image_mset fst y + {#a#} = image_mset fst ya \<and>
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2443
               image_mset snd y + {#b#} = image_mset snd ya \<and>
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2444
               set_mset ya \<subseteq> {(x, y). R x y}"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2445
   apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2446
  }
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2447
  thus ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2448
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2449
  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
  2450
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2451
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2452
lemma rel_mset'_imp_rel_mset:
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2453
  "rel_mset' R M N \<Longrightarrow> rel_mset R M N"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2454
apply(induct rule: rel_mset'.induct)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2455
using rel_mset_Zero rel_mset_Plus by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2456
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2457
lemma rel_mset_size:
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2458
  "rel_mset R M N \<Longrightarrow> size M = size N"
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2459
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
  2460
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2461
lemma multiset_induct2[case_names empty addL addR]:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2462
assumes empty: "P {#} {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2463
and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2464
and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2465
shows "P M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2466
apply(induct N rule: multiset_induct)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2467
  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
  2468
  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
  2469
done
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2470
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2471
lemma multiset_induct2_size[consumes 1, case_names empty add]:
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2472
assumes c: "size M = size N"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2473
and empty: "P {#} {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2474
and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2475
shows "P M N"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2476
using c 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
  2477
  case (less M)  show ?case
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2478
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2479
    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
  2480
    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
  2481
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2482
    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
  2483
    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
  2484
    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
  2485
    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
  2486
    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
  2487
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2488
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2489
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2490
lemma msed_map_invL:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2491
assumes "image_mset f (M + {#a#}) = N"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2492
shows "\<exists>N1. N = N1 + {#f a#} \<and> image_mset f M = N1"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2493
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2494
  have "f a \<in># N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2495
  using assms multiset.set_map[of f "M + {#a#}"] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2496
  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
  2497
  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
  2498
  thus ?thesis using N by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2499
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2500
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2501
lemma msed_map_invR:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2502
assumes "image_mset f M = N + {#b#}"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2503
shows "\<exists>M1 a. M = M1 + {#a#} \<and> f a = b \<and> image_mset f M1 = N"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2504
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2505
  obtain a where a: "a \<in># M" and fa: "f a = b"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2506
  using multiset.set_map[of f M] unfolding assms
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2507
  by (metis image_iff mem_set_mset_iff union_single_eq_member)
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2508
  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
  2509
  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
  2510
  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
  2511
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2512
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2513
lemma msed_rel_invL:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2514
assumes "rel_mset R (M + {#a#}) N"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2515
shows "\<exists>N1 b. N = N1 + {#b#} \<and> R a b \<and> rel_mset R M N1"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2516
proof-
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2517
  obtain K where KM: "image_mset fst K = M + {#a#}"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2518
  and KN: "image_mset snd K = N" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2519
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2520
  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
  2521
  obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2522
  and K1M: "image_mset fst K1 = M" using msed_map_invR[OF KM] by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2523
  obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "image_mset snd K1 = N1"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2524
  using msed_map_invL[OF KN[unfolded K]] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2525
  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
  2526
  have "rel_mset R M N1" using sK K1M K1N1
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2527
  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
  2528
  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
  2529
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2530
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2531
lemma msed_rel_invR:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2532
assumes "rel_mset R M (N + {#b#})"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2533
shows "\<exists>M1 a. M = M1 + {#a#} \<and> R a b \<and> rel_mset R M1 N"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2534
proof-
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2535
  obtain K where KN: "image_mset snd K = N + {#b#}"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2536
  and KM: "image_mset fst K = M" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2537
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2538
  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
  2539
  obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2540
  and K1N: "image_mset snd K1 = N" using msed_map_invR[OF KN] by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2541
  obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "image_mset fst K1 = M1"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2542
  using msed_map_invL[OF KM[unfolded K]] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2543
  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
  2544
  have "rel_mset R M1 N" using sK K1N K1M1
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2545
  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
  2546
  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
  2547
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2548
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2549
lemma rel_mset_imp_rel_mset':
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2550
assumes "rel_mset R M N"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2551
shows "rel_mset' R M N"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2552
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
  2553
  case (less M)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2554
  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
  2555
  show ?case
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2556
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2557
    case True hence "N = {#}" using c by simp
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2558
    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
  2559
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2560
    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
  2561
    obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_mset R M1 N1"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2562
    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
  2563
    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
  2564
    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
  2565
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2566
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2567
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2568
lemma rel_mset_rel_mset':
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2569
"rel_mset R M N = rel_mset' R M N"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2570
using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2571
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2572
(* The main end product for rel_mset: inductive characterization *)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2573
theorems rel_mset_induct[case_names empty add, induct pred: rel_mset] =
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2574
         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
  2575
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2576
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2577
subsection \<open>Size setup\<close>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2578
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2579
lemma multiset_size_o_map: "size_multiset g \<circ> image_mset f = size_multiset (g \<circ> f)"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  2580
  unfolding o_apply by (rule ext) (induct_tac, auto)
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2581
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2582
setup \<open>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2583
BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset}
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2584
  @{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2585
    size_union}
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2586
  @{thms multiset_size_o_map}
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2587
\<close>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2588
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2589
hide_const (open) wcount
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  2590
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2591
end