src/HOL/Library/Multiset.thy
author wenzelm
Mon, 03 Oct 2016 21:36:10 +0200
changeset 64027 4a33d740c9dc
parent 63924 f91766530e13
child 64017 6e7bf7678518
permissions -rw-r--r--
proper log output;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Library/Multiset.thy
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
     2
    Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
     3
    Author:     Andrei Popescu, TU Muenchen
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
     4
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
     5
    Author:     Dmitriy Traytel, TU Muenchen
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
     6
    Author:     Mathias Fleury, MPII
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     7
*)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     8
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
     9
section \<open>(Finite) multisets\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    10
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
    11
theory Multiset
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents: 51548
diff changeset
    12
imports Main
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
    13
begin
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    14
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    15
subsection \<open>The type of multisets\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    16
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    17
definition "multiset = {f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    18
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    19
typedef 'a multiset = "multiset :: ('a \<Rightarrow> nat) set"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    20
  morphisms count Abs_multiset
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    21
  unfolding multiset_def
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    22
proof
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    23
  show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    24
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    25
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    26
setup_lifting type_definition_multiset
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
    27
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    28
lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    29
  by (simp only: count_inject [symmetric] fun_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    30
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    31
lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    32
  using multiset_eq_iff by auto
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    33
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    34
text \<open>Preservation of the representing set @{term multiset}.\<close>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    35
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    36
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    37
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    38
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    39
lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    40
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    41
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    42
lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    43
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    44
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    45
lemma diff_preserves_multiset:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    46
  assumes "M \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    47
  shows "(\<lambda>a. M a - N a) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    48
proof -
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    49
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    50
    by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    51
  with assms show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    52
    by (auto simp add: multiset_def intro: finite_subset)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    53
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    54
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    55
lemma filter_preserves_multiset:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    56
  assumes "M \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    57
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    58
proof -
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    59
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    60
    by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    61
  with assms show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    62
    by (auto simp add: multiset_def intro: finite_subset)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    63
qed
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    64
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    65
lemmas in_multiset = const0_in_multiset only1_in_multiset
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    66
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    67
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    68
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    69
subsection \<open>Representing multisets\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    70
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
    71
text \<open>Multiset enumeration\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    72
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    73
instantiation multiset :: (type) cancel_comm_monoid_add
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    74
begin
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    75
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    76
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    77
by (rule const0_in_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    78
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    79
abbreviation Mempty :: "'a multiset" ("{#}") where
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    80
  "Mempty \<equiv> 0"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    81
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    82
lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    83
by (rule union_preserves_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    84
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
    85
lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
    86
by (rule diff_preserves_multiset)
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59813
diff changeset
    87
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    88
instance
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
    89
  by (standard; transfer; simp add: fun_eq_iff)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    90
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    91
end
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    92
63195
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    93
context
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    94
begin
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    95
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    96
qualified definition is_empty :: "'a multiset \<Rightarrow> bool" where
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    97
  [code_abbrev]: "is_empty A \<longleftrightarrow> A = {#}"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    98
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
    99
end
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
   100
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   101
lemma add_mset_in_multiset:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   102
  assumes M: \<open>M \<in> multiset\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   103
  shows \<open>(\<lambda>b. if b = a then Suc (M b) else M b) \<in> multiset\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   104
  using assms by (simp add: multiset_def insert_Collect[symmetric])
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   105
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   106
lift_definition add_mset :: "'a \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   107
  "\<lambda>a M b. if b = a then Suc (M b) else M b"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   108
by (rule add_mset_in_multiset)
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   109
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   110
syntax
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   111
  "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
25507
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   112
translations
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   113
  "{#x, xs#}" == "CONST add_mset x {#xs#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   114
  "{#x#}" == "CONST add_mset x {#}"
25507
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   115
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
   116
lemma count_empty [simp]: "count {#} a = 0"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   117
  by (simp add: zero_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   118
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   119
lemma count_add_mset [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   120
  "count (add_mset b A) a = (if b = a then Suc (count A a) else count A a)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   121
  by (simp add: add_mset.rep_eq)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   122
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   123
lemma count_single: "count {#b#} a = (if b = a then 1 else 0)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   124
  by simp
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   125
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   126
lemma
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   127
  add_mset_not_empty [simp]: \<open>add_mset a A \<noteq> {#}\<close> and
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   128
  empty_not_add_mset [simp]: "{#} \<noteq> add_mset a A"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   129
  by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   130
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   131
lemma add_mset_add_mset_same_iff [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   132
  "add_mset a A = add_mset a B \<longleftrightarrow> A = B"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   133
  by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   134
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   135
lemma add_mset_commute:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   136
  "add_mset x (add_mset y M) = add_mset y (add_mset x M)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   137
  by (auto simp: multiset_eq_iff)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29509
diff changeset
   138
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   139
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   140
subsection \<open>Basic operations\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   141
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   142
subsubsection \<open>Conversion to set and membership\<close>
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   143
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   144
definition set_mset :: "'a multiset \<Rightarrow> 'a set"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   145
  where "set_mset M = {x. count M x > 0}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   146
62537
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   147
abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   148
  where "Melem a M \<equiv> a \<in> set_mset M"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   149
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   150
notation
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   151
  Melem  ("op \<in>#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   152
  Melem  ("(_/ \<in># _)" [51, 51] 50)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   153
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   154
notation  (ASCII)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   155
  Melem  ("op :#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   156
  Melem  ("(_/ :# _)" [51, 51] 50)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   157
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   158
abbreviation not_Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   159
  where "not_Melem a M \<equiv> a \<notin> set_mset M"
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   160
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   161
notation
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   162
  not_Melem  ("op \<notin>#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   163
  not_Melem  ("(_/ \<notin># _)" [51, 51] 50)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   164
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   165
notation  (ASCII)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   166
  not_Melem  ("op ~:#") and
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   167
  not_Melem  ("(_/ ~:# _)" [51, 51] 50)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   168
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   169
context
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   170
begin
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   171
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   172
qualified abbreviation Ball :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   173
  where "Ball M \<equiv> Set.Ball (set_mset M)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   174
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   175
qualified abbreviation Bex :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   176
  where "Bex M \<equiv> Set.Bex (set_mset M)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   177
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   178
end
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   179
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   180
syntax
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   181
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>#_./ _)" [0, 0, 10] 10)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   182
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>#_./ _)" [0, 0, 10] 10)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   183
62537
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   184
syntax  (ASCII)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   185
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_:#_./ _)" [0, 0, 10] 10)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   186
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_:#_./ _)" [0, 0, 10] 10)
7a9aa69f9b38 syntax for multiset membership modelled after syntax for set membership
haftmann
parents: 62430
diff changeset
   187
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   188
translations
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   189
  "\<forall>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Ball A (\<lambda>x. P)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   190
  "\<exists>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Bex A (\<lambda>x. P)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   191
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   192
lemma count_eq_zero_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   193
  "count M x = 0 \<longleftrightarrow> x \<notin># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   194
  by (auto simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   195
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   196
lemma not_in_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   197
  "x \<notin># M \<longleftrightarrow> count M x = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   198
  by (auto simp add: count_eq_zero_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   199
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   200
lemma count_greater_zero_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   201
  "count M x > 0 \<longleftrightarrow> x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   202
  by (auto simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   203
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   204
lemma count_inI:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   205
  assumes "count M x = 0 \<Longrightarrow> False"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   206
  shows "x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   207
proof (rule ccontr)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   208
  assume "x \<notin># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   209
  with assms show False by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   210
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   211
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   212
lemma in_countE:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   213
  assumes "x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   214
  obtains n where "count M x = Suc n"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   215
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   216
  from assms have "count M x > 0" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   217
  then obtain n where "count M x = Suc n"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   218
    using gr0_conv_Suc by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   219
  with that show thesis .
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   220
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   221
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   222
lemma count_greater_eq_Suc_zero_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   223
  "count M x \<ge> Suc 0 \<longleftrightarrow> x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   224
  by (simp add: Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   225
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   226
lemma count_greater_eq_one_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   227
  "count M x \<ge> 1 \<longleftrightarrow> x \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   228
  by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   229
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   230
lemma set_mset_empty [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   231
  "set_mset {#} = {}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   232
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   233
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   234
lemma set_mset_single:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   235
  "set_mset {#b#} = {b}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   236
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   237
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   238
lemma set_mset_eq_empty_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   239
  "set_mset M = {} \<longleftrightarrow> M = {#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   240
  by (auto simp add: multiset_eq_iff count_eq_zero_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   241
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   242
lemma finite_set_mset [iff]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   243
  "finite (set_mset M)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   244
  using count [of M] by (simp add: multiset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   245
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   246
lemma set_mset_add_mset_insert [simp]: \<open>set_mset (add_mset a A) = insert a (set_mset A)\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   247
  by (auto simp del: count_greater_eq_Suc_zero_iff
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   248
      simp: count_greater_eq_Suc_zero_iff[symmetric] split: if_splits)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   249
63924
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   250
lemma multiset_nonemptyE [elim]:
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   251
  assumes "A \<noteq> {#}"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   252
  obtains x where "x \<in># A"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   253
proof -
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   254
  have "\<exists>x. x \<in># A" by (rule ccontr) (insert assms, auto)
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   255
  with that show ?thesis by blast
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   256
qed
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
   257
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   258
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   259
subsubsection \<open>Union\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   260
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   261
lemma count_union [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   262
  "count (M + N) a = count M a + count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   263
  by (simp add: plus_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   264
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   265
lemma set_mset_union [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   266
  "set_mset (M + N) = set_mset M \<union> set_mset N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   267
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_union) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   268
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   269
lemma union_mset_add_mset_left [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   270
  "add_mset a A + B = add_mset a (A + B)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   271
  by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   272
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   273
lemma union_mset_add_mset_right [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   274
  "A + add_mset a B = add_mset a (A + B)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   275
  by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   276
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   277
lemma add_mset_add_single: \<open>add_mset a A = A + {#a#}\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   278
  by (subst union_mset_add_mset_right, subst add.comm_neutral) standard
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   279
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   280
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   281
subsubsection \<open>Difference\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   282
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   283
instance multiset :: (type) comm_monoid_diff
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   284
  by standard (transfer; simp add: fun_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   285
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   286
lemma count_diff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   287
  "count (M - N) a = count M a - count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   288
  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
   289
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   290
lemma add_mset_diff_bothsides:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   291
  \<open>add_mset a M - add_mset a A = M - A\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   292
  by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   293
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   294
lemma in_diff_count:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   295
  "a \<in># M - N \<longleftrightarrow> count N a < count M a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   296
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   297
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   298
lemma count_in_diffI:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   299
  assumes "\<And>n. count N x = n + count M x \<Longrightarrow> False"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   300
  shows "x \<in># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   301
proof (rule ccontr)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   302
  assume "x \<notin># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   303
  then have "count N x = (count N x - count M x) + count M x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   304
    by (simp add: in_diff_count not_less)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   305
  with assms show False by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   306
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   307
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   308
lemma in_diff_countE:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   309
  assumes "x \<in># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   310
  obtains n where "count M x = Suc n + count N x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   311
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   312
  from assms have "count M x - count N x > 0" by (simp add: in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   313
  then have "count M x > count N x" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   314
  then obtain n where "count M x = Suc n + count N x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   315
    using less_iff_Suc_add by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   316
  with that show thesis .
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   317
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   318
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   319
lemma in_diffD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   320
  assumes "a \<in># M - N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   321
  shows "a \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   322
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   323
  have "0 \<le> count N a" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   324
  also from assms have "count N a < count M a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   325
    by (simp add: in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   326
  finally show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   327
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   328
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   329
lemma set_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   330
  "set_mset (M - N) = {a. count N a < count M a}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   331
  by (simp add: set_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   332
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   333
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   334
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   335
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   336
lemma diff_cancel: "A - A = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   337
  by (fact Groups.diff_cancel)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   338
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   339
lemma diff_union_cancelR: "M + N - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   340
  by (fact add_diff_cancel_right')
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   341
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   342
lemma diff_union_cancelL: "N + M - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   343
  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
   344
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   345
lemma diff_right_commute:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   346
  fixes M N Q :: "'a multiset"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   347
  shows "M - N - Q = M - Q - N"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   348
  by (fact diff_right_commute)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   349
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   350
lemma diff_add:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   351
  fixes M N Q :: "'a multiset"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   352
  shows "M - (N + Q) = M - N - Q"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   353
  by (rule sym) (fact diff_diff_add)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   354
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   355
lemma insert_DiffM [simp]: "x \<in># M \<Longrightarrow> add_mset 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
   356
  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
   357
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   358
lemma insert_DiffM2: "x \<in># M \<Longrightarrow> (M - {#x#}) + {#x#} = M"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   359
  by simp
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   360
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   361
lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> add_mset b (M - {#a#}) = add_mset b M - {#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
   362
  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
   363
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   364
lemma diff_add_mset_swap [simp]: "b \<notin># A \<Longrightarrow> add_mset b M - A = add_mset b (M - A)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   365
  by (auto simp add: multiset_eq_iff simp: not_in_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   366
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   367
lemma diff_union_swap2 [simp]: "y \<in># M \<Longrightarrow> add_mset x M - {#y#} = add_mset x (M - {#y#})"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   368
  by (metis add_mset_diff_bothsides diff_union_swap diff_zero insert_DiffM)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   369
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   370
lemma diff_diff_add_mset [simp]: "(M::'a multiset) - N - P = M - (N + P)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   371
  by (rule diff_diff_add)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   372
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   373
lemma diff_union_single_conv:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   374
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   375
  by (simp add: multiset_eq_iff Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   376
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   377
lemma mset_add [elim?]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   378
  assumes "a \<in># A"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   379
  obtains B where "A = add_mset a B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   380
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   381
  from assms have "A = add_mset a (A - {#a#})"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   382
    by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   383
  with that show thesis .
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   384
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   385
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   386
lemma union_iff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   387
  "a \<in># A + B \<longleftrightarrow> a \<in># A \<or> a \<in># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   388
  by auto
26143
314c0bcb7df7 Added useful general lemmas from the work with the HeapMonad
bulwahn
parents: 26033
diff changeset
   389
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   390
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   391
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
   392
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   393
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
   394
  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
   395
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   396
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
   397
  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
   398
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   399
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
   400
  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
   401
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   402
lemma multi_self_add_other_not_self [simp]: "M = add_mset x M \<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
   403
  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
   404
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   405
lemma add_mset_remove_trivial [simp]: \<open>add_mset x M - {#x#} = M\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   406
  by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   407
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   408
lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   409
  by (auto simp add: multiset_eq_iff not_in_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   410
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   411
lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = add_mset x N"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   412
  by auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   413
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   414
lemma union_single_eq_diff: "add_mset x M = N \<Longrightarrow> M = N - {#x#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   415
  unfolding add_mset_add_single[of _ M] by (fact add_implies_diff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   416
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   417
lemma union_single_eq_member: "add_mset x M = N \<Longrightarrow> x \<in># N"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   418
  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
   419
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   420
lemma add_mset_remove_trivial_If:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   421
  "add_mset a (N - {#a#}) = (if a \<in># N then N else add_mset a N)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   422
  by (simp add: diff_single_trivial)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   423
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   424
lemma add_mset_remove_trivial_eq: \<open>N = add_mset a (N - {#a#}) \<longleftrightarrow> a \<in># N\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   425
  by (auto simp: add_mset_remove_trivial_If)
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
   426
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   427
lemma union_is_single:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   428
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N = {#} \<or> M = {#} \<and> N = {#a#}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   429
  (is "?lhs = ?rhs")
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   430
proof
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   431
  show ?lhs if ?rhs using that by auto
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   432
  show ?rhs if ?lhs
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   433
    by (metis Multiset.diff_cancel add.commute add_diff_cancel_left' diff_add_zero diff_single_trivial insert_DiffM that)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   434
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
   435
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   436
lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   437
  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
   438
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   439
lemma add_eq_conv_diff:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   440
  "add_mset a M = add_mset b N \<longleftrightarrow> M = N \<and> a = b \<or> M = add_mset b (N - {#a#}) \<and> N = add_mset a (M - {#b#})"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   441
  (is "?lhs \<longleftrightarrow> ?rhs")
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44339
diff changeset
   442
(* 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
   443
proof
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   444
  show ?lhs if ?rhs
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   445
    using that
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   446
    by (auto simp add: add_mset_commute[of a b])
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   447
  show ?rhs if ?lhs
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   448
  proof (cases "a = b")
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   449
    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
   450
  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
   451
    case False
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   452
    from \<open>?lhs\<close> have "a \<in># add_mset b N" 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
   453
    with False have "a \<in># N" by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   454
    moreover from \<open>?lhs\<close> have "M = add_mset b N - {#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
   455
    moreover note False
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   456
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#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
   457
  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
   458
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
   459
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   460
lemma add_mset_eq_single [iff]: "add_mset b M = {#a#} \<longleftrightarrow> b = a \<and> M = {#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   461
  by (auto simp: add_eq_conv_diff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   462
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   463
lemma single_eq_add_mset [iff]: "{#a#} = add_mset b M \<longleftrightarrow> b = a \<and> M = {#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   464
  by (auto simp: add_eq_conv_diff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   465
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   466
lemma insert_noteq_member:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   467
  assumes BC: "add_mset b B = add_mset c C"
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
   468
   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
   469
  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
   470
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   471
  have "c \<in># add_mset c C" 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
   472
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   473
  then have "c \<in># add_mset b B" using BC 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
   474
  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
   475
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
   476
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   477
lemma add_eq_conv_ex:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   478
  "(add_mset a M = add_mset b N) =
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   479
    (M = N \<and> a = b \<or> (\<exists>K. M = add_mset b K \<and> N = add_mset a K))"
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
   480
  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
   481
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   482
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = add_mset x A"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   483
  by (rule exI [where x = "M - {#x#}"]) simp
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   484
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
   485
lemma multiset_add_sub_el_shuffle:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   486
  assumes "c \<in># B"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   487
    and "b \<noteq> c"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   488
  shows "add_mset b (B - {#c#}) = add_mset b B - {#c#}"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   489
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   490
  from \<open>c \<in># B\<close> obtain A where B: "B = add_mset c A"
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   491
    by (blast dest: multi_member_split)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   492
  have "add_mset b A = add_mset c (add_mset b A) - {#c#}" by simp
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   493
  then have "add_mset b A = add_mset b (add_mset c A) - {#c#}"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
   494
    by (simp add: \<open>b \<noteq> c\<close>)
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   495
  then show ?thesis using B by simp
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   496
qed
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
   497
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   498
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   499
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
   500
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   501
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<subseteq>#" 50)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   502
  where "A \<subseteq># B = (\<forall>a. count A a \<le> count B a)"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   503
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   504
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   505
  where "A \<subset># B = (A \<subseteq># B \<and> A \<noteq> B)"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   506
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   507
abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supseteq>#" 50)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   508
  where "supseteq_mset A B \<equiv> B \<subseteq># A"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   509
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   510
abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supset>#" 50)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   511
  where "supset_mset A B \<equiv> B \<subset># A"
62208
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   512
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   513
notation (input)
62208
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   514
  subseteq_mset  (infix "\<le>#" 50) and
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   515
  supseteq_mset  (infix "\<ge>#" 50)
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   516
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   517
notation (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
   518
  subseteq_mset  (infix "<=#" 50) and
62208
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   519
  subset_mset  (infix "<#" 50) and
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   520
  supseteq_mset  (infix ">=#" 50) and
ad43b3ab06e4 added 'supset' variants for new '<#' etc. symbols on multisets
blanchet
parents: 62082
diff changeset
   521
  supset_mset  (infix ">#" 50)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   522
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   523
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   524
  by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym)
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   525
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   526
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   527
interpretation subset_mset: ordered_ab_semigroup_monoid_add_imp_le "op +" 0 "op -" "op \<le>#" "op <#"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   528
  by standard
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   529
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   530
lemma mset_subset_eqI:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   531
  "(\<And>a. count A a \<le> count B a) \<Longrightarrow> A \<subseteq># B"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   532
  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
   533
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   534
lemma mset_subset_eq_count:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   535
  "A \<subseteq># B \<Longrightarrow> count A a \<le> count B a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   536
  by (simp add: subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   537
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   538
lemma mset_subset_eq_exists_conv: "(A::'a multiset) \<subseteq># B \<longleftrightarrow> (\<exists>C. B = A + C)"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   539
  unfolding subseteq_mset_def
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   540
  apply (rule iffI)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   541
   apply (rule exI [where x = "B - A"])
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   542
   apply (auto intro: multiset_eq_iff [THEN iffD2])
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   543
  done
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   544
63560
3e3097ac37d1 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63547
diff changeset
   545
interpretation subset_mset: ordered_cancel_comm_monoid_diff "op +" 0 "op \<le>#" "op <#" "op -"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   546
  by standard (simp, fact mset_subset_eq_exists_conv)
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   547
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   548
lemma mset_subset_eq_mono_add_right_cancel: "(A::'a multiset) + C \<subseteq># B + C \<longleftrightarrow> A \<subseteq># B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   549
   by (fact subset_mset.add_le_cancel_right)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   550
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   551
lemma mset_subset_eq_mono_add_left_cancel: "C + (A::'a multiset) \<subseteq># C + B \<longleftrightarrow> A \<subseteq># B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   552
   by (fact subset_mset.add_le_cancel_left)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   553
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   554
lemma mset_subset_eq_mono_add: "(A::'a multiset) \<subseteq># B \<Longrightarrow> C \<subseteq># D \<Longrightarrow> A + C \<subseteq># B + D"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   555
   by (fact subset_mset.add_mono)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   556
63560
3e3097ac37d1 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63547
diff changeset
   557
lemma mset_subset_eq_add_left: "(A::'a multiset) \<subseteq># A + B"
3e3097ac37d1 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63547
diff changeset
   558
   by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   559
63560
3e3097ac37d1 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63547
diff changeset
   560
lemma mset_subset_eq_add_right: "B \<subseteq># (A::'a multiset) + B"
3e3097ac37d1 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63547
diff changeset
   561
   by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   562
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   563
lemma single_subset_iff [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   564
  "{#a#} \<subseteq># M \<longleftrightarrow> a \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   565
  by (auto simp add: subseteq_mset_def Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   566
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   567
lemma mset_subset_eq_single: "a \<in># B \<Longrightarrow> {#a#} \<subseteq># B"
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   568
  by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   569
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   570
lemma mset_subset_eq_add_mset_cancel: \<open>add_mset a A \<subseteq># add_mset a B \<longleftrightarrow> A \<subseteq># B\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   571
  unfolding add_mset_add_single[of _ A] add_mset_add_single[of _ B]
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   572
  by (rule mset_subset_eq_mono_add_right_cancel)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   573
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   574
lemma multiset_diff_union_assoc:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   575
  fixes A B C D :: "'a multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   576
  shows "C \<subseteq># B \<Longrightarrow> A + B - C = A + (B - C)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   577
  by (fact subset_mset.diff_add_assoc)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   578
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   579
lemma mset_subset_eq_multiset_union_diff_commute:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   580
  fixes A B C D :: "'a multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   581
  shows "B \<subseteq># A \<Longrightarrow> A - B + C = A + C - B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   582
  by (fact subset_mset.add_diff_assoc2)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   583
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   584
lemma diff_subset_eq_self[simp]:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   585
  "(M::'a multiset) - N \<subseteq># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   586
  by (simp add: subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   587
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   588
lemma mset_subset_eqD:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   589
  assumes "A \<subseteq># B" and "x \<in># A"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   590
  shows "x \<in># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   591
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   592
  from \<open>x \<in># A\<close> have "count A x > 0" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   593
  also from \<open>A \<subseteq># B\<close> have "count A x \<le> count B x"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   594
    by (simp add: subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   595
  finally show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   596
qed
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   597
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   598
lemma mset_subsetD:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   599
  "A \<subset># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   600
  by (auto intro: mset_subset_eqD [of A])
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   601
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   602
lemma set_mset_mono:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   603
  "A \<subseteq># B \<Longrightarrow> set_mset A \<subseteq> set_mset B"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   604
  by (metis mset_subset_eqD subsetI)
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   605
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   606
lemma mset_subset_eq_insertD:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   607
  "add_mset x A \<subseteq># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   608
apply (rule conjI)
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   609
 apply (simp add: mset_subset_eqD)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   610
 apply (clarsimp simp: subset_mset_def subseteq_mset_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   611
 apply safe
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   612
  apply (erule_tac x = a in allE)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   613
  apply (auto split: if_split_asm)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   614
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
   615
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   616
lemma mset_subset_insertD:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   617
  "add_mset x A \<subset># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   618
  by (rule mset_subset_eq_insertD) simp
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   619
63831
ea7471c921f5 more simp
nipkow
parents: 63830
diff changeset
   620
lemma mset_subset_of_empty[simp]: "A \<subset># {#} \<longleftrightarrow> False"
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   621
  by (simp only: subset_mset.not_less_zero)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   622
63831
ea7471c921f5 more simp
nipkow
parents: 63830
diff changeset
   623
lemma empty_subset_add_mset[simp]: "{#} <# add_mset x M"
ea7471c921f5 more simp
nipkow
parents: 63830
diff changeset
   624
by(auto intro: subset_mset.gr_zeroI)
ea7471c921f5 more simp
nipkow
parents: 63830
diff changeset
   625
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   626
lemma empty_le: "{#} \<subseteq># A"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   627
  by (fact subset_mset.zero_le)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   628
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   629
lemma insert_subset_eq_iff:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   630
  "add_mset a A \<subseteq># B \<longleftrightarrow> a \<in># B \<and> A \<subseteq># B - {#a#}"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   631
  using le_diff_conv2 [of "Suc 0" "count B a" "count A a"]
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   632
  apply (auto simp add: subseteq_mset_def not_in_iff Suc_le_eq)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   633
  apply (rule ccontr)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   634
  apply (auto simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   635
  done
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   636
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   637
lemma insert_union_subset_iff:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   638
  "add_mset a A \<subset># B \<longleftrightarrow> a \<in># B \<and> A \<subset># B - {#a#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   639
  by (auto simp add: insert_subset_eq_iff subset_mset_def)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   640
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   641
lemma subset_eq_diff_conv:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   642
  "A - C \<subseteq># B \<longleftrightarrow> A \<subseteq># B + C"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   643
  by (simp add: subseteq_mset_def le_diff_conv)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   644
63831
ea7471c921f5 more simp
nipkow
parents: 63830
diff changeset
   645
lemma subset_eq_empty[simp]: "M \<subseteq># {#} \<longleftrightarrow> M = {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   646
  by auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   647
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   648
lemma multi_psub_of_add_self [simp]: "A \<subset># add_mset x A"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   649
  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
   650
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   651
lemma multi_psub_self[simp]: "(A::'a multiset) \<subset># A = False"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   652
  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
   653
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   654
lemma mset_subset_add_mset [simp]: "add_mset x N \<subset># add_mset x M \<longleftrightarrow> N \<subset># M"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   655
  unfolding add_mset_add_single[of _ N] add_mset_add_single[of _ M]
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   656
  by (fact subset_mset.add_less_cancel_right)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   657
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   658
lemma mset_subset_empty_nonempty: "{#} \<subset># S \<longleftrightarrow> S \<noteq> {#}"
62378
85ed00c1fe7c generalize more theorems to support enat and ennreal
hoelzl
parents: 62376
diff changeset
   659
  by (fact subset_mset.zero_less_iff_neq_zero)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   660
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
   661
lemma mset_subset_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} \<subset># B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   662
  by (auto simp: subset_mset_def elim: mset_add)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   663
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   664
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   665
subsubsection \<open>Intersection\<close>
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   666
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   667
definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "\<inter>#" 70) where
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   668
  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
   669
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   670
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<subseteq>#" "op \<subset>#"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   671
proof -
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   672
  have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   673
    by arith
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   674
  show "class.semilattice_inf op \<inter># op \<subseteq># op \<subset>#"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   675
    by standard (auto simp add: multiset_inter_def subseteq_mset_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   676
qed
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   677
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   678
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   679
lemma multiset_inter_count [simp]:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
   680
  fixes A B :: "'a multiset"
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   681
  shows "count (A \<inter># B) x = min (count A x) (count B x)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   682
  by (simp add: multiset_inter_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   683
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   684
lemma set_mset_inter [simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   685
  "set_mset (A \<inter># B) = set_mset A \<inter> set_mset B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   686
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] multiset_inter_count) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   687
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   688
lemma diff_intersect_left_idem [simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   689
  "M - M \<inter># N = M - N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   690
  by (simp add: multiset_eq_iff min_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   691
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   692
lemma diff_intersect_right_idem [simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   693
  "M - N \<inter># M = M - N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   694
  by (simp add: multiset_eq_iff min_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   695
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   696
lemma multiset_inter_single[simp]: "a \<noteq> b \<Longrightarrow> {#a#} \<inter># {#b#} = {#}"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   697
  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
   698
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   699
lemma multiset_union_diff_commute:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   700
  assumes "B \<inter># C = {#}"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   701
  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
   702
proof (rule multiset_eqI)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   703
  fix x
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   704
  from assms have "min (count B x) (count C x) = 0"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   705
    by (auto simp add: multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   706
  then have "count B x = 0 \<or> count C x = 0"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   707
    unfolding min_def by (auto split: if_splits)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   708
  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
   709
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   710
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   711
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   712
lemma disjunct_not_in:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   713
  "A \<inter># B = {#} \<longleftrightarrow> (\<forall>a. a \<notin># A \<or> a \<notin># B)" (is "?P \<longleftrightarrow> ?Q")
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   714
proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   715
  assume ?P
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   716
  show ?Q
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   717
  proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   718
    fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   719
    from \<open>?P\<close> have "min (count A a) (count B a) = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   720
      by (simp add: multiset_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   721
    then have "count A a = 0 \<or> count B a = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   722
      by (cases "count A a \<le> count B a") (simp_all add: min_def)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   723
    then show "a \<notin># A \<or> a \<notin># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   724
      by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   725
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   726
next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   727
  assume ?Q
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   728
  show ?P
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   729
  proof (rule multiset_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   730
    fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   731
    from \<open>?Q\<close> have "count A a = 0 \<or> count B a = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   732
      by (auto simp add: not_in_iff)
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   733
    then show "count (A \<inter># B) a = count {#} a"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   734
      by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   735
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   736
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   737
63831
ea7471c921f5 more simp
nipkow
parents: 63830
diff changeset
   738
lemma add_mset_inter_add_mset[simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   739
  "add_mset a A \<inter># add_mset a B = add_mset a (A \<inter># B)"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   740
  by (metis add_mset_add_single add_mset_diff_bothsides diff_subset_eq_self multiset_inter_def
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   741
      subset_mset.diff_add_assoc2)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   742
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   743
lemma add_mset_disjoint [simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   744
  "add_mset a A \<inter># B = {#} \<longleftrightarrow> a \<notin># B \<and> A \<inter># B = {#}"
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   745
  "{#} = add_mset a A \<inter># B \<longleftrightarrow> a \<notin># B \<and> {#} = A \<inter># B"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   746
  by (auto simp: disjunct_not_in)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   747
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   748
lemma disjoint_add_mset [simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   749
  "B \<inter># add_mset a A = {#} \<longleftrightarrow> a \<notin># B \<and> B \<inter># A = {#}"
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   750
  "{#} = A \<inter># add_mset b B \<longleftrightarrow> b \<notin># A \<and> {#} = A \<inter># B"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   751
  by (auto simp: disjunct_not_in)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   752
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   753
lemma empty_inter[simp]: "{#} \<inter># M = {#}"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   754
  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
   755
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   756
lemma inter_empty[simp]: "M \<inter># {#} = {#}"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   757
  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
   758
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   759
lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (add_mset x M) \<inter># N = M \<inter># N"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   760
  by (simp add: multiset_eq_iff not_in_iff)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   761
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   762
lemma inter_add_left2: "x \<in># N \<Longrightarrow> (add_mset x M) \<inter># N = add_mset x (M \<inter># (N - {#x#}))"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   763
  by (auto simp add: multiset_eq_iff elim: mset_add)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   764
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   765
lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N \<inter># (add_mset x M) = N \<inter># M"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   766
  by (simp add: multiset_eq_iff not_in_iff)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   767
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   768
lemma inter_add_right2: "x \<in># N \<Longrightarrow> N \<inter># (add_mset x M) = add_mset x ((N - {#x#}) \<inter># M)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   769
  by (auto simp add: multiset_eq_iff elim: mset_add)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   770
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   771
lemma disjunct_set_mset_diff:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   772
  assumes "M \<inter># N = {#}"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   773
  shows "set_mset (M - N) = set_mset M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   774
proof (rule set_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   775
  fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   776
  from assms have "a \<notin># M \<or> a \<notin># N"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   777
    by (simp add: disjunct_not_in)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   778
  then show "a \<in># M - N \<longleftrightarrow> a \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   779
    by (auto dest: in_diffD) (simp add: in_diff_count not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   780
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   781
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   782
lemma at_most_one_mset_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   783
  assumes "a \<notin># M - {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   784
  shows "set_mset (M - {#a#}) = set_mset M - {a}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   785
  using assms by (auto simp add: not_in_iff in_diff_count set_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   786
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   787
lemma more_than_one_mset_mset_diff:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   788
  assumes "a \<in># M - {#a#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   789
  shows "set_mset (M - {#a#}) = set_mset M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   790
proof (rule set_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   791
  fix b
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   792
  have "Suc 0 < count M b \<Longrightarrow> count M b > 0" by arith
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   793
  then show "b \<in># M - {#a#} \<longleftrightarrow> b \<in># M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   794
    using assms by (auto simp add: in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   795
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   796
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   797
lemma inter_iff:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   798
  "a \<in># A \<inter># B \<longleftrightarrow> a \<in># A \<and> a \<in># B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   799
  by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   800
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   801
lemma inter_union_distrib_left:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   802
  "A \<inter># B + C = (A + C) \<inter># (B + C)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   803
  by (simp add: multiset_eq_iff min_add_distrib_left)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   804
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   805
lemma inter_union_distrib_right:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   806
  "C + A \<inter># B = (C + A) \<inter># (C + B)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   807
  using inter_union_distrib_left [of A B C] by (simp add: ac_simps)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   808
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   809
lemma inter_subset_eq_union:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   810
  "A \<inter># B \<subseteq># A + B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   811
  by (auto simp add: subseteq_mset_def)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   812
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   813
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   814
subsubsection \<open>Bounded union\<close>
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   815
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   816
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "\<union>#" 70)
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   817
  where "sup_subset_mset A B = A + (B - A)" \<comment> \<open>FIXME irregular fact name\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   818
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   819
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<subseteq>#" "op \<subset>#"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   820
proof -
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   821
  have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   822
    by arith
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   823
  show "class.semilattice_sup op \<union># op \<subseteq># op \<subset>#"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
   824
    by standard (auto simp add: sup_subset_mset_def subseteq_mset_def)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   825
qed
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   826
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   827
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   828
interpretation subset_mset: bounded_lattice_bot "op \<inter>#" "op \<subseteq>#" "op \<subset>#"
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   829
  "op \<union>#" "{#}"
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   830
  by standard auto
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   831
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
   832
lemma sup_subset_mset_count [simp]: \<comment> \<open>FIXME irregular fact name\<close>
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   833
  "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
   834
  by (simp add: sup_subset_mset_def)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   835
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   836
lemma set_mset_sup [simp]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   837
  "set_mset (A \<union># B) = set_mset A \<union> set_mset B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   838
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] sup_subset_mset_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   839
    (auto simp add: not_in_iff elim: mset_add)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   840
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   841
lemma empty_sup: "{#} \<union># M = M"
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   842
  by (fact subset_mset.sup_bot.left_neutral)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   843
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   844
lemma sup_empty: "M \<union># {#} = M"
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
   845
  by (fact subset_mset.sup_bot.right_neutral)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   846
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   847
lemma sup_union_left1 [simp]: "\<not> x \<in># N \<Longrightarrow> (add_mset x M) \<union># N = add_mset x (M \<union># N)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   848
  by (simp add: multiset_eq_iff not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   849
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   850
lemma sup_union_left2: "x \<in># N \<Longrightarrow> (add_mset x M) \<union># N = add_mset x (M \<union># (N - {#x#}))"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   851
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   852
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   853
lemma sup_union_right1 [simp]: "\<not> x \<in># N \<Longrightarrow> N \<union># (add_mset x M) = add_mset x (N \<union># M)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   854
  by (simp add: multiset_eq_iff not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   855
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   856
lemma sup_union_right2: "x \<in># N \<Longrightarrow> N \<union># (add_mset x M) = add_mset x ((N - {#x#}) \<union># M)"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   857
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   858
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   859
lemma sup_union_distrib_left:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   860
  "A \<union># B + C = (A + C) \<union># (B + C)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   861
  by (simp add: multiset_eq_iff max_add_distrib_left)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   862
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   863
lemma union_sup_distrib_right:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   864
  "C + A \<union># B = (C + A) \<union># (C + B)"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   865
  using sup_union_distrib_left [of A B C] by (simp add: ac_simps)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   866
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   867
lemma union_diff_inter_eq_sup:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   868
  "A + B - A \<inter># B = A \<union># B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   869
  by (auto simp add: multiset_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   870
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   871
lemma union_diff_sup_eq_inter:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   872
  "A + B - A \<union># B = A \<inter># B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   873
  by (auto simp add: multiset_eq_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   874
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   875
lemma add_mset_union:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
   876
  \<open>add_mset a A \<union># add_mset a B = add_mset a (A \<union># B)\<close>
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   877
  by (auto simp: multiset_eq_iff max_def)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   878
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   879
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
   880
subsubsection \<open>Subset is an order\<close>
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
   881
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
   882
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   883
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63388
diff changeset
   884
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   885
subsection \<open>Replicate and repeat operations\<close>
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   886
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   887
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   888
  "replicate_mset n x = (add_mset x ^^ n) {#}"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   889
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   890
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   891
  unfolding replicate_mset_def by simp
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   892
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   893
lemma replicate_mset_Suc [simp]: "replicate_mset (Suc n) x = add_mset x (replicate_mset n x)"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   894
  unfolding replicate_mset_def by (induct n) (auto intro: add.commute)
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   895
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   896
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   897
  unfolding replicate_mset_def by (induct n) auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   898
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   899
fun repeat_mset :: "nat \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   900
  "repeat_mset 0 _ = {#}" |
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   901
  "repeat_mset (Suc n) A = A + repeat_mset n A"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   902
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   903
lemma count_repeat_mset [simp]: "count (repeat_mset i A) a = i * count A a"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   904
  by (induction i) auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   905
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   906
lemma repeat_mset_right [simp]: "repeat_mset a (repeat_mset b A) = repeat_mset (a * b) A"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   907
  by (auto simp: multiset_eq_iff left_diff_distrib')
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   908
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   909
lemma left_diff_repeat_mset_distrib': \<open>repeat_mset (i - j) u = repeat_mset i u - repeat_mset j u\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   910
  by (auto simp: multiset_eq_iff left_diff_distrib')
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   911
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   912
lemma left_add_mult_distrib_mset:
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   913
  "repeat_mset i u + (repeat_mset j u + k) = repeat_mset (i+j) u + k"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   914
  by (auto simp: multiset_eq_iff add_mult_distrib)
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   915
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   916
lemma repeat_mset_distrib:
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   917
  "repeat_mset (m + n) A = repeat_mset m A + repeat_mset n A"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   918
  by (auto simp: multiset_eq_iff Nat.add_mult_distrib)
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   919
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   920
lemma repeat_mset_distrib2[simp]:
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   921
  "repeat_mset n (A + B) = repeat_mset n A + repeat_mset n B"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   922
  by (auto simp: multiset_eq_iff add_mult_distrib2)
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   923
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   924
lemma repeat_mset_replicate_mset[simp]:
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   925
  "repeat_mset n {#a#} = replicate_mset n a"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   926
  by (auto simp: multiset_eq_iff)
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   927
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   928
lemma repeat_mset_distrib_add_mset[simp]:
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   929
  "repeat_mset n (add_mset a A) = replicate_mset n a + repeat_mset n A"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   930
  by (auto simp: multiset_eq_iff)
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   931
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   932
lemma repeat_mset_empty[simp]: "repeat_mset n {#} = {#}"
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   933
  by (induction n) simp_all
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   934
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   935
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   936
subsubsection \<open>Simprocs\<close>
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   937
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   938
lemma mset_diff_add_eq1:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   939
  "j \<le> (i::nat) \<Longrightarrow> ((repeat_mset i u + m) - (repeat_mset j u + n)) = ((repeat_mset (i-j) u + m) - n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   940
  by (auto simp: multiset_eq_iff nat_diff_add_eq1)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   941
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   942
lemma mset_diff_add_eq2:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   943
  "i \<le> (j::nat) \<Longrightarrow> ((repeat_mset i u + m) - (repeat_mset j u + n)) = (m - (repeat_mset (j-i) u + n))"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   944
  by (auto simp: multiset_eq_iff nat_diff_add_eq2)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   945
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   946
lemma mset_eq_add_iff1:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   947
   "j \<le> (i::nat) \<Longrightarrow> (repeat_mset i u + m = repeat_mset j u + n) = (repeat_mset (i-j) u + m = n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   948
  by (auto simp: multiset_eq_iff nat_eq_add_iff1)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   949
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   950
lemma mset_eq_add_iff2:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   951
   "i \<le> (j::nat) \<Longrightarrow> (repeat_mset i u + m = repeat_mset j u + n) = (m = repeat_mset (j-i) u + n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   952
  by (auto simp: multiset_eq_iff nat_eq_add_iff2)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   953
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   954
lemma mset_subseteq_add_iff1:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   955
  "j \<le> (i::nat) \<Longrightarrow> (repeat_mset i u + m \<subseteq># repeat_mset j u + n) = (repeat_mset (i-j) u + m \<subseteq># n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   956
  by (auto simp add: subseteq_mset_def nat_le_add_iff1)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   957
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   958
lemma mset_subseteq_add_iff2:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   959
  "i \<le> (j::nat) \<Longrightarrow> (repeat_mset i u + m \<subseteq># repeat_mset j u + n) = (m \<subseteq># repeat_mset (j-i) u + n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   960
  by (auto simp add: subseteq_mset_def nat_le_add_iff2)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   961
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   962
lemma mset_subset_add_iff1:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   963
  "j \<le> (i::nat) \<Longrightarrow> (repeat_mset i u + m \<subset># repeat_mset j u + n) = (repeat_mset (i-j) u + m \<subset># n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   964
  unfolding subset_mset_def by (simp add: mset_eq_add_iff1 mset_subseteq_add_iff1)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   965
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   966
lemma mset_subset_add_iff2:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   967
  "i \<le> (j::nat) \<Longrightarrow> (repeat_mset i u + m \<subset># repeat_mset j u + n) = (m \<subset># repeat_mset (j-i) u + n)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   968
  unfolding subset_mset_def by (simp add: mset_eq_add_iff2 mset_subseteq_add_iff2)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   969
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   970
ML_file "multiset_simprocs_util.ML"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   971
ML_file "multiset_simprocs.ML"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   972
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   973
simproc_setup mseteq_cancel_numerals
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   974
  ("(l::'a multiset) + m = n" | "(l::'a multiset) = m + n" |
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   975
   "add_mset a m = n" | "m = add_mset a n" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   976
   "replicate_mset p a = n" | "m = replicate_mset p a" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   977
   "repeat_mset p m = n" | "m = repeat_mset p m") =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   978
  \<open>fn phi => Multiset_Simprocs.eq_cancel_msets\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   979
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   980
simproc_setup msetless_cancel_numerals
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   981
  ("(l::'a multiset) + m \<subset># n" | "(l::'a multiset) \<subset># m + n" |
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   982
   "add_mset a m \<subset># n" | "m \<subset># add_mset a n" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   983
   "replicate_mset p r \<subset># n" | "m \<subset># replicate_mset p r" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   984
   "repeat_mset p m \<subset># n" | "m \<subset># repeat_mset p m") =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   985
  \<open>fn phi => Multiset_Simprocs.subset_cancel_msets\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   986
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   987
simproc_setup msetle_cancel_numerals
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   988
  ("(l::'a multiset) + m \<subseteq># n" | "(l::'a multiset) \<subseteq># m + n" |
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   989
   "add_mset a m \<subseteq># n" | "m \<subseteq># add_mset a n" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   990
   "replicate_mset p r \<subseteq># n" | "m \<subseteq># replicate_mset p r" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   991
   "repeat_mset p m \<subseteq># n" | "m \<subseteq># repeat_mset p m") =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   992
  \<open>fn phi => Multiset_Simprocs.subseteq_cancel_msets\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   993
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   994
simproc_setup msetdiff_cancel_numerals
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   995
  ("((l::'a multiset) + m) - n" | "(l::'a multiset) - (m + n)" |
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   996
   "add_mset a m - n" | "m - add_mset a n" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   997
   "replicate_mset p r - n" | "m - replicate_mset p r" |
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
   998
   "repeat_mset p m - n" | "m - repeat_mset p m") =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
   999
  \<open>fn phi => Multiset_Simprocs.diff_cancel_msets\<close>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1000
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1001
63358
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1002
subsubsection \<open>Conditionally complete lattice\<close>
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1003
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1004
instantiation multiset :: (type) Inf
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1005
begin
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1006
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1007
lift_definition Inf_multiset :: "'a multiset set \<Rightarrow> 'a multiset" is
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1008
  "\<lambda>A i. if A = {} then 0 else Inf ((\<lambda>f. f i) ` A)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1009
proof -
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1010
  fix A :: "('a \<Rightarrow> nat) set" assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<in> multiset"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1011
  have "finite {i. (if A = {} then 0 else Inf ((\<lambda>f. f i) ` A)) > 0}" unfolding multiset_def
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1012
  proof (cases "A = {}")
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1013
    case False
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1014
    then obtain f where "f \<in> A" by blast
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1015
    hence "{i. Inf ((\<lambda>f. f i) ` A) > 0} \<subseteq> {i. f i > 0}"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1016
      by (auto intro: less_le_trans[OF _ cInf_lower])
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1017
    moreover from \<open>f \<in> A\<close> * have "finite \<dots>" by (simp add: multiset_def)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1018
    ultimately have "finite {i. Inf ((\<lambda>f. f i) ` A) > 0}" by (rule finite_subset)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1019
    with False show ?thesis by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1020
  qed simp_all
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1021
  thus "(\<lambda>i. if A = {} then 0 else INF f:A. f i) \<in> multiset" by (simp add: multiset_def)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1022
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1023
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1024
instance ..
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1025
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1026
end
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1027
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1028
lemma Inf_multiset_empty: "Inf {} = {#}"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1029
  by transfer simp_all
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1030
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1031
lemma count_Inf_multiset_nonempty: "A \<noteq> {} \<Longrightarrow> count (Inf A) x = Inf ((\<lambda>X. count X x) ` A)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1032
  by transfer simp_all
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1033
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1034
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1035
instantiation multiset :: (type) Sup
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1036
begin
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1037
63360
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1038
definition Sup_multiset :: "'a multiset set \<Rightarrow> 'a multiset" where
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1039
  "Sup_multiset A = (if A \<noteq> {} \<and> subset_mset.bdd_above A then
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1040
           Abs_multiset (\<lambda>i. Sup ((\<lambda>X. count X i) ` A)) else {#})"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1041
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1042
lemma Sup_multiset_empty: "Sup {} = {#}"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1043
  by (simp add: Sup_multiset_def)
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1044
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1045
lemma Sup_multiset_unbounded: "\<not>subset_mset.bdd_above A \<Longrightarrow> Sup A = {#}"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1046
  by (simp add: Sup_multiset_def)
63358
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1047
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1048
instance ..
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1049
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1050
end
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1051
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1052
lemma bdd_below_multiset [simp]: "subset_mset.bdd_below A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1053
  by (intro subset_mset.bdd_belowI[of _ "{#}"]) simp_all
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1054
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1055
lemma bdd_above_multiset_imp_bdd_above_count:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1056
  assumes "subset_mset.bdd_above (A :: 'a multiset set)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1057
  shows   "bdd_above ((\<lambda>X. count X x) ` A)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1058
proof -
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1059
  from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1060
    by (auto simp: subset_mset.bdd_above_def)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1061
  hence "count X x \<le> count Y x" if "X \<in> A" for X
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1062
    using that by (auto intro: mset_subset_eq_count)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1063
  thus ?thesis by (intro bdd_aboveI[of _ "count Y x"]) auto
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1064
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1065
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1066
lemma bdd_above_multiset_imp_finite_support:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1067
  assumes "A \<noteq> {}" "subset_mset.bdd_above (A :: 'a multiset set)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1068
  shows   "finite (\<Union>X\<in>A. {x. count X x > 0})"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1069
proof -
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1070
  from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1071
    by (auto simp: subset_mset.bdd_above_def)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1072
  hence "count X x \<le> count Y x" if "X \<in> A" for X x
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1073
    using that by (auto intro: mset_subset_eq_count)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1074
  hence "(\<Union>X\<in>A. {x. count X x > 0}) \<subseteq> {x. count Y x > 0}"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1075
    by safe (erule less_le_trans)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1076
  moreover have "finite \<dots>" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1077
  ultimately show ?thesis by (rule finite_subset)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1078
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1079
63360
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1080
lemma Sup_multiset_in_multiset:
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1081
  assumes "A \<noteq> {}" "subset_mset.bdd_above A"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1082
  shows   "(\<lambda>i. SUP X:A. count X i) \<in> multiset"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1083
  unfolding multiset_def
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1084
proof
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1085
  have "{i. Sup ((\<lambda>X. count X i) ` A) > 0} \<subseteq> (\<Union>X\<in>A. {i. 0 < count X i})"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1086
  proof safe
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1087
    fix i assume pos: "(SUP X:A. count X i) > 0"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1088
    show "i \<in> (\<Union>X\<in>A. {i. 0 < count X i})"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1089
    proof (rule ccontr)
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1090
      assume "i \<notin> (\<Union>X\<in>A. {i. 0 < count X i})"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1091
      hence "\<forall>X\<in>A. count X i \<le> 0" by (auto simp: count_eq_zero_iff)
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1092
      with assms have "(SUP X:A. count X i) \<le> 0"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1093
        by (intro cSup_least bdd_above_multiset_imp_bdd_above_count) auto
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1094
      with pos show False by simp
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1095
    qed
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1096
  qed
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1097
  moreover from assms have "finite \<dots>" by (rule bdd_above_multiset_imp_finite_support)
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1098
  ultimately show "finite {i. Sup ((\<lambda>X. count X i) ` A) > 0}" by (rule finite_subset)
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1099
qed
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1100
63358
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1101
lemma count_Sup_multiset_nonempty:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1102
  assumes "A \<noteq> {}" "subset_mset.bdd_above A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1103
  shows   "count (Sup A) x = (SUP X:A. count X x)"
63360
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1104
  using assms by (simp add: Sup_multiset_def Abs_multiset_inverse Sup_multiset_in_multiset)
63358
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1105
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1106
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  1107
interpretation subset_mset: conditionally_complete_lattice Inf Sup "op \<inter>#" "op \<subseteq>#" "op \<subset>#" "op \<union>#"
63358
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1108
proof
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1109
  fix X :: "'a multiset" and A
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1110
  assume "X \<in> A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1111
  show "Inf A \<subseteq># X"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1112
  proof (rule mset_subset_eqI)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1113
    fix x
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1114
    from \<open>X \<in> A\<close> have "A \<noteq> {}" by auto
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1115
    hence "count (Inf A) x = (INF X:A. count X x)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1116
      by (simp add: count_Inf_multiset_nonempty)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1117
    also from \<open>X \<in> A\<close> have "\<dots> \<le> count X x"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1118
      by (intro cInf_lower) simp_all
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1119
    finally show "count (Inf A) x \<le> count X x" .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1120
  qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1121
next
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1122
  fix X :: "'a multiset" and A
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1123
  assume nonempty: "A \<noteq> {}" and le: "\<And>Y. Y \<in> A \<Longrightarrow> X \<subseteq># Y"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1124
  show "X \<subseteq># Inf A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1125
  proof (rule mset_subset_eqI)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1126
    fix x
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1127
    from nonempty have "count X x \<le> (INF X:A. count X x)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1128
      by (intro cInf_greatest) (auto intro: mset_subset_eq_count le)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1129
    also from nonempty have "\<dots> = count (Inf A) x" by (simp add: count_Inf_multiset_nonempty)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1130
    finally show "count X x \<le> count (Inf A) x" .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1131
  qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1132
next
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1133
  fix X :: "'a multiset" and A
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1134
  assume X: "X \<in> A" and bdd: "subset_mset.bdd_above A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1135
  show "X \<subseteq># Sup A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1136
  proof (rule mset_subset_eqI)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1137
    fix x
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1138
    from X have "A \<noteq> {}" by auto
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1139
    have "count X x \<le> (SUP X:A. count X x)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1140
      by (intro cSUP_upper X bdd_above_multiset_imp_bdd_above_count bdd)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1141
    also from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1142
      have "(SUP X:A. count X x) = count (Sup A) x" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1143
    finally show "count X x \<le> count (Sup A) x" .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1144
  qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1145
next
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1146
  fix X :: "'a multiset" and A
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1147
  assume nonempty: "A \<noteq> {}" and ge: "\<And>Y. Y \<in> A \<Longrightarrow> Y \<subseteq># X"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1148
  from ge have bdd: "subset_mset.bdd_above A" by (rule subset_mset.bdd_aboveI[of _ X])
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1149
  show "Sup A \<subseteq># X"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1150
  proof (rule mset_subset_eqI)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1151
    fix x
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1152
    from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1153
      have "count (Sup A) x = (SUP X:A. count X x)" .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1154
    also from nonempty have "\<dots> \<le> count X x"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1155
      by (intro cSup_least) (auto intro: mset_subset_eq_count ge)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1156
    finally show "count (Sup A) x \<le> count X x" .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1157
  qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1158
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1159
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1160
lemma set_mset_Inf:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1161
  assumes "A \<noteq> {}"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1162
  shows   "set_mset (Inf A) = (\<Inter>X\<in>A. set_mset X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1163
proof safe
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1164
  fix x X assume "x \<in># Inf A" "X \<in> A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1165
  hence nonempty: "A \<noteq> {}" by (auto simp: Inf_multiset_empty)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1166
  from \<open>x \<in># Inf A\<close> have "{#x#} \<subseteq># Inf A" by auto
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1167
  also from \<open>X \<in> A\<close> have "\<dots> \<subseteq># X" by (rule subset_mset.cInf_lower) simp_all
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1168
  finally show "x \<in># X" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1169
next
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1170
  fix x assume x: "x \<in> (\<Inter>X\<in>A. set_mset X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1171
  hence "{#x#} \<subseteq># X" if "X \<in> A" for X using that by auto
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1172
  from assms and this have "{#x#} \<subseteq># Inf A" by (rule subset_mset.cInf_greatest)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1173
  thus "x \<in># Inf A" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1174
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1175
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1176
lemma in_Inf_multiset_iff:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1177
  assumes "A \<noteq> {}"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1178
  shows   "x \<in># Inf A \<longleftrightarrow> (\<forall>X\<in>A. x \<in># X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1179
proof -
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1180
  from assms have "set_mset (Inf A) = (\<Inter>X\<in>A. set_mset X)" by (rule set_mset_Inf)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1181
  also have "x \<in> \<dots> \<longleftrightarrow> (\<forall>X\<in>A. x \<in># X)" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1182
  finally show ?thesis .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1183
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1184
63360
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1185
lemma in_Inf_multisetD: "x \<in># Inf A \<Longrightarrow> X \<in> A \<Longrightarrow> x \<in># X"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1186
  by (subst (asm) in_Inf_multiset_iff) auto
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1187
63358
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1188
lemma set_mset_Sup:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1189
  assumes "subset_mset.bdd_above A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1190
  shows   "set_mset (Sup A) = (\<Union>X\<in>A. set_mset X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1191
proof safe
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1192
  fix x assume "x \<in># Sup A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1193
  hence nonempty: "A \<noteq> {}" by (auto simp: Sup_multiset_empty)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1194
  show "x \<in> (\<Union>X\<in>A. set_mset X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1195
  proof (rule ccontr)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1196
    assume x: "x \<notin> (\<Union>X\<in>A. set_mset X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1197
    have "count X x \<le> count (Sup A) x" if "X \<in> A" for X x
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1198
      using that by (intro mset_subset_eq_count subset_mset.cSup_upper assms)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1199
    with x have "X \<subseteq># Sup A - {#x#}" if "X \<in> A" for X
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1200
      using that by (auto simp: subseteq_mset_def algebra_simps not_in_iff)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1201
    hence "Sup A \<subseteq># Sup A - {#x#}" by (intro subset_mset.cSup_least nonempty)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1202
    with \<open>x \<in># Sup A\<close> show False
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1203
      by (auto simp: subseteq_mset_def count_greater_zero_iff [symmetric]
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1204
               simp del: count_greater_zero_iff dest!: spec[of _ x])
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1205
  qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1206
next
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1207
  fix x X assume "x \<in> set_mset X" "X \<in> A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1208
  hence "{#x#} \<subseteq># X" by auto
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1209
  also have "X \<subseteq># Sup A" by (intro subset_mset.cSup_upper \<open>X \<in> A\<close> assms)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1210
  finally show "x \<in> set_mset (Sup A)" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1211
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1212
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1213
lemma in_Sup_multiset_iff:
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1214
  assumes "subset_mset.bdd_above A"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1215
  shows   "x \<in># Sup A \<longleftrightarrow> (\<exists>X\<in>A. x \<in># X)"
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1216
proof -
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1217
  from assms have "set_mset (Sup A) = (\<Union>X\<in>A. set_mset X)" by (rule set_mset_Sup)
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1218
  also have "x \<in> \<dots> \<longleftrightarrow> (\<exists>X\<in>A. x \<in># X)" by simp
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1219
  finally show ?thesis .
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1220
qed
a500677d4cec Conditionally complete lattice of multisets
Manuel Eberl <eberlm@in.tum.de>
parents: 63310
diff changeset
  1221
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1222
lemma in_Sup_multisetD:
63360
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1223
  assumes "x \<in># Sup A"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1224
  shows   "\<exists>X\<in>A. x \<in># X"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1225
proof -
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1226
  have "subset_mset.bdd_above A"
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1227
    by (rule ccontr) (insert assms, simp_all add: Sup_multiset_unbounded)
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1228
  with assms show ?thesis by (simp add: in_Sup_multiset_iff)
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  1229
qed
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  1230
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  1231
interpretation subset_mset: distrib_lattice "op \<inter>#" "op \<subseteq>#" "op \<subset>#" "op \<union>#"
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  1232
proof
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  1233
  fix A B C :: "'a multiset"
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  1234
  show "A \<union># (B \<inter># C) = A \<union># B \<inter># (A \<union># C)"
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  1235
    by (intro multiset_eqI) simp_all
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  1236
qed
63360
65a9eb946ff2 Tuned multiset lattice
Manuel Eberl <eberlm@in.tum.de>
parents: 63358
diff changeset
  1237
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1238
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1239
subsubsection \<open>Filter (with comprehension syntax)\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1240
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1241
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
  1242
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1243
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
  1244
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
  1245
by (rule filter_preserves_multiset)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1246
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1247
syntax (ASCII)
63689
61171cbeedde tuning whitespace in output syntax
blanchet
parents: 63660
diff changeset
  1248
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{#_ :# _./ _#})")
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1249
syntax
63689
61171cbeedde tuning whitespace in output syntax
blanchet
parents: 63660
diff changeset
  1250
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{#_ \<in># _./ _#})")
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1251
translations
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1252
  "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1253
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1254
lemma count_filter_mset [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1255
  "count (filter_mset P M) a = (if P a then count M a else 0)"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1256
  by (simp add: filter_mset.rep_eq)
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1257
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1258
lemma set_mset_filter [simp]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1259
  "set_mset (filter_mset P M) = {a \<in> set_mset M. P a}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1260
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_filter_mset) simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1261
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1262
lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1263
  by (rule multiset_eqI) simp
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1264
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1265
lemma filter_single_mset: "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
  1266
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1267
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1268
lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N"
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
  1269
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
  1270
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1271
lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1272
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1273
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  1274
lemma filter_inter_mset [simp]: "filter_mset P (M \<inter># N) = filter_mset P M \<inter># filter_mset P N"
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
  1275
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
  1276
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  1277
lemma filter_sup_mset[simp]: "filter_mset P (A \<union># B) = filter_mset P A \<union># filter_mset P B"
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  1278
  by (rule multiset_eqI) simp
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  1279
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1280
lemma filter_mset_add_mset [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1281
   "filter_mset P (add_mset x A) =
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  1282
     (if P x then add_mset x (filter_mset P A) else filter_mset P A)"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1283
   by (auto simp: multiset_eq_iff)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1284
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1285
lemma multiset_filter_subset[simp]: "filter_mset f M \<subseteq># M"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1286
  by (simp add: mset_subset_eqI)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  1287
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1288
lemma multiset_filter_mono:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1289
  assumes "A \<subseteq># B"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1290
  shows "filter_mset f A \<subseteq># filter_mset f B"
58035
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
  1291
proof -
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1292
  from assms[unfolded mset_subset_eq_exists_conv]
58035
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
  1293
  obtain C where B: "B = A + C" by auto
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
  1294
  show ?thesis unfolding B by auto
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
  1295
qed
177eeda93a8c added lemmas contributed by Rene Thiemann
blanchet
parents: 57966
diff changeset
  1296
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1297
lemma filter_mset_eq_conv:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1298
  "filter_mset P M = N \<longleftrightarrow> N \<subseteq># M \<and> (\<forall>b\<in>#N. P b) \<and> (\<forall>a\<in>#M - N. \<not> P a)" (is "?P \<longleftrightarrow> ?Q")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1299
proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1300
  assume ?P then show ?Q by auto (simp add: multiset_eq_iff in_diff_count)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1301
next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1302
  assume ?Q
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1303
  then obtain Q where M: "M = N + Q"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1304
    by (auto simp add: mset_subset_eq_exists_conv)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1305
  then have MN: "M - N = Q" by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1306
  show ?P
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1307
  proof (rule multiset_eqI)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1308
    fix a
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1309
    from \<open>?Q\<close> MN have *: "\<not> P a \<Longrightarrow> a \<notin># N" "P a \<Longrightarrow> a \<notin># Q"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1310
      by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1311
    show "count (filter_mset P M) a = count N a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1312
    proof (cases "a \<in># M")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1313
      case True
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1314
      with * show ?thesis
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1315
        by (simp add: not_in_iff M)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1316
    next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1317
      case False then have "count M a = 0"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1318
        by (simp add: not_in_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1319
      with M show ?thesis by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1320
    qed
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1321
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1322
qed
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1323
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1324
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1325
subsubsection \<open>Size\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1326
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1327
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1328
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1329
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
  1330
  by (auto simp: wcount_def add_mult_distrib)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1331
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1332
lemma wcount_add_mset:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1333
  "wcount f (add_mset x M) a = (if x = a then Suc (f a) else 0) + wcount f M a"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1334
  unfolding add_mset_add_single[of _ M] wcount_union by (auto simp: wcount_def)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1335
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1336
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
  1337
  "size_multiset f M = setsum (wcount f M) (set_mset M)"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1338
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1339
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1340
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1341
instantiation multiset :: (type) size
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1342
begin
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1343
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1344
definition size_multiset where
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1345
  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
  1346
instance ..
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1347
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
  1348
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
  1349
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1350
lemmas size_multiset_overloaded_eq =
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1351
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1352
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1353
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1354
by (simp add: size_multiset_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1355
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1356
lemma size_empty [simp]: "size {#} = 0"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1357
by (simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1358
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1359
lemma size_multiset_single : "size_multiset f {#b#} = Suc (f b)"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1360
by (simp add: size_multiset_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1361
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1362
lemma size_single: "size {#b#} = 1"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1363
by (simp add: size_multiset_overloaded_def size_multiset_single)
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1364
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1365
lemma setsum_wcount_Int:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1366
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1367
  by (induct rule: finite_induct)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1368
    (simp_all add: Int_insert_left wcount_def count_eq_zero_iff)
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1369
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1370
lemma size_multiset_union [simp]:
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1371
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
57418
6ab1c7cb0b8d fact consolidation
haftmann
parents: 56656
diff changeset
  1372
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
  1373
apply (subst Int_commute)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1374
apply (simp add: setsum_wcount_Int)
26178
nipkow
parents: 26176
diff changeset
  1375
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1376
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1377
lemma size_multiset_add_mset [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1378
  "size_multiset f (add_mset a M) = Suc (f a) + size_multiset f M"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1379
  unfolding add_mset_add_single[of _ M] size_multiset_union by (auto simp: size_multiset_single)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1380
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1381
lemma size_add_mset [simp]: "size (add_mset a A) = Suc (size A)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1382
by (simp add: size_multiset_overloaded_def wcount_add_mset)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1383
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1384
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1385
by (auto simp add: size_multiset_overloaded_def)
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1386
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1387
lemma size_multiset_eq_0_iff_empty [iff]:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1388
  "size_multiset f M = 0 \<longleftrightarrow> M = {#}"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1389
  by (auto simp add: size_multiset_eq count_eq_zero_iff)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1390
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1391
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1392
by (auto simp add: size_multiset_overloaded_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1393
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1394
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
26178
nipkow
parents: 26176
diff changeset
  1395
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
  1396
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1397
lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M"
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  1398
apply (unfold size_multiset_overloaded_eq)
26178
nipkow
parents: 26176
diff changeset
  1399
apply (drule setsum_SucD)
nipkow
parents: 26176
diff changeset
  1400
apply auto
nipkow
parents: 26176
diff changeset
  1401
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1402
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
  1403
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
  1404
  assumes "size M = Suc n"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1405
  shows "\<exists>a N. M = add_mset a N"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1406
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
  1407
  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
  1408
    by (erule size_eq_Suc_imp_elem [THEN exE])
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1409
  then have "M = add_mset a (M - {#a#})" 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
  1410
  then show ?thesis by blast
23611
65b168646309 more interpretations
nipkow
parents: 23373
diff changeset
  1411
qed
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
  1412
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1413
lemma size_mset_mono:
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1414
  fixes A B :: "'a multiset"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1415
  assumes "A \<subseteq># B"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1416
  shows "size A \<le> size B"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1417
proof -
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1418
  from assms[unfolded mset_subset_eq_exists_conv]
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1419
  obtain C where B: "B = A + C" by auto
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1420
  show ?thesis unfolding B by (induct C) auto
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1421
qed
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1422
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1423
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
  1424
by (rule size_mset_mono[OF multiset_filter_subset])
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1425
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1426
lemma size_Diff_submset:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1427
  "M \<subseteq># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1428
by (metis add_diff_cancel_left' size_union mset_subset_eq_exists_conv)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1429
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1430
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1431
subsection \<open>Induction and case splits\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1432
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1433
theorem multiset_induct [case_names empty add, induct type: multiset]:
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1434
  assumes empty: "P {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1435
  assumes add: "\<And>x M. P M \<Longrightarrow> P (add_mset x M)"
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1436
  shows "P M"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1437
proof (induct n \<equiv> "size M" arbitrary: M)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1438
  case 0 thus "P M" by (simp add: empty)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1439
next
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1440
  case (Suc k)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1441
  obtain N x where "M = add_mset x N"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1442
    using \<open>Suc k = size M\<close> [symmetric]
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1443
    using size_eq_Suc_imp_eq_union by fast
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
  1444
  with Suc add show "P M" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1445
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1446
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1447
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = add_mset a A"
26178
nipkow
parents: 26176
diff changeset
  1448
by (induct M) auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1449
55913
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
  1450
lemma multiset_cases [cases type]:
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
  1451
  obtains (empty) "M = {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1452
    | (add) x N where "M = add_mset x N"
63092
a949b2a5f51d eliminated use of empty "assms";
wenzelm
parents: 63089
diff changeset
  1453
  by (induct M) simp_all
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1454
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
  1455
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
  1456
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
  1457
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1458
lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1459
apply (subst multiset_eq_iff)
26178
nipkow
parents: 26176
diff changeset
  1460
apply auto
nipkow
parents: 26176
diff changeset
  1461
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1462
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1463
lemma mset_subset_size: "(A::'a multiset) \<subset># B \<Longrightarrow> size A < size B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1464
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
  1465
  case (empty M)
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1466
  then have "M \<noteq> {#}" by (simp add: mset_subset_empty_nonempty)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1467
  then obtain M' x where "M = add_mset x M'"
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
  1468
    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
  1469
  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
  1470
next
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1471
  case (add x S T)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1472
  have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1473
  have SxsubT: "add_mset x S \<subset># T" by fact
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1474
  then have "x \<in># T" and "S \<subset># T"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1475
    by (auto dest: mset_subset_insertD)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1476
  then obtain T' where T: "T = add_mset x T'"
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
  1477
    by (blast dest: multi_member_split)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1478
  then have "S \<subset># T'" using SxsubT
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1479
    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
  1480
  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
  1481
  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
  1482
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
  1483
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1484
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
  1485
by (cases M) auto
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  1486
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1487
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1488
subsubsection \<open>Strong induction and subset induction for multisets\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1489
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1490
text \<open>Well-foundedness of strict subset relation\<close>
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
  1491
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1492
lemma wf_subset_mset_rel: "wf {(M, N :: 'a multiset). M \<subset># N}"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1493
apply (rule wf_measure [THEN wf_subset, where f1=size])
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1494
apply (clarsimp simp: measure_def inv_image_def mset_subset_size)
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
  1495
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
  1496
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1497
lemma full_multiset_induct [case_names less]:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1498
assumes ih: "\<And>B. \<forall>(A::'a multiset). A \<subset># B \<longrightarrow> P A \<Longrightarrow> P B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1499
shows "P B"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1500
apply (rule wf_subset_mset_rel [THEN wf_induct])
58098
ff478d85129b inlined unused definition
haftmann
parents: 58035
diff changeset
  1501
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
  1502
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
  1503
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1504
lemma multi_subset_induct [consumes 2, case_names empty add]:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1505
  assumes "F \<subseteq># A"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1506
    and empty: "P {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1507
    and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (add_mset a F)"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1508
  shows "P F"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1509
proof -
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1510
  from \<open>F \<subseteq># A\<close>
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1511
  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
  1512
  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
  1513
    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
  1514
  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
  1515
    fix x F
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1516
    assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "add_mset x F \<subseteq># A"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1517
    show "P (add_mset x F)"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1518
    proof (rule insert)
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1519
      from i show "x \<in># A" by (auto dest: mset_subset_eq_insertD)
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  1520
      from i have "F \<subseteq># A" by (auto dest: mset_subset_eq_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
  1521
      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
  1522
    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
  1523
  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
  1524
qed
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1525
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1526
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1527
subsection \<open>The fold combinator\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1528
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1529
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1530
where
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1531
  "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
  1532
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1533
lemma fold_mset_empty [simp]: "fold_mset f s {#} = s"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1534
  by (simp add: fold_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1535
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1536
context comp_fun_commute
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1537
begin
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1538
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1539
lemma fold_mset_add_mset [simp]: "fold_mset f s (add_mset x M) = f x (fold_mset f s M)"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1540
proof -
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1541
  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
  1542
    by (fact comp_fun_commute_funpow)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1543
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (add_mset x M) y"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1544
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1545
  show ?thesis
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1546
  proof (cases "x \<in> set_mset M")
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1547
    case False
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1548
    then have *: "count (add_mset x M) x = 1"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1549
      by (simp add: not_in_iff)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1550
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (add_mset x M) y) s (set_mset M) =
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1551
      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
  1552
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1553
    with False * show ?thesis
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1554
      by (simp add: fold_mset_def del: count_add_mset)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1555
  next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1556
    case True
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62837
diff changeset
  1557
    define N where "N = set_mset M - {x}"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1558
    from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1559
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (add_mset x M) y) s N =
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1560
      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
  1561
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1562
    with * show ?thesis by (simp add: fold_mset_def del: count_add_mset) simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1563
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1564
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1565
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1566
corollary fold_mset_single: "fold_mset f s {#x#} = f x s"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1567
  by simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1568
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1569
lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1570
  by (induct M) (simp_all add: fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1571
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1572
lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1573
  by (induct M) (simp_all add: fold_mset_fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1574
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1575
lemma fold_mset_fusion:
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1576
  assumes "comp_fun_commute g"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1577
    and *: "\<And>x y. h (g x y) = f x (h y)"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1578
  shows "h (fold_mset g w A) = fold_mset f (h w) A"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1579
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1580
  interpret comp_fun_commute g by (fact assms)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1581
  from * show ?thesis by (induct A) auto
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1582
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1583
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1584
end
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1585
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1586
lemma union_fold_mset_add_mset: "A + B = fold_mset add_mset A B"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1587
proof -
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1588
  interpret comp_fun_commute add_mset
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1589
    by standard auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1590
  show ?thesis
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1591
    by (induction B) auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1592
qed
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1593
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1594
text \<open>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1595
  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
  1596
  subterm @{term "fold_mset F"}, code generation is not automatic. When
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1597
  interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1598
  would be code thms for @{const fold_mset} become thms like
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1599
  @{term "fold_mset F z {#} = z"} where \<open>F\<close> is not a pattern but
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1600
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1601
  constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1602
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1603
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1604
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1605
subsection \<open>Image\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1606
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1607
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1608
  "image_mset f = fold_mset (add_mset \<circ> f) {#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1609
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1610
lemma comp_fun_commute_mset_image: "comp_fun_commute (add_mset \<circ> f)"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1611
proof
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1612
qed (simp add: fun_eq_iff)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1613
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1614
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1615
  by (simp add: image_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1616
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1617
lemma image_mset_single: "image_mset f {#x#} = {#f x#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1618
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1619
  interpret comp_fun_commute "add_mset \<circ> f"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1620
    by (fact comp_fun_commute_mset_image)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1621
  show ?thesis by (simp add: image_mset_def)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1622
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1623
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1624
lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1625
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1626
  interpret comp_fun_commute "add_mset \<circ> f"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1627
    by (fact comp_fun_commute_mset_image)
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1628
  show ?thesis by (induct N) (simp_all add: image_mset_def)
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1629
qed
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1630
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1631
corollary image_mset_add_mset [simp]:
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1632
  "image_mset f (add_mset a M) = add_mset (f a) (image_mset f M)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1633
  unfolding image_mset_union add_mset_add_single[of a M] by (simp add: image_mset_single)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1634
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1635
lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1636
  by (induct M) simp_all
48040
4caf6cd063be add lemma set_of_image_mset
huffman
parents: 48023
diff changeset
  1637
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1638
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1639
  by (induct M) simp_all
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1640
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1641
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
  1642
  by (cases M) auto
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1643
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1644
lemma image_mset_If:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1645
  "image_mset (\<lambda>x. if P x then f x else g x) A =
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1646
     image_mset f (filter_mset P A) + image_mset g (filter_mset (\<lambda>x. \<not>P x) A)"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1647
  by (induction A) auto
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1648
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1649
lemma image_mset_Diff:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1650
  assumes "B \<subseteq># A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1651
  shows   "image_mset f (A - B) = image_mset f A - image_mset f B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1652
proof -
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1653
  have "image_mset f (A - B + B) = image_mset f (A - B) + image_mset f B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1654
    by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1655
  also from assms have "A - B + B = A"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1656
    by (simp add: subset_mset.diff_add)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1657
  finally show ?thesis by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1658
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1659
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1660
lemma count_image_mset:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1661
  "count (image_mset f A) x = (\<Sum>y\<in>f -` {x} \<inter> set_mset A. count A y)"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1662
proof (induction A)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1663
  case empty
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1664
  then show ?case by simp
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1665
next
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1666
  case (add x A)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1667
  moreover have *: "(if x = y then Suc n else n) = n + (if x = y then 1 else 0)" for n y
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1668
    by simp
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1669
  ultimately show ?case
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1670
    by (auto simp: setsum.distrib setsum.delta' intro!: setsum.mono_neutral_left)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1671
qed
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1672
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  1673
lemma image_mset_subseteq_mono: "A \<subseteq># B \<Longrightarrow> image_mset f A \<subseteq># image_mset f B"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  1674
  by (metis image_mset_union subset_mset.le_iff_add)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  1675
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1676
syntax (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1677
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1678
syntax
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1679
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1680
translations
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1681
  "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1682
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1683
syntax (ASCII)
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1684
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1685
syntax
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  1686
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1687
translations
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1688
  "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1689
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1690
text \<open>
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1691
  This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"}
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1692
  but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source]
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1693
  "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1694
  @{term "{#x+x|x\<in>#M. x<c#}"}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1695
\<close>
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1696
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1697
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1698
by (metis set_image_mset)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1699
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
  1700
functor image_mset: image_mset
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1701
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1702
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1703
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1704
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1705
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1706
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1707
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1708
  show "image_mset id = id"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1709
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1710
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1711
    show "image_mset id A = id A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1712
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1713
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1714
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1715
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1716
declare
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1717
  image_mset.id [simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1718
  image_mset.identity [simp]
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1719
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1720
lemma image_mset_id[simp]: "image_mset id x = x"
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1721
  unfolding id_def by auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1722
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1723
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
  1724
  by (induct M) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1725
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1726
lemma image_mset_cong_pair:
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1727
  "(\<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
  1728
  by (metis image_mset_cong split_cong)
49717
56494eedf493 default simp rule for image under identity
haftmann
parents: 49394
diff changeset
  1729
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
  1730
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  1731
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
  1732
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1733
primrec mset :: "'a list \<Rightarrow> 'a multiset" where
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1734
  "mset [] = {#}" |
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1735
  "mset (a # x) = add_mset a (mset 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
  1736
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1737
lemma in_multiset_in_set:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1738
  "x \<in># mset xs \<longleftrightarrow> x \<in> set xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1739
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1740
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1741
lemma count_mset:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1742
  "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1743
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1744
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1745
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1746
  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
  1747
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1748
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
  1749
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
  1750
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1751
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
  1752
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
  1753
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1754
lemma set_mset_comp_mset [simp]: "set_mset \<circ> mset = set"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1755
  by (simp add: fun_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1756
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1757
lemma size_mset [simp]: "size (mset xs) = length xs"
48012
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
  1758
  by (induct xs) simp_all
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
  1759
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1760
lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1761
  by (induct xs arbitrary: ys) 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
  1762
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1763
lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1764
  by (induct xs) simp_all
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1765
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1766
lemma mset_rev [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1767
  "mset (rev xs) = mset xs"
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
  1768
  by (induct xs) simp_all
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
  1769
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1770
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
  1771
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
  1772
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
  1773
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
  1774
 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
  1775
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
  1776
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
  1777
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
  1778
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1779
lemma distinct_count_atmost_1:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1780
  "distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1781
proof (induct x)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1782
  case Nil then show ?case by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1783
next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1784
  case (Cons x xs) show ?case (is "?lhs \<longleftrightarrow> ?rhs")
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1785
  proof
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1786
    assume ?lhs then show ?rhs using Cons by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1787
  next
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1788
    assume ?rhs then have "x \<notin> set xs"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1789
      by (simp split: if_splits)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1790
    moreover from \<open>?rhs\<close> have "(\<forall>a. count (mset xs) a =
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1791
       (if a \<in> set xs then 1 else 0))"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1792
      by (auto split: if_splits simp add: count_eq_zero_iff)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1793
    ultimately show ?lhs using Cons by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1794
  qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1795
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1796
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1797
lemma mset_eq_setD:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1798
  assumes "mset xs = mset ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1799
  shows "set xs = set ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1800
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1801
  from assms have "set_mset (mset xs) = set_mset (mset ys)"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1802
    by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1803
  then show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  1804
qed
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1805
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1806
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
  1807
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1808
    (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
  1809
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
  1810
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1811
lemma set_eq_iff_mset_remdups_eq:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1812
   "(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
  1813
apply (rule iffI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1814
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
  1815
apply (drule distinct_remdups [THEN distinct_remdups
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1816
      [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
  1817
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
  1818
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
  1819
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1820
lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1821
  by (induct xs) 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
  1822
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  1823
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1824
proof (induct ls arbitrary: i)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1825
  case Nil
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1826
  then show ?case by simp
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1827
next
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1828
  case Cons
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1829
  then show ?case by (cases i) auto
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1830
qed
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1831
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1832
lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1833
  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
  1834
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1835
lemma mset_eq_length:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1836
  assumes "mset xs = mset ys"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1837
  shows "length xs = length ys"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1838
  using assms by (metis size_mset)
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1839
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1840
lemma mset_eq_length_filter:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1841
  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
  1842
  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
  1843
  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
  1844
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1845
lemma fold_multiset_equiv:
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1846
  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
  1847
    and equiv: "mset xs = mset ys"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1848
  shows "List.fold f xs = List.fold f ys"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1849
  using f equiv [symmetric]
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1850
proof (induct xs arbitrary: ys)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1851
  case Nil
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1852
  then show ?case by simp
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1853
next
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1854
  case (Cons x xs)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1855
  then have *: "set ys = set (x # xs)"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1856
    by (blast dest: mset_eq_setD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  1857
  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
  1858
    by (rule Cons.prems(1)) (simp_all add: *)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1859
  moreover from * have "x \<in> set ys"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1860
    by simp
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1861
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1862
    by (fact fold_remove1_split)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1863
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)"
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  1864
    by (auto intro: Cons.hyps)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1865
  ultimately show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1866
qed
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1867
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1868
lemma mset_insort [simp]: "mset (insort x xs) = add_mset x (mset xs)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1869
  by (induct xs) simp_all
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1870
63524
4ec755485732 adding mset_map to the simp rules
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63410
diff changeset
  1871
lemma mset_map[simp]: "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
  1872
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1873
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1874
global_interpretation mset_set: folding add_mset "{#}"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1875
  defines mset_set = "folding.F add_mset {#}"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1876
  by standard (simp add: fun_eq_iff)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1877
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1878
lemma count_mset_set [simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1879
  "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
  1880
  "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1881
  "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
  1882
proof -
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1883
  have *: "count (mset_set A) x = 0" if "x \<notin> A" for A
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1884
  proof (cases "finite A")
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1885
    case False then show ?thesis by simp
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1886
  next
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1887
    case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  1888
  qed
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1889
  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
  1890
  by (auto elim!: Set.set_insert)
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  1891
qed \<comment> \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1892
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1893
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
  1894
  by (induct A rule: finite_induct) simp_all
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  1895
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1896
lemma mset_set_Union:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1897
  "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> mset_set (A \<union> B) = mset_set A + mset_set B"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1898
  by (induction A rule: finite_induct) auto
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1899
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1900
lemma filter_mset_mset_set [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1901
  "finite A \<Longrightarrow> filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1902
proof (induction A rule: finite_induct)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1903
  case (insert x A)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1904
  from insert.hyps have "filter_mset P (mset_set (insert x A)) =
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1905
      filter_mset P (mset_set A) + mset_set (if P x then {x} else {})"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1906
    by simp
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1907
  also have "filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1908
    by (rule insert.IH)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1909
  also from insert.hyps
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1910
    have "\<dots> + mset_set (if P x then {x} else {}) =
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1911
            mset_set ({x \<in> A. P x} \<union> (if P x then {x} else {}))" (is "_ = mset_set ?A")
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1912
     by (intro mset_set_Union [symmetric]) simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1913
  also from insert.hyps have "?A = {y\<in>insert x A. P y}" by auto
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1914
  finally show ?case .
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1915
qed simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1916
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1917
lemma mset_set_Diff:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1918
  assumes "finite A" "B \<subseteq> A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1919
  shows  "mset_set (A - B) = mset_set A - mset_set B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1920
proof -
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1921
  from assms have "mset_set ((A - B) \<union> B) = mset_set (A - B) + mset_set B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1922
    by (intro mset_set_Union) (auto dest: finite_subset)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1923
  also from assms have "A - B \<union> B = A" by blast
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1924
  finally show ?thesis by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1925
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1926
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1927
lemma mset_set_set: "distinct xs \<Longrightarrow> mset_set (set xs) = mset xs"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1928
  by (induction xs) simp_all
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1929
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1930
context linorder
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1931
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1932
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1933
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1934
where
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  1935
  "sorted_list_of_multiset M = fold_mset insort [] M"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1936
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1937
lemma sorted_list_of_multiset_empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1938
  "sorted_list_of_multiset {#} = []"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1939
  by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1940
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1941
lemma sorted_list_of_multiset_singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1942
  "sorted_list_of_multiset {#x#} = [x]"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1943
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1944
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1945
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1946
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1947
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1948
lemma sorted_list_of_multiset_insert [simp]:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1949
  "sorted_list_of_multiset (add_mset x M) = List.insort x (sorted_list_of_multiset M)"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1950
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1951
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1952
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1953
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1954
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1955
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1956
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1957
lemma mset_sorted_list_of_multiset [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1958
  "mset (sorted_list_of_multiset M) = M"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1959
by (induct M) simp_all
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1960
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1961
lemma sorted_list_of_multiset_mset [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  1962
  "sorted_list_of_multiset (mset xs) = sort xs"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1963
by (induct xs) simp_all
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1964
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1965
lemma finite_set_mset_mset_set[simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1966
  "finite A \<Longrightarrow> set_mset (mset_set A) = A"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1967
by (induct A rule: finite_induct) simp_all
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1968
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1969
lemma mset_set_empty_iff: "mset_set A = {#} \<longleftrightarrow> A = {} \<or> infinite A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1970
  using finite_set_mset_mset_set by fastforce
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1971
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1972
lemma infinite_set_mset_mset_set:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1973
  "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1974
by simp
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1975
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1976
lemma set_sorted_list_of_multiset [simp]:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  1977
  "set (sorted_list_of_multiset M) = set_mset M"
60513
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1978
by (induct M) (simp_all add: set_insort)
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1979
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1980
lemma sorted_list_of_mset_set [simp]:
55c7316f76d6 multiset_of_set -> mset_set
nipkow
parents: 60503
diff changeset
  1981
  "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1982
by (cases "finite A") (induct A rule: finite_induct, simp_all)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1983
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1984
lemma mset_upt [simp]: "mset [m..<n] = mset_set {m..<n}"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  1985
  by (induction n) (simp_all add: atLeastLessThanSuc)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1986
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1987
lemma image_mset_map_of:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1988
  "distinct (map fst xs) \<Longrightarrow> {#the (map_of xs i). i \<in># mset (map fst xs)#} = mset (map snd xs)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1989
proof (induction xs)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1990
  case (Cons x xs)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1991
  have "{#the (map_of (x # xs) i). i \<in># mset (map fst (x # xs))#} =
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1992
          add_mset (snd x) {#the (if i = fst x then Some (snd x) else map_of xs i).
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1993
             i \<in># mset (map fst xs)#}" (is "_ = add_mset _ ?A") by simp
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1994
  also from Cons.prems have "?A = {#the (map_of xs i). i :# mset (map fst xs)#}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1995
    by (cases x, intro image_mset_cong) (auto simp: in_multiset_in_set)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1996
  also from Cons.prems have "\<dots> = mset (map snd xs)" by (intro Cons.IH) simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1997
  finally show ?case by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  1998
qed simp_all
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  1999
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2000
(* Contributed by Lukas Bulwahn *)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2001
lemma image_mset_mset_set:
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2002
  assumes "inj_on f A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2003
  shows "image_mset f (mset_set A) = mset_set (f ` A)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2004
proof cases
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2005
  assume "finite A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2006
  from this \<open>inj_on f A\<close> show ?thesis
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2007
    by (induct A) auto
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2008
next
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2009
  assume "infinite A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2010
  from this \<open>inj_on f A\<close> have "infinite (f ` A)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2011
    using finite_imageD by blast
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2012
  from \<open>infinite A\<close> \<open>infinite (f ` A)\<close> show ?thesis by simp
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2013
qed
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63908
diff changeset
  2014
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2015
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
  2016
subsection \<open>More properties of the replicate and repeat operations\<close>
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2017
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2018
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2019
  unfolding replicate_mset_def by (induct n) auto
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2020
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2021
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2022
  by (auto split: if_splits)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2023
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2024
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2025
  by (induct n, simp_all)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2026
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  2027
lemma count_le_replicate_mset_subset_eq: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<subseteq># M"
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  2028
  by (auto simp add: mset_subset_eqI) (metis count_replicate_mset subseteq_mset_def)
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2029
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2030
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2031
  by (induct D) simp_all
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2032
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2033
lemma replicate_count_mset_eq_filter_eq:
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2034
  "replicate (count (mset xs) k) k = filter (HOL.eq k) xs"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2035
  by (induct xs) auto
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2036
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2037
lemma replicate_mset_eq_empty_iff [simp]:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2038
  "replicate_mset n a = {#} \<longleftrightarrow> n = 0"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2039
  by (induct n) simp_all
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2040
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2041
lemma replicate_mset_eq_iff:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2042
  "replicate_mset m a = replicate_mset n b \<longleftrightarrow>
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2043
    m = 0 \<and> n = 0 \<or> m = n \<and> a = b"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2044
  by (auto simp add: multiset_eq_iff)
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2045
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
  2046
lemma repeat_mset_cancel1: "repeat_mset a A = repeat_mset a B \<longleftrightarrow> A = B \<or> a = 0"
63849
0dd6731060d7 delete looping simp rule
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63831
diff changeset
  2047
  by (auto simp: multiset_eq_iff)
0dd6731060d7 delete looping simp rule
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63831
diff changeset
  2048
63908
ca41b6670904 support replicate_mset in multiset simproc
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63882
diff changeset
  2049
lemma repeat_mset_cancel2: "repeat_mset a A = repeat_mset b A \<longleftrightarrow> a = b \<or> A = {#}"
63849
0dd6731060d7 delete looping simp rule
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63831
diff changeset
  2050
  by (auto simp: multiset_eq_iff)
0dd6731060d7 delete looping simp rule
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63831
diff changeset
  2051
63924
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2052
lemma image_replicate_mset [simp]:
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2053
  "image_mset f (replicate_mset n a) = replicate_mset n (f a)"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2054
  by (induct n) simp_all
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2055
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2056
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2057
subsection \<open>Big operators\<close>
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2058
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2059
locale comm_monoid_mset = comm_monoid
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2060
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2061
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2062
definition F :: "'a multiset \<Rightarrow> 'a"
63290
9ac558ab0906 boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents: 63195
diff changeset
  2063
  where eq_fold: "F M = fold_mset f \<^bold>1 M"
9ac558ab0906 boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents: 63195
diff changeset
  2064
9ac558ab0906 boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents: 63195
diff changeset
  2065
lemma empty [simp]: "F {#} = \<^bold>1"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2066
  by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2067
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2068
lemma singleton [simp]: "F {#x#} = x"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2069
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2070
  interpret comp_fun_commute
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2071
    by standard (simp add: fun_eq_iff left_commute)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2072
  show ?thesis by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2073
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2074
63290
9ac558ab0906 boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents: 63195
diff changeset
  2075
lemma union [simp]: "F (M + N) = F M \<^bold>* F N"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2076
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2077
  interpret comp_fun_commute f
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2078
    by standard (simp add: fun_eq_iff left_commute)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2079
  show ?thesis
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2080
    by (induct N) (simp_all add: left_commute eq_fold)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2081
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2082
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2083
lemma add_mset [simp]: "F (add_mset x N) = x \<^bold>* F N"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2084
  unfolding add_mset_add_single[of x N] union by (simp add: ac_simps)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2085
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2086
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2087
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 61031
diff changeset
  2088
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2089
  by standard (simp add: add_ac comp_def)
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2090
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2091
declare comp_fun_commute.fold_mset_add_mset[OF comp_fun_commute_plus_mset, simp]
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2092
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59986
diff changeset
  2093
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
  2094
  by (induct NN) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2095
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  2096
context comm_monoid_add
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  2097
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  2098
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2099
sublocale sum_mset: comm_monoid_mset plus 0
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2100
  defines sum_mset = sum_mset.F ..
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2101
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2102
lemma (in semiring_1) sum_mset_replicate_mset [simp]:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2103
  "sum_mset (replicate_mset n a) = of_nat n * a"
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2104
  by (induct n) (simp_all add: algebra_simps)
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2105
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2106
lemma setsum_unfold_sum_mset:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2107
  "setsum f A = sum_mset (image_mset f (mset_set A))"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2108
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2109
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2110
lemma sum_mset_delta: "sum_mset (image_mset (\<lambda>x. if x = y then c else 0) A) = c * count A y"
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  2111
  by (induction A) simp_all
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  2112
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2113
lemma sum_mset_delta': "sum_mset (image_mset (\<lambda>x. if y = x then c else 0) A) = c * count A y"
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  2114
  by (induction A) simp_all
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  2115
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2116
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2117
63860
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2118
lemma of_nat_sum_mset [simp]:
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2119
  "of_nat (sum_mset M) = sum_mset (image_mset of_nat M)"
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2120
by (induction M) auto
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2121
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2122
lemma sum_mset_0_iff [simp]:
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2123
  "sum_mset M = (0::'a::canonically_ordered_monoid_add)
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2124
   \<longleftrightarrow> (\<forall>x \<in> set_mset M. x = 0)"
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2125
by(induction M) (auto simp: add_eq_0_iff_both_eq_0)
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2126
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2127
lemma sum_mset_diff:
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 61031
diff changeset
  2128
  fixes M N :: "('a :: ordered_cancel_comm_monoid_diff) multiset"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2129
  shows "N \<subseteq># M \<Longrightarrow> sum_mset (M - N) = sum_mset M - sum_mset N"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2130
  by (metis add_diff_cancel_right' sum_mset.union subset_mset.diff_add)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2131
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2132
lemma size_eq_sum_mset: "size M = sum_mset (image_mset (\<lambda>_. 1) M)"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2133
proof (induct M)
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2134
  case empty then show ?case by simp
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2135
next
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2136
  case (add x M) then show ?case
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2137
    by (cases "x \<in> set_mset M")
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2138
      (simp_all add: size_multiset_overloaded_eq not_in_iff setsum.If_cases Diff_eq[symmetric]
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2139
        setsum.remove)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2140
qed
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2141
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  2142
lemma size_mset_set [simp]: "size (mset_set A) = card A"
63860
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2143
by (simp only: size_eq_sum_mset card_eq_setsum setsum_unfold_sum_mset)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63092
diff changeset
  2144
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2145
syntax (ASCII)
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2146
  "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2147
syntax
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2148
  "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2149
translations
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2150
  "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST sum_mset (CONST image_mset (\<lambda>i. b) A)"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  2151
63860
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2152
lemma sum_mset_distrib_left:
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2153
  fixes f :: "'a \<Rightarrow> 'b::semiring_0"
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2154
  shows "c * (\<Sum>x \<in># M. f x) = (\<Sum>x \<in># M. c * f(x))"
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2155
by (induction M) (simp_all add: distrib_left)
caae34eabcef more lemmas
nipkow
parents: 63849
diff changeset
  2156
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2157
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2158
  where "\<Union># MM \<equiv> sum_mset MM" \<comment> \<open>FIXME ambiguous notation --
62837
237ef2bab6c7 isabelle update_cartouches -c -t;
wenzelm
parents: 62651
diff changeset
  2159
    could likewise refer to \<open>\<Squnion>#\<close>\<close>
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2160
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2161
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
  2162
  by (induct MM) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2163
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2164
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
  2165
  by (induct MM) auto
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  2166
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2167
lemma count_setsum:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2168
  "count (setsum f A) x = setsum (\<lambda>a. count (f a) x) A"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2169
  by (induct A rule: infinite_finite_induct) simp_all
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2170
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2171
lemma setsum_eq_empty_iff:
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2172
  assumes "finite A"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2173
  shows "setsum f A = {#} \<longleftrightarrow> (\<forall>a\<in>A. f a = {#})"
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2174
  using assms by induct simp_all
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2175
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2176
lemma Union_mset_empty_conv[simp]: "\<Union># M = {#} \<longleftrightarrow> (\<forall>i\<in>#M. i = {#})"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2177
  by (induction M) auto
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2178
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  2179
context comm_monoid_mult
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  2180
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  2181
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2182
sublocale prod_mset: comm_monoid_mset times 1
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2183
  defines prod_mset = prod_mset.F ..
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2184
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2185
lemma prod_mset_empty:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2186
  "prod_mset {#} = 1"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2187
  by (fact prod_mset.empty)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2188
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2189
lemma prod_mset_singleton:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2190
  "prod_mset {#x#} = x"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2191
  by (fact prod_mset.singleton)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2192
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2193
lemma prod_mset_Un:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2194
  "prod_mset (A + B) = prod_mset A * prod_mset B"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2195
  by (fact prod_mset.union)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2196
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2197
lemma prod_mset_replicate_mset [simp]:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2198
  "prod_mset (replicate_mset n a) = a ^ n"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2199
  by (induct n) simp_all
60804
080a979a985b formal class for factorial (semi)rings
haftmann
parents: 60752
diff changeset
  2200
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2201
lemma setprod_unfold_prod_mset:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2202
  "setprod f A = prod_mset (image_mset f (mset_set A))"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2203
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2204
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2205
lemma prod_mset_multiplicity:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2206
  "prod_mset M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2207
  by (simp add: fold_mset_def setprod.eq_fold prod_mset.eq_fold funpow_times_power comp_def)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2208
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2209
lemma prod_mset_delta: "prod_mset (image_mset (\<lambda>x. if x = y then c else 1) A) = c ^ count A y"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2210
  by (induction A) simp_all
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  2211
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2212
lemma prod_mset_delta': "prod_mset (image_mset (\<lambda>x. if y = x then c else 1) A) = c ^ count A y"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2213
  by (induction A) simp_all
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63524
diff changeset
  2214
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2215
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2216
61955
e96292f32c3c former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents: 61890
diff changeset
  2217
syntax (ASCII)
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2218
  "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3PROD _:#_. _)" [0, 51, 10] 10)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2219
syntax
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2220
  "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2221
translations
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2222
  "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST prod_mset (CONST image_mset (\<lambda>i. b) A)"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2223
63924
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2224
lemma (in comm_monoid_mult) prod_mset_subset_imp_dvd:
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2225
  assumes "A \<subseteq># B"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2226
  shows   "prod_mset A dvd prod_mset B"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2227
proof -
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2228
  from assms have "B = (B - A) + A" by (simp add: subset_mset.diff_add)
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2229
  also have "prod_mset \<dots> = prod_mset (B - A) * prod_mset A" by simp
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2230
  also have "prod_mset A dvd \<dots>" by simp
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2231
  finally show ?thesis .
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2232
qed
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2233
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2234
lemma (in comm_monoid_mult) dvd_prod_mset:
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2235
  assumes "x \<in># A"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2236
  shows "x dvd prod_mset A"
63924
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2237
  using assms prod_mset_subset_imp_dvd [of "{#x#}" A] by simp
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2238
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2239
lemma (in semidom) prod_mset_zero_iff [iff]:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2240
  "prod_mset A = 0 \<longleftrightarrow> 0 \<in># A"
62366
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2241
  by (induct A) auto
95c6cf433c91 more theorems
haftmann
parents: 62324
diff changeset
  2242
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2243
lemma (in semidom_divide) prod_mset_diff:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2244
  assumes "B \<subseteq># A" and "0 \<notin># B"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2245
  shows "prod_mset (A - B) = prod_mset A div prod_mset B"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2246
proof -
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2247
  from assms obtain C where "A = B + C"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2248
    by (metis subset_mset.add_diff_inverse)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2249
  with assms show ?thesis by simp
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2250
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2251
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2252
lemma (in semidom_divide) prod_mset_minus:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2253
  assumes "a \<in># A" and "a \<noteq> 0"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2254
  shows "prod_mset (A - {#a#}) = prod_mset A div a"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2255
  using assms prod_mset_diff [of "{#a#}" A] by auto
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2256
63924
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2257
lemma (in algebraic_semidom) is_unit_prod_mset_iff:
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2258
  "is_unit (prod_mset A) \<longleftrightarrow> (\<forall>x \<in># A. is_unit x)"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2259
  by (induct A) (auto simp: is_unit_mult_iff)
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2260
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2261
lemma (in normalization_semidom) normalize_prod_mset:
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2262
  "normalize (prod_mset A) = prod_mset (image_mset normalize A)"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2263
  by (induct A) (simp_all add: normalize_mult)
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2264
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2265
lemma (in normalization_semidom) normalized_prod_msetI:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2266
  assumes "\<And>a. a \<in># A \<Longrightarrow> normalize a = a"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  2267
  shows "normalize (prod_mset A) = prod_mset A"
63924
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2268
proof -
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2269
  from assms have "image_mset normalize A = A"
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2270
    by (induct A) simp_all
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2271
  then show ?thesis by (simp add: normalize_prod_mset)
f91766530e13 more generic algebraic lemmas
haftmann
parents: 63922
diff changeset
  2272
qed
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2273
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2274
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2275
subsection \<open>Alternative representations\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2276
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2277
subsubsection \<open>Lists\<close>
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  2278
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2279
context linorder
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2280
begin
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2281
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2282
lemma mset_insort [simp]:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2283
  "mset (insort_key k x xs) = add_mset x (mset xs)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2284
  by (induct xs) simp_all
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2285
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2286
lemma mset_sort [simp]:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2287
  "mset (sort_key k xs) = mset xs"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2288
  by (induct xs) simp_all
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  2289
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2290
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
  2291
  This lemma shows which properties suffice to show that a function
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  2292
  \<open>f\<close> with \<open>f xs = ys\<close> behaves like sort.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2293
\<close>
37074
322d065ebef7 localized properties_for_sort
haftmann
parents: 36903
diff changeset
  2294
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2295
lemma properties_for_sort_key:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2296
  assumes "mset ys = mset xs"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2297
    and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2298
    and "sorted (map f ys)"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2299
  shows "sort_key f xs = ys"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2300
  using assms
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  2301
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
  2302
  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
  2303
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
  2304
  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
  2305
  from Cons.prems(2) have
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  2306
    "\<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
  2307
    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
  2308
  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
  2309
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2310
  moreover from Cons.prems have "x \<in># mset ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2311
    by auto
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2312
  then have "x \<in> set ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2313
    by simp
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2314
  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
  2315
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
  2316
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2317
lemma properties_for_sort:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2318
  assumes multiset: "mset ys = mset xs"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2319
    and "sorted ys"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2320
  shows "sort xs = ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2321
proof (rule properties_for_sort_key)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2322
  from multiset show "mset ys = mset xs" .
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2323
  from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2324
  from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2325
    by (rule mset_eq_length_filter)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2326
  then have "replicate (length (filter (\<lambda>y. k = y) ys)) k =
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2327
    replicate (length (filter (\<lambda>x. k = x) xs)) k" for k
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2328
    by simp
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2329
  then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2330
    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
  2331
qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2332
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2333
lemma sort_key_inj_key_eq:
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2334
  assumes mset_equal: "mset xs = mset ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2335
    and "inj_on f (set xs)"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2336
    and "sorted (map f ys)"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2337
  shows "sort_key f xs = ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2338
proof (rule properties_for_sort_key)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2339
  from mset_equal
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2340
  show "mset ys = mset xs" by simp
61188
b34551d94934 isabelle update_cartouches;
wenzelm
parents: 61076
diff changeset
  2341
  from \<open>sorted (map f ys)\<close>
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2342
  show "sorted (map f ys)" .
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2343
  show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2344
  proof -
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2345
    from mset_equal
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2346
    have set_equal: "set xs = set ys" by (rule mset_eq_setD)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2347
    with that have "insert k (set ys) = set ys" by auto
61188
b34551d94934 isabelle update_cartouches;
wenzelm
parents: 61076
diff changeset
  2348
    with \<open>inj_on f (set xs)\<close> have inj: "inj_on f (insert k (set ys))"
61031
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2349
      by (simp add: set_equal)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2350
    from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2351
      by (auto intro!: inj_on_filter_key_eq)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2352
    also have "\<dots> = replicate (count (mset ys) k) k"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2353
      by (simp add: replicate_count_mset_eq_filter_eq)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2354
    also have "\<dots> = replicate (count (mset xs) k) k"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2355
      using mset_equal by simp
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2356
    also have "\<dots> = filter (HOL.eq k) xs"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2357
      by (simp add: replicate_count_mset_eq_filter_eq)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2358
    also have "\<dots> = [x\<leftarrow>xs . f k = f x]"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2359
      using inj by (auto intro!: inj_on_filter_key_eq [symmetric] simp add: set_equal)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2360
    finally show ?thesis .
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2361
  qed
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2362
qed
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2363
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2364
lemma sort_key_eq_sort_key:
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2365
  assumes "mset xs = mset ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2366
    and "inj_on f (set xs)"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2367
  shows "sort_key f xs = sort_key f ys"
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2368
  by (rule sort_key_inj_key_eq) (simp_all add: assms)
162bd20dae23 more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents: 60804
diff changeset
  2369
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2370
lemma sort_key_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2371
  "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
  2372
    @ [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
  2373
    @ 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
  2374
proof (rule properties_for_sort_key)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2375
  show "mset ?rhs = mset ?lhs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2376
    by (rule multiset_eqI) (auto simp add: mset_filter)
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2377
  show "sorted (map f ?rhs)"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2378
    by (auto simp add: sorted_append intro: sorted_map_same)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2379
next
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  2380
  fix l
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  2381
  assume "l \<in> set ?rhs"
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  2382
  let ?pivot = "f (xs ! (length xs div 2))"
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  2383
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  2384
  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
  2385
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  2386
  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
  2387
  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
  2388
  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
  2389
    [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
  2390
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  2391
  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
  2392
  proof (cases "f l" ?pivot rule: linorder_cases)
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2393
    case less
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2394
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2395
    with less show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  2396
      by (simp add: filter_sort [symmetric] ** ***)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  2397
  next
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  2398
    case equal then show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  2399
      by (simp add: * less_le)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  2400
  next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2401
    case greater
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2402
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2403
    with greater show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  2404
      by (simp add: filter_sort [symmetric] ** ***)
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  2405
  qed
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2406
qed
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2407
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2408
lemma sort_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2409
  "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
  2410
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  2411
    @ 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
  2412
  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
  2413
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2414
text \<open>A stable parametrized quicksort\<close>
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2415
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2416
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
  2417
  "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
  2418
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2419
lemma part_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2420
  "part f pivot [] = ([], [], [])"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2421
  "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
  2422
     if x' < pivot then (x # lts, eqs, gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2423
     else if x' > pivot then (lts, eqs, x # gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2424
     else (lts, x # eqs, gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2425
  by (auto simp add: part_def Let_def split_def)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2426
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2427
lemma sort_key_by_quicksort_code [code]:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2428
  "sort_key f xs =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2429
    (case xs of
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2430
      [] \<Rightarrow> []
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2431
    | [x] \<Rightarrow> xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2432
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2433
    | _ \<Rightarrow>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2434
        let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2435
        in sort_key f lts @ eqs @ sort_key f gts)"
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2436
proof (cases xs)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2437
  case Nil then show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2438
next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  2439
  case (Cons _ ys) note hyps = Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  2440
  proof (cases ys)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2441
    case Nil with hyps show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2442
  next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  2443
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  2444
    proof (cases zs)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2445
      case Nil with hyps show ?thesis by auto
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2446
    next
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2447
      case Cons
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2448
      from sort_key_by_quicksort [of f xs]
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2449
      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
  2450
        in sort_key f lts @ eqs @ sort_key f gts)"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2451
      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
  2452
      with hyps Cons show ?thesis by (simp only: list.cases)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2453
    qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2454
  qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2455
qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2456
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2457
end
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  2458
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2459
hide_const (open) part
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  2460
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  2461
lemma mset_remdups_subset_eq: "mset (remdups xs) \<subseteq># mset xs"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  2462
  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
  2463
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2464
lemma mset_update:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2465
  "i < length ls \<Longrightarrow> mset (ls[i := v]) = add_mset v (mset ls - {#ls ! i#})"
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
  2466
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
  2467
  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
  2468
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
  2469
  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
  2470
  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
  2471
  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
  2472
    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
  2473
  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
  2474
    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
  2475
    with Cons show ?thesis
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2476
      by (cases \<open>x = xs ! i'\<close>) 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
  2477
  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
  2478
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
  2479
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2480
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
  2481
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2482
    mset (ls[j := ls ! i, i := ls ! j]) = mset ls"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  2483
  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
  2484
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  2485
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2486
subsection \<open>The multiset order\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2487
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2488
subsubsection \<open>Well-foundedness\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2489
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2490
definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2491
  "mult1 r = {(N, M). \<exists>a M0 K. M = add_mset a M0 \<and> N = M0 + K \<and>
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  2492
      (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2493
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2494
definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  2495
  "mult r = (mult1 r)\<^sup>+"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2496
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2497
lemma mult1I:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2498
  assumes "M = add_mset a M0" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2499
  shows "(N, M) \<in> mult1 r"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2500
  using assms unfolding mult1_def by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2501
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2502
lemma mult1E:
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2503
  assumes "(N, M) \<in> mult1 r"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2504
  obtains a M0 K where "M = add_mset a M0" "N = M0 + K" "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2505
  using assms unfolding mult1_def by blast
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2506
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2507
lemma mono_mult1:
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2508
  assumes "r \<subseteq> r'" shows "mult1 r \<subseteq> mult1 r'"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2509
unfolding mult1_def using assms by blast
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2510
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2511
lemma mono_mult:
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2512
  assumes "r \<subseteq> r'" shows "mult r \<subseteq> mult r'"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2513
unfolding mult_def using mono_mult1[OF assms] trancl_mono by blast
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2514
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2515
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  2516
by (simp add: mult1_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2517
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2518
lemma less_add:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2519
  assumes mult1: "(N, add_mset a M0) \<in> mult1 r"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2520
  shows
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2521
    "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = add_mset a M) \<or>
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2522
     (\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2523
proof -
60607
d440af2e584f more symbols;
wenzelm
parents: 60606
diff changeset
  2524
  let ?r = "\<lambda>K a. \<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2525
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = add_mset a M0 \<and> N = M0 + K \<and> ?r K a"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2526
  obtain a' M0' K where M0: "add_mset a M0 = add_mset a' M0'"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2527
    and N: "N = M0' + K"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2528
    and r: "?r K a'"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2529
    using mult1 unfolding mult1_def by auto
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2530
  show ?thesis (is "?case1 \<or> ?case2")
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2531
  proof -
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2532
    from M0 consider "M0 = M0'" "a = a'"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2533
      | K' where "M0 = add_mset a' K'" "M0' = add_mset a K'"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2534
      by atomize_elim (simp only: add_eq_conv_ex)
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  2535
    then show ?thesis
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2536
    proof cases
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2537
      case 1
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  2538
      with N r have "?r K a \<and> N = M0 + K" by simp
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2539
      then have ?case2 ..
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2540
      then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2541
    next
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2542
      case 2
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2543
      from N 2(2) have n: "N = add_mset a (K' + K)" by simp
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2544
      with r 2(1) have "?R (K' + K) M0" by blast
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2545
      with n have ?case1 by (simp add: mult1_def)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2546
      then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2547
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2548
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2549
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2550
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2551
lemma all_accessible:
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2552
  assumes "wf r"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2553
  shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2554
proof
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2555
  let ?R = "mult1 r"
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 52289
diff changeset
  2556
  let ?W = "Wellfounded.acc ?R"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2557
  {
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2558
    fix M M0 a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2559
    assume M0: "M0 \<in> ?W"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2560
      and wf_hyp: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. add_mset b M \<in> ?W)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2561
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R \<longrightarrow> add_mset a M \<in> ?W"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2562
    have "add_mset a M0 \<in> ?W"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2563
    proof (rule accI [of "add_mset a M0"])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2564
      fix N
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2565
      assume "(N, add_mset a M0) \<in> ?R"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2566
      then consider M where "(M, M0) \<in> ?R" "N = add_mset a M"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2567
        | K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K"
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2568
        by atomize_elim (rule less_add)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2569
      then show "N \<in> ?W"
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2570
      proof cases
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2571
        case 1
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2572
        from acc_hyp have "(M, M0) \<in> ?R \<longrightarrow> add_mset a M \<in> ?W" ..
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2573
        from this and \<open>(M, M0) \<in> ?R\<close> have "add_mset a M \<in> ?W" ..
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2574
        then show "N \<in> ?W" by (simp only: \<open>N = add_mset a M\<close>)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2575
      next
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2576
        case 2
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2577
        from this(1) have "M0 + K \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2578
        proof (induct K)
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  2579
          case empty
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2580
          from M0 show "M0 + {#} \<in> ?W" by simp
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  2581
        next
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2582
          case (add x K)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2583
          from add.prems have "(x, a) \<in> r" by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2584
          with wf_hyp have "\<forall>M \<in> ?W. add_mset x M \<in> ?W" by blast
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2585
          moreover from add have "M0 + K \<in> ?W" by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2586
          ultimately have "add_mset x (M0 + K) \<in> ?W" ..
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2587
          then show "M0 + (add_mset x K) \<in> ?W" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2588
        qed
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2589
        then show "N \<in> ?W" by (simp only: 2(2))
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2590
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2591
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2592
  } note tedious_reasoning = this
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2593
60608
c5cbd90bd94e tuned proofs;
wenzelm
parents: 60607
diff changeset
  2594
  show "M \<in> ?W" for M
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2595
  proof (induct M)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2596
    show "{#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2597
    proof (rule accI)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2598
      fix b assume "(b, {#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2599
      with not_less_empty show "b \<in> ?W" by contradiction
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2600
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2601
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2602
    fix M a assume "M \<in> ?W"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2603
    from \<open>wf r\<close> have "\<forall>M \<in> ?W. add_mset a M \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2604
    proof induct
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2605
      fix a
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2606
      assume r: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. add_mset b M \<in> ?W)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2607
      show "\<forall>M \<in> ?W. add_mset a M \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2608
      proof
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  2609
        fix M assume "M \<in> ?W"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2610
        then show "add_mset a M \<in> ?W"
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 23281
diff changeset
  2611
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2612
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2613
    qed
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2614
    from this and \<open>M \<in> ?W\<close> show "add_mset a M \<in> ?W" ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2615
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2616
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2617
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2618
theorem wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)"
26178
nipkow
parents: 26176
diff changeset
  2619
by (rule acc_wfI) (rule all_accessible)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2620
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2621
theorem wf_mult: "wf r \<Longrightarrow> wf (mult r)"
26178
nipkow
parents: 26176
diff changeset
  2622
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2623
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2624
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2625
subsubsection \<open>Closure-free presentation\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2626
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2627
text \<open>One direction.\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2628
lemma mult_implies_one_step:
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2629
  assumes
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2630
    trans: "trans r" and
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2631
    MN: "(M, N) \<in> mult r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2632
  shows "\<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2633
  using MN unfolding mult_def mult1_def
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2634
proof (induction rule: converse_trancl_induct)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2635
  case (base y)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2636
  then show ?case by force
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2637
next
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2638
  case (step y z) note yz = this(1) and zN = this(2) and N_decomp = this(3)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2639
  obtain I J K where
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2640
    N: "N = I + J" "z = I + K" "J \<noteq> {#}" "\<forall>k\<in>#K. \<exists>j\<in>#J. (k, j) \<in> r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2641
    using N_decomp by blast
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2642
  obtain a M0 K' where
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2643
    z: "z = add_mset a M0" and y: "y = M0 + K'" and K: "\<forall>b. b \<in># K' \<longrightarrow> (b, a) \<in> r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2644
    using yz by blast
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2645
  show ?case
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2646
  proof (cases "a \<in># K")
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2647
    case True
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2648
    moreover have "\<exists>j\<in>#J. (k, j) \<in> r" if "k \<in># K'" for k
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2649
      using K N trans True by (meson that transE)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2650
    ultimately show ?thesis
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2651
      by (rule_tac x = I in exI, rule_tac x = J in exI, rule_tac x = "(K - {#a#}) + K'" in exI)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2652
        (use z y N in \<open>auto simp: subset_mset.add_diff_assoc dest: in_diffD\<close>)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2653
  next
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2654
    case False
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2655
    then have "a \<in># I" by (metis N(2) union_iff union_single_eq_member z)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2656
    moreover have "M0 = I + K - {#a#}"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2657
      using N(2) z by force
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2658
    ultimately show ?thesis
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2659
      by (rule_tac x = "I - {#a#}" in exI, rule_tac x = "add_mset a J" in exI,
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2660
          rule_tac x = "K + K'" in exI)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2661
        (use z y N False K in \<open>auto simp: subset_mset.diff_add_assoc2\<close>)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2662
  qed
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2663
qed
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2664
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  2665
lemma one_step_implies_mult:
63795
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2666
  assumes
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2667
    "J \<noteq> {#}" and
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2668
    "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2669
  shows "(I + K, I + J) \<in> mult r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2670
  using assms
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2671
proof (induction "size J" arbitrary: I J K)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2672
  case 0
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2673
  then show ?case by auto
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2674
next
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2675
  case (Suc n) note IH = this(1) and size_J = this(2)[THEN sym]
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2676
  obtain J' a where J: "J = add_mset a J'"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2677
    using size_J by (blast dest: size_eq_Suc_imp_eq_union)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2678
  show ?case
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2679
  proof (cases "J' = {#}")
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2680
    case True
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2681
    then show ?thesis
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2682
      using J Suc by (fastforce simp add: mult_def mult1_def)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2683
  next
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2684
    case [simp]: False
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2685
    have K: "K = {#x \<in># K. (x, a) \<in> r#} + {#x \<in># K. (x, a) \<notin> r#}"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2686
      by (rule multiset_partition)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2687
    have "(I + K, (I + {# x \<in># K. (x, a) \<in> r #}) + J') \<in> mult r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2688
      using IH[of J' "{# x \<in># K. (x, a) \<notin> r#}" "I + {# x \<in># K. (x, a) \<in> r#}"]
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2689
        J Suc.prems K size_J by (auto simp: ac_simps)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2690
    moreover have "(I + {#x \<in># K. (x, a) \<in> r#} + J', I + J) \<in> mult r"
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2691
      by (fastforce simp: J mult1_def mult_def)
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2692
    ultimately show ?thesis
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2693
      unfolding mult_def by simp
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2694
  qed
7f6128adfe67 tuning multisets; more interpretations
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63794
diff changeset
  2695
qed
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2696
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2697
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2698
subsection \<open>The multiset extension is cancellative for multiset union\<close>
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2699
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2700
lemma mult_cancel:
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2701
  assumes "trans s" and "irrefl s"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2702
  shows "(X + Z, Y + Z) \<in> mult s \<longleftrightarrow> (X, Y) \<in> mult s" (is "?L \<longleftrightarrow> ?R")
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2703
proof
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2704
  assume ?L thus ?R
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2705
  proof (induct Z)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2706
    case (add z Z)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2707
    obtain X' Y' Z' where *: "add_mset z X + Z = Z' + X'" "add_mset z Y + Z = Z' + Y'" "Y' \<noteq> {#}"
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2708
      "\<forall>x \<in> set_mset X'. \<exists>y \<in> set_mset Y'. (x, y) \<in> s"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2709
      using mult_implies_one_step[OF `trans s` add(2)] by auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2710
    consider Z2 where "Z' = add_mset z Z2" | X2 Y2 where "X' = add_mset z X2" "Y' = add_mset z Y2"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2711
      using *(1,2) by (metis add_mset_remove_trivial_If insert_iff set_mset_add_mset_insert union_iff)
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2712
    thus ?case
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2713
    proof (cases)
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2714
      case 1 thus ?thesis using * one_step_implies_mult[of Y' X' s Z2]
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2715
        by (auto simp: add.commute[of _ "{#_#}"] add.assoc intro: add(1))
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2716
    next
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2717
      case 2 then obtain y where "y \<in> set_mset Y2" "(z, y) \<in> s" using *(4) `irrefl s`
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2718
        by (auto simp: irrefl_def)
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2719
      moreover from this transD[OF `trans s` _ this(2)]
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2720
      have "x' \<in> set_mset X2 \<Longrightarrow> \<exists>y \<in> set_mset Y2. (x', y) \<in> s" for x'
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2721
        using 2 *(4)[rule_format, of x'] by auto
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2722
      ultimately show ?thesis using  * one_step_implies_mult[of Y2 X2 s Z'] 2
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2723
        by (force simp: add.commute[of "{#_#}"] add.assoc[symmetric] intro: add(1))
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2724
    qed
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2725
  qed auto
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2726
next
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2727
  assume ?R then obtain I J K
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2728
    where "Y = I + J" "X = I + K" "J \<noteq> {#}" "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> s"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2729
    using mult_implies_one_step[OF `trans s`] by blast
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2730
  thus ?L using one_step_implies_mult[of J K s "I + Z"] by (auto simp: ac_simps)
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2731
qed
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2732
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2733
lemma mult_cancel_max:
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2734
  assumes "trans s" and "irrefl s"
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2735
  shows "(X, Y) \<in> mult s \<longleftrightarrow> (X - X \<inter># Y, Y - X \<inter># Y) \<in> mult s" (is "?L \<longleftrightarrow> ?R")
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2736
proof -
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2737
  have "X - X \<inter># Y + X \<inter># Y = X" "Y - X \<inter># Y + X \<inter># Y = Y" by (auto simp: count_inject[symmetric])
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2738
  thus ?thesis using mult_cancel[OF assms, of "X - X \<inter># Y"  "X \<inter># Y" "Y - X \<inter># Y"] by auto
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2739
qed
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2740
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2741
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2742
subsection \<open>Quasi-executable version of the multiset extension\<close>
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2743
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2744
text \<open>
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2745
  Predicate variants of \<open>mult\<close> and the reflexive closure of \<open>mult\<close>, which are
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2746
  executable whenever the given predicate \<open>P\<close> is. Together with the standard
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2747
  code equations for \<open>op \<inter>#\<close> and \<open>op -\<close> this should yield quadratic
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2748
  (with respect to calls to \<open>P\<close>) implementations of \<open>multp\<close> and \<open>multeqp\<close>.
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2749
\<close>
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2750
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2751
definition multp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2752
  "multp P N M =
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2753
    (let Z = M \<inter># N; X = M - Z in
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2754
    X \<noteq> {#} \<and> (let Y = N - Z in (\<forall>y \<in> set_mset Y. \<exists>x \<in> set_mset X. P y x)))"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2755
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2756
definition multeqp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2757
  "multeqp P N M =
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2758
    (let Z = M \<inter># N; X = M - Z; Y = N - Z in
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2759
    (\<forall>y \<in> set_mset Y. \<exists>x \<in> set_mset X. P y x))"
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2760
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2761
lemma multp_iff:
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2762
  assumes "irrefl R" and "trans R" and [simp]: "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2763
  shows "multp P N M \<longleftrightarrow> (N, M) \<in> mult R" (is "?L \<longleftrightarrow> ?R")
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2764
proof -
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2765
  have *: "M \<inter># N + (N - M \<inter># N) = N" "M \<inter># N + (M - M \<inter># N) = M"
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2766
    "(M - M \<inter># N) \<inter># (N - M \<inter># N) = {#}" by (auto simp: count_inject[symmetric])
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2767
  show ?thesis
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2768
  proof
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2769
    assume ?L thus ?R
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2770
      using one_step_implies_mult[of "M - M \<inter># N" "N - M \<inter># N" R "M \<inter># N"] *
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2771
      by (auto simp: multp_def Let_def)
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2772
  next
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2773
    { fix I J K :: "'a multiset" assume "(I + J) \<inter># (I + K) = {#}"
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2774
      then have "I = {#}" by (metis inter_union_distrib_right union_eq_empty)
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2775
    } note [dest!] = this
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2776
    assume ?R thus ?L
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2777
      using mult_implies_one_step[OF assms(2), of "N - M \<inter># N" "M - M \<inter># N"]
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2778
        mult_cancel_max[OF assms(2,1), of "N" "M"] * by (auto simp: multp_def)
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2779
  qed
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2780
qed
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2781
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2782
lemma multeqp_iff:
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2783
  assumes "irrefl R" and "trans R" and "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2784
  shows "multeqp P N M \<longleftrightarrow> (N, M) \<in> (mult R)\<^sup>="
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2785
proof -
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2786
  { assume "N \<noteq> M" "M - M \<inter># N = {#}"
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2787
    then obtain y where "count N y \<noteq> count M y" by (auto simp: count_inject[symmetric])
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  2788
    then have "\<exists>y. count M y < count N y" using `M - M \<inter># N = {#}`
63660
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2789
      by (auto simp: count_inject[symmetric] dest!: le_neq_implies_less fun_cong[of _ _ y])
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2790
  }
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2791
  then have "multeqp P N M \<longleftrightarrow> multp P N M \<or> N = M"
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2792
    by (auto simp: multeqp_def multp_def Let_def in_diff_count)
76302202a92d add monotonicity propertyies of `mult1` and `mult`
Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
parents: 63560
diff changeset
  2793
  thus ?thesis using multp_iff[OF assms] by simp
63088
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2794
qed
f2177f5d2aed a quasi-recursive characterization of the multiset order (by Christian Sternagel)
haftmann
parents: 63060
diff changeset
  2795
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2796
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2797
subsubsection \<open>Partial-order properties\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2798
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2799
lemma (in preorder) mult1_lessE:
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2800
  assumes "(N, M) \<in> mult1 {(a, b). a < b}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2801
  obtains a M0 K where "M = add_mset a M0" "N = M0 + K"
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2802
    "a \<notin># K" "\<And>b. b \<in># K \<Longrightarrow> b < a"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2803
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2804
  from assms obtain a M0 K where "M = add_mset a M0" "N = M0 + K" and
63539
70d4d9e5707b tuned proofs -- avoid improper use of "this";
wenzelm
parents: 63524
diff changeset
  2805
    *: "b \<in># K \<Longrightarrow> b < a" for b by (blast elim: mult1E)
70d4d9e5707b tuned proofs -- avoid improper use of "this";
wenzelm
parents: 63524
diff changeset
  2806
  moreover from * [of a] have "a \<notin># K" by auto
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2807
  ultimately show thesis by (auto intro: that)
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2808
qed
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  2809
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2810
instantiation multiset :: (preorder) order
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2811
begin
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2812
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2813
definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2814
  where "M' < M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2815
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2816
definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2817
  where "less_eq_multiset M' M \<longleftrightarrow> M' < M \<or> M' = M"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2818
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2819
instance
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2820
proof -
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2821
  have irrefl: "\<not> M < M" for M :: "'a multiset"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2822
  proof
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2823
    assume "M < M"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2824
    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
  2825
    have "trans {(x'::'a, x). x' < x}"
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2826
      by (metis (mono_tags, lifting) case_prodD case_prodI less_trans mem_Collect_eq transI)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2827
    moreover note MM
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2828
    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
  2829
      \<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
  2830
      by (rule mult_implies_one_step)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2831
    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
  2832
      and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" by blast
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2833
    then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2834
    have "finite (set_mset K)" by simp
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2835
    moreover note **
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2836
    ultimately have "set_mset K = {}"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2837
      by (induct rule: finite_induct) (auto intro: order_less_trans)
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  2838
    with * show False by simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2839
  qed
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2840
  have trans: "K < M \<Longrightarrow> M < N \<Longrightarrow> K < N" for K M N :: "'a multiset"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2841
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2842
  show "OFCLASS('a multiset, order_class)"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2843
    by standard (auto simp add: less_eq_multiset_def irrefl dest: trans)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2844
qed
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2845
end \<comment> \<open>FIXME avoid junk stemming from type class interpretation\<close>
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2846
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2847
lemma mset_le_irrefl [elim!]:
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2848
  fixes M :: "'a::preorder multiset"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2849
  shows "M < M \<Longrightarrow> R"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  2850
  by simp
26567
7bcebb8c2d33 instantiation replacing primitive instance plus overloaded defs; more conservative type arities
haftmann
parents: 26178
diff changeset
  2851
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2852
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2853
subsubsection \<open>Monotonicity of multiset union\<close>
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2854
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2855
lemma mult1_union: "(B, D) \<in> mult1 r \<Longrightarrow> (C + B, C + D) \<in> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  2856
apply (unfold mult1_def)
nipkow
parents: 26176
diff changeset
  2857
apply auto
nipkow
parents: 26176
diff changeset
  2858
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  2859
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
  2860
apply (simp add: add.assoc)
26178
nipkow
parents: 26176
diff changeset
  2861
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2862
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2863
lemma union_le_mono2: "B < D \<Longrightarrow> C + B < C + (D::'a::preorder multiset)"
26178
nipkow
parents: 26176
diff changeset
  2864
apply (unfold less_multiset_def mult_def)
nipkow
parents: 26176
diff changeset
  2865
apply (erule trancl_induct)
40249
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  2866
 apply (blast intro: mult1_union)
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  2867
apply (blast intro: mult1_union trancl_trans)
26178
nipkow
parents: 26176
diff changeset
  2868
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2869
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2870
lemma union_le_mono1: "B < D \<Longrightarrow> B + C < D + (C::'a::preorder multiset)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2871
apply (subst add.commute [of B C])
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2872
apply (subst add.commute [of D C])
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2873
apply (erule union_le_mono2)
26178
nipkow
parents: 26176
diff changeset
  2874
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  2875
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  2876
lemma union_less_mono:
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2877
  fixes A B C D :: "'a::preorder multiset"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2878
  shows "A < C \<Longrightarrow> B < D \<Longrightarrow> A + B < C + D"
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2879
  by (blast intro!: union_le_mono1 union_le_mono2 less_trans)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2880
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  2881
instantiation multiset :: (preorder) ordered_ab_semigroup_add
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2882
begin
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2883
instance
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2884
  by standard (auto simp add: less_eq_multiset_def intro: union_le_mono2)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  2885
end
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
  2886
63409
3f3223b90239 moved lemmas and locales around (with minor incompatibilities)
blanchet
parents: 63388
diff changeset
  2887
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2888
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
  2889
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2890
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
  2891
  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
  2892
  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
  2893
  by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2894
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2895
definition "ms_strict = mult pair_less"
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  2896
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
  2897
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2898
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
  2899
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
  2900
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
  2901
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2902
lemma smsI:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2903
  "(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
  2904
  unfolding ms_strict_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2905
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
  2906
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2907
lemma wmsI:
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2908
  "(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
  2909
  \<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
  2910
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
  2911
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
  2912
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2913
inductive pw_leq
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2914
where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2915
  pw_leq_empty: "pw_leq {#} {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2916
| 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
  2917
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2918
lemma pw_leq_lstep:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2919
  "(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
  2920
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
  2921
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2922
lemma pw_leq_split:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2923
  assumes "pw_leq X Y"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2924
  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
  2925
  using assms
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2926
proof induct
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2927
  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
  2928
next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2929
  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
  2930
  then obtain A B Z where
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2931
    [simp]: "X = A + Z" "Y = B + Z"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2932
      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
  2933
    by auto
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2934
  from pw_leq_step consider "x = y" | "(x, y) \<in> pair_less"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2935
    unfolding pair_leq_def by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2936
  thus ?case
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2937
  proof cases
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2938
    case [simp]: 1
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2939
    have "{#x#} + X = A + ({#y#}+Z) \<and> {#y#} + Y = B + ({#y#}+Z) \<and>
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2940
      ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2941
      by auto
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2942
    thus ?thesis by blast
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2943
  next
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2944
    case 2
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2945
    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
  2946
    have "{#x#} + X = ?A' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2947
      "{#y#} + Y = ?B' + Z"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2948
      by auto
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2949
    moreover have
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2950
      "(set_mset ?A', set_mset ?B') \<in> max_strict"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2951
      using 1 2 unfolding max_strict_def
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2952
      by (auto elim!: max_ext.cases)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2953
    ultimately show ?thesis by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2954
  qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2955
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2956
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2957
lemma
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2958
  assumes pwleq: "pw_leq Z Z'"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2959
  shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2960
    and ms_weakI1:  "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2961
    and ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2962
proof -
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2963
  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
  2964
  obtain A' B' Z''
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2965
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2966
    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
  2967
    by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2968
  {
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2969
    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
  2970
    from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2971
    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
  2972
    proof
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  2973
      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
  2974
      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
  2975
        by (auto simp: max_strict_def intro: max_ext_additive)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  2976
      thus ?thesis by (rule smsI)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2977
    next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2978
      assume [simp]: "A' = {#} \<and> B' = {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2979
      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
  2980
    qed
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2981
    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
  2982
    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
  2983
  }
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2984
  from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2985
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  2986
  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
  2987
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2988
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  2989
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
  2990
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2991
and nonempty_single: "{# x #} \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2992
by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  2993
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  2994
setup \<open>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2995
  let
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2996
    fun msetT T = Type (@{type_name multiset}, [T]);
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2997
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  2998
    fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  2999
      | mk_mset T [x] =
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3000
        Const (@{const_name add_mset}, T --> msetT T --> msetT T) $ x $
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3001
          Const (@{const_abbrev Mempty}, msetT T)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3002
      | mk_mset T (x :: xs) =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3003
        Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3004
          mk_mset T [x] $ mk_mset T xs
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3005
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3006
    fun mset_member_tac ctxt m i =
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3007
      if m <= 0 then
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3008
        resolve_tac ctxt @{thms multi_member_this} i ORELSE
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3009
        resolve_tac ctxt @{thms multi_member_last} i
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3010
      else
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3011
        resolve_tac ctxt @{thms multi_member_skip} i THEN mset_member_tac ctxt (m - 1) i
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3012
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3013
    fun mset_nonempty_tac ctxt =
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3014
      resolve_tac ctxt @{thms nonempty_plus} ORELSE'
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3015
      resolve_tac ctxt @{thms nonempty_single}
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  3016
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3017
    fun regroup_munion_conv ctxt =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3018
      Function_Lib.regroup_conv ctxt @{const_abbrev Mempty} @{const_name plus}
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3019
        (map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3020
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3021
    fun unfold_pwleq_tac ctxt i =
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3022
      (resolve_tac ctxt @{thms pw_leq_step} i THEN (fn st => unfold_pwleq_tac ctxt (i + 1) st))
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3023
        ORELSE (resolve_tac ctxt @{thms pw_leq_lstep} i)
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3024
        ORELSE (resolve_tac ctxt @{thms pw_leq_empty} i)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3025
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3026
    val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union},
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3027
                        @{thm Un_insert_left}, @{thm Un_empty_left}]
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3028
  in
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3029
    ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3030
    {
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3031
      msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3032
      mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3033
      mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps,
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3034
      smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60678
diff changeset
  3035
      reduction_pair = @{thm ms_reduction_pair}
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3036
    })
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3037
  end
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3038
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3039
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3040
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3041
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
  3042
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  3043
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
  3044
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  3045
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
  3046
  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
  3047
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  3048
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
  3049
  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
  3050
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  3051
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
  3052
  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
  3053
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3054
lemmas union_ac = union_assoc union_commute union_lcomm add_mset_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
  3055
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  3056
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
  3057
  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
  3058
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  3059
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
  3060
  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
  3061
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  3062
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
  3063
  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
  3064
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3065
lemma mset_subset_trans: "(M::'a multiset) \<subset># K \<Longrightarrow> K \<subset># N \<Longrightarrow> M \<subset># N"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  3066
  by (fact subset_mset.less_trans)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3067
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3068
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
  3069
  by (fact subset_mset.inf.commute)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3070
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3071
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
  3072
  by (fact subset_mset.inf.assoc [symmetric])
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3073
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3074
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
  3075
  by (fact subset_mset.inf.left_commute)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3076
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3077
lemmas multiset_inter_ac =
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3078
  multiset_inter_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3079
  multiset_inter_assoc
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3080
  multiset_inter_left_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  3081
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  3082
lemma mset_le_not_refl: "\<not> M < (M::'a::preorder multiset)"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3083
  by (fact less_irrefl)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3084
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  3085
lemma mset_le_trans: "K < M \<Longrightarrow> M < N \<Longrightarrow> K < (N::'a::preorder multiset)"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3086
  by (fact less_trans)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3087
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  3088
lemma mset_le_not_sym: "M < N \<Longrightarrow> \<not> N < (M::'a::preorder multiset)"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3089
  by (fact less_not_sym)
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3090
63410
9789ccc2a477 more instantiations for multiset
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63409
diff changeset
  3091
lemma mset_le_asym: "M < N \<Longrightarrow> (\<not> P \<Longrightarrow> N < (M::'a::preorder multiset)) \<Longrightarrow> P"
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3092
  by (fact 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
  3093
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3094
declaration \<open>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3095
  let
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3096
    fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) (Const _ $ t') =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3097
          let
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3098
            val (maybe_opt, ps) =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3099
              Nitpick_Model.dest_plain_fun t'
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3100
              ||> op ~~
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3101
              ||> map (apsnd (snd o HOLogic.dest_number))
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3102
            fun elems_for t =
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3103
              (case AList.lookup (op =) ps t of
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3104
                SOME n => replicate n t
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3105
              | NONE => [Const (maybe_name, elem_T --> elem_T) $ t])
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3106
          in
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3107
            (case maps elems_for (all_values elem_T) @
61333
24b5e7579fdd compile
blanchet
parents: 61188
diff changeset
  3108
                 (if maybe_opt then [Const (Nitpick_Model.unrep_mixfix (), elem_T)] else []) of
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3109
              [] => Const (@{const_name zero_class.zero}, T)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3110
            | ts =>
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3111
                foldl1 (fn (s, t) => Const (@{const_name add_mset}, elem_T --> T --> T) $ s $ t)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3112
                  ts)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3113
          end
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3114
      | multiset_postproc _ _ _ _ t = t
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3115
  in Nitpick_Model.register_term_postprocessor @{typ "'a multiset"} multiset_postproc end
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3116
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3117
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3118
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3119
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
  3120
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3121
code_datatype mset
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3122
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3123
lemma [code]: "{#} = mset []"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3124
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3125
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3126
lemma [code]: "add_mset x (mset xs) = mset (x # xs)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3127
  by simp
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3128
63195
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
  3129
lemma [code]: "Multiset.is_empty (mset xs) \<longleftrightarrow> List.null xs"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
  3130
  by (simp add: Multiset.is_empty_def List.null_def)
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63099
diff changeset
  3131
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3132
lemma union_code [code]: "mset xs + mset ys = mset (xs @ ys)"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3133
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3134
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3135
lemma [code]: "image_mset f (mset xs) = mset (map f xs)"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3136
  by simp
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3137
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3138
lemma [code]: "filter_mset f (mset xs) = mset (filter f xs)"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3139
  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
  3140
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3141
lemma [code]: "mset xs - mset ys = mset (fold remove1 ys xs)"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3142
  by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute diff_diff_add)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3143
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3144
lemma [code]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3145
  "mset xs \<inter># mset ys =
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3146
    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
  3147
      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
  3148
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3149
  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
  3150
    if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3151
      (mset xs \<inter># mset ys) + mset zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3152
    by (induct xs arbitrary: ys)
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3153
      (auto simp add: inter_add_right1 inter_add_right2 ac_simps)
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3154
  then show ?thesis by simp
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3155
qed
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3156
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3157
lemma [code]:
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3158
  "mset xs \<union># mset ys =
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 61378
diff changeset
  3159
    mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3160
proof -
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 61378
diff changeset
  3161
  have "\<And>zs. mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
63919
9aed2da07200 # after multiset intersection and union symbol
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63908
diff changeset
  3162
      (mset xs \<union># mset ys) + mset zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  3163
    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
  3164
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3165
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3166
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  3167
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
  3168
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3169
lemma [code]: "count (mset xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0"
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3170
proof -
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3171
  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
  3172
    by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3173
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3174
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3175
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3176
declare set_mset_mset [code]
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3177
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3178
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
  3179
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61566
diff changeset
  3180
lemma [code]: \<comment> \<open>not very efficient, but representation-ignorant!\<close>
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3181
  "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
  3182
  apply (cases "finite A")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3183
  apply simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3184
  apply (induct A rule: finite_induct)
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  3185
  apply simp_all
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3186
  done
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3187
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3188
declare size_mset [code]
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3189
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3190
fun subset_eq_mset_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3191
  "subset_eq_mset_impl [] ys = Some (ys \<noteq> [])"
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3192
| "subset_eq_mset_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
  3193
     None \<Rightarrow> None
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3194
   | Some (ys1,_,ys2) \<Rightarrow> subset_eq_mset_impl xs (ys1 @ ys2))"
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3195
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3196
lemma subset_eq_mset_impl: "(subset_eq_mset_impl xs ys = None \<longleftrightarrow> \<not> mset xs \<subseteq># mset ys) \<and>
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3197
  (subset_eq_mset_impl xs ys = Some True \<longleftrightarrow> mset xs \<subset># mset ys) \<and>
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3198
  (subset_eq_mset_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
  3199
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
  3200
  case (Nil ys)
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3201
  show ?case by (auto simp: mset_subset_empty_nonempty)
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3202
next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3203
  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
  3204
  show ?case
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3205
  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
  3206
    case None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3207
    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
  3208
    {
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3209
      assume "mset (x # xs) \<subseteq># mset ys"
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  3210
      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
  3211
    } note nle = this
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3212
    moreover
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3213
    {
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3214
      assume "mset (x # xs) \<subset># mset ys"
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3215
      hence "mset (x # xs) \<subseteq># mset ys" by auto
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3216
      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
  3217
    }
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3218
    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
  3219
  next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3220
    case (Some res)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3221
    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
  3222
    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
  3223
    from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3224
    hence id: "mset ys = add_mset x (mset (ys1 @ ys2))"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  3225
      by auto
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3226
    show ?thesis unfolding subset_eq_mset_impl.simps
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3227
      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
  3228
      unfolding id
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3229
      using Cons[of "ys1 @ ys2"]
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59999
diff changeset
  3230
      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
  3231
  qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3232
qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3233
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3234
lemma [code]: "mset xs \<subseteq># mset ys \<longleftrightarrow> subset_eq_mset_impl xs ys \<noteq> None"
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3235
  using subset_eq_mset_impl[of xs ys] by (cases "subset_eq_mset_impl xs ys", auto)
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3236
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3237
lemma [code]: "mset xs \<subset># mset ys \<longleftrightarrow> subset_eq_mset_impl xs ys = Some True"
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3238
  using subset_eq_mset_impl[of xs ys] by (cases "subset_eq_mset_impl xs ys", auto)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3239
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3240
instantiation multiset :: (equal) equal
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3241
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3242
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3243
definition
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  3244
  [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B"
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3245
lemma [code]: "HOL.equal (mset xs) (mset ys) \<longleftrightarrow> subset_eq_mset_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
  3246
  unfolding equal_multiset_def
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63290
diff changeset
  3247
  using subset_eq_mset_impl[of xs ys] by (cases "subset_eq_mset_impl xs ys", auto)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3248
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3249
instance
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3250
  by standard (simp add: equal_multiset_def)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3251
37169
f69efa106feb make Nitpick "show_all" option behave less surprisingly
blanchet
parents: 37107
diff changeset
  3252
end
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
  3253
63882
018998c00003 renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents: 63860
diff changeset
  3254
lemma [code]: "sum_mset (mset xs) = sum_list xs"
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  3255
  by (induct xs) simp_all
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3256
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3257
lemma [code]: "prod_mset (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
  3258
proof -
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3259
  have "\<And>x. fold times xs x = prod_mset (mset xs) * x"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3260
    by (induct xs) (simp_all add: ac_simps)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3261
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3262
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3263
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3264
text \<open>
63388
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3265
  Exercise for the casual reader: add implementations for @{term "op \<le>"}
a095acd4cfbf instantiate multiset with multiset ordering
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63360
diff changeset
  3266
  and @{term "op <"} (multiset order).
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3267
\<close>
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3268
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3269
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
  3270
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3271
definition (in term_syntax)
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 61031
diff changeset
  3272
  msetify :: "'a::typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3273
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3274
  [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
  3275
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3276
notation fcomp (infixl "\<circ>>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3277
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
  3278
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3279
instantiation multiset :: (random) random
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3280
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3281
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3282
definition
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3283
  "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
  3284
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3285
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3286
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3287
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3288
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3289
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
  3290
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
  3291
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3292
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
  3293
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3294
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3295
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
  3296
where
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3297
  "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
  3298
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3299
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3300
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3301
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3302
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3303
hide_const (open) msetify
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  3304
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3305
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3306
subsection \<open>BNF setup\<close>
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3307
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3308
definition rel_mset where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3309
  "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
  3310
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3311
lemma mset_zip_take_Cons_drop_twice:
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3312
  assumes "length xs = length ys" "j \<le> length xs"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3313
  shows "mset (zip (take j xs @ x # drop j xs) (take j ys @ y # drop j ys)) =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3314
    add_mset (x,y) (mset (zip xs ys))"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3315
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3316
proof (induct xs ys arbitrary: x y j rule: list_induct2)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3317
  case Nil
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3318
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3319
    by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3320
next
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3321
  case (Cons x xs y ys)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3322
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3323
  proof (cases "j = 0")
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3324
    case True
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3325
    thus ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3326
      by simp
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3327
  next
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3328
    case False
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3329
    then obtain k where k: "j = Suc k"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3330
      by (cases j) simp
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3331
    hence "k \<le> length xs"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3332
      using Cons.prems by auto
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3333
    hence "mset (zip (take k xs @ x # drop k xs) (take k ys @ y # drop k ys)) =
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3334
      add_mset (x,y) (mset (zip xs ys))"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3335
      by (rule Cons.hyps(2))
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3336
    thus ?thesis
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  3337
      unfolding k by auto
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  3338
  qed
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3339
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3340
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3341
lemma ex_mset_zip_left:
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3342
  assumes "length xs = length ys" "mset xs' = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3343
  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
  3344
using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3345
proof (induct xs ys arbitrary: xs' rule: list_induct2)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3346
  case Nil
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3347
  thus ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3348
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3349
next
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3350
  case (Cons x xs y ys xs')
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3351
  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
  3352
    by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD)
58425
246985c6b20b simpler proof
blanchet
parents: 58247
diff changeset
  3353
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62837
diff changeset
  3354
  define xsa where "xsa = take j xs' @ drop (Suc j) xs'"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3355
  have "mset xs' = {#x#} + mset xsa"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3356
    unfolding xsa_def using j_len nth_j
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3357
    by (metis Cons_nth_drop_Suc union_mset_add_mset_right add_mset_remove_trivial add_diff_cancel_left'
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3358
        append_take_drop_id mset.simps(2) mset_append)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3359
  hence ms_x: "mset xsa = mset xs"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3360
    by (simp add: Cons.prems)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3361
  then obtain ysa where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3362
    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
  3363
    using Cons.hyps(2) by blast
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3364
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62837
diff changeset
  3365
  define ys' where "ys' = take j ysa @ y # drop j ysa"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3366
  have xs': "xs' = take j xsa @ x # drop j xsa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3367
    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
  3368
    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
  3369
      length_drop size_mset)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3370
  have j_len': "j \<le> length xsa"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3371
    using j_len xs' xsa_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3372
    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
  3373
  have "length ys' = length xs'"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3374
    unfolding ys'_def using Cons.prems len_a ms_x
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3375
    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
  3376
  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
  3377
    unfolding xs' ys'_def
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3378
    by (rule trans[OF mset_zip_take_Cons_drop_twice])
63794
bcec0534aeea clean argument of simp add
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63793
diff changeset
  3379
      (auto simp: len_a ms_a j_len')
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3380
  ultimately show ?case
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3381
    by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3382
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3383
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3384
lemma list_all2_reorder_left_invariance:
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3385
  assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3386
  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
  3387
proof -
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3388
  have len: "length xs = length ys"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3389
    using rel list_all2_conv_all_nth by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3390
  obtain ys' where
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3391
    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
  3392
    using len ms_x by (metis ex_mset_zip_left)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3393
  have "list_all2 R xs' ys'"
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3394
    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
  3395
  moreover have "mset ys' = mset ys"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3396
    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
  3397
  ultimately show ?thesis
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3398
    by blast
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3399
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3400
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3401
lemma ex_mset: "\<exists>xs. mset xs = X"
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3402
  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
  3403
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3404
inductive pred_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> bool"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3405
where
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3406
  "pred_mset P {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3407
| "\<lbrakk>P a; pred_mset P M\<rbrakk> \<Longrightarrow> pred_mset P (add_mset a M)"
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3408
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3409
bnf "'a multiset"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3410
  map: image_mset
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60400
diff changeset
  3411
  sets: set_mset
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3412
  bd: natLeq
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3413
  wits: "{#}"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3414
  rel: rel_mset
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3415
  pred: pred_mset
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3416
proof -
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3417
  show "image_mset id = id"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3418
    by (rule image_mset.id)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3419
  show "image_mset (g \<circ> f) = image_mset g \<circ> image_mset f" for f g
59813
6320064f22bb more multiset theorems
blanchet
parents: 59807
diff changeset
  3420
    unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3421
  show "(\<And>z. z \<in> set_mset X \<Longrightarrow> f z = g z) \<Longrightarrow> image_mset f X = image_mset g X" for f g X
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3422
    by (induct X) simp_all
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3423
  show "set_mset \<circ> image_mset f = op ` f \<circ> set_mset" for f
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3424
    by auto
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3425
  show "card_order natLeq"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3426
    by (rule natLeq_card_order)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3427
  show "BNF_Cardinal_Arithmetic.cinfinite natLeq"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3428
    by (rule natLeq_cinfinite)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3429
  show "ordLeq3 (card_of (set_mset X)) natLeq" for X
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3430
    by transfer
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3431
      (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3432
  show "rel_mset R OO rel_mset S \<le> rel_mset (R OO S)" for R S
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3433
    unfolding rel_mset_def[abs_def] OO_def
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3434
    apply clarify
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3435
    subgoal for X Z Y xs ys' ys zs
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3436
      apply (drule list_all2_reorder_left_invariance [where xs = ys' and ys = zs and xs' = ys])
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3437
      apply (auto intro: list_all2_trans)
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3438
      done
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3439
    done
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3440
  show "rel_mset R =
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3441
    (\<lambda>x y. \<exists>z. set_mset z \<subseteq> {(x, y). R x y} \<and>
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3442
    image_mset fst z = x \<and> image_mset snd z = y)" for R
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3443
    unfolding rel_mset_def[abs_def]
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3444
    apply (rule ext)+
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3445
    apply safe
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3446
     apply (rule_tac x = "mset (zip xs ys)" in exI;
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3447
       auto simp: in_set_zip list_all2_iff mset_map[symmetric])
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3448
    apply (rename_tac XY)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3449
    apply (cut_tac X = XY in ex_mset)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3450
    apply (erule exE)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3451
    apply (rename_tac xys)
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3452
    apply (rule_tac x = "map fst xys" in exI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3453
    apply (auto simp: mset_map)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3454
    apply (rule_tac x = "map snd xys" in exI)
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60513
diff changeset
  3455
    apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd)
59997
90fb391a15c1 tuned proofs;
wenzelm
parents: 59986
diff changeset
  3456
    done
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3457
  show "z \<in> set_mset {#} \<Longrightarrow> False" for z
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3458
    by auto
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3459
  show "pred_mset P = (\<lambda>x. Ball (set_mset x) P)" for P
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3460
  proof (intro ext iffI)
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3461
    fix x
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3462
    assume "pred_mset P x"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3463
    then show "Ball (set_mset x) P" by (induct pred: pred_mset; simp)
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3464
  next
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3465
    fix x
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3466
    assume "Ball (set_mset x) P"
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3467
    then show "pred_mset P x" by (induct x; auto intro: pred_mset.intros)
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 62208
diff changeset
  3468
  qed
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3469
qed
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3470
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3471
inductive rel_mset'
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3472
where
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3473
  Zero[intro]: "rel_mset' R {#} {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3474
| Plus[intro]: "\<lbrakk>R a b; rel_mset' R M N\<rbrakk> \<Longrightarrow> rel_mset' R (add_mset a M) (add_mset b N)"
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3475
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3476
lemma rel_mset_Zero: "rel_mset R {#} {#}"
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3477
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
  3478
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3479
declare multiset.count[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3480
declare Abs_multiset_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3481
declare multiset.count_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3482
declare union_preserves_multiset[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3483
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3484
lemma rel_mset_Plus:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3485
  assumes ab: "R a b"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3486
    and MN: "rel_mset R M N"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3487
  shows "rel_mset R (add_mset a M) (add_mset b N)"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3488
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3489
  have "\<exists>ya. add_mset a (image_mset fst y) = image_mset fst ya \<and>
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3490
    add_mset b (image_mset snd y) = image_mset snd ya \<and>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3491
    set_mset ya \<subseteq> {(x, y). R x y}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3492
    if "R a b" and "set_mset y \<subseteq> {(x, y). R x y}" for y
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3493
    using that by (intro exI[of _ "add_mset (a,b) y"]) auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3494
  thus ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3495
  using assms
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3496
  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
  3497
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3498
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3499
lemma rel_mset'_imp_rel_mset: "rel_mset' R M N \<Longrightarrow> rel_mset R M N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3500
  by (induct rule: rel_mset'.induct) (auto simp: rel_mset_Zero rel_mset_Plus)
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3501
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3502
lemma rel_mset_size: "rel_mset R M N \<Longrightarrow> size M = size N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3503
  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
  3504
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3505
lemma multiset_induct2[case_names empty addL addR]:
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3506
  assumes empty: "P {#} {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3507
    and addL: "\<And>a M N. P M N \<Longrightarrow> P (add_mset a M) N"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3508
    and addR: "\<And>a M N. P M N \<Longrightarrow> P M (add_mset a N)"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3509
  shows "P M N"
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3510
apply(induct N rule: multiset_induct)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3511
  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
  3512
  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
  3513
done
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3514
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  3515
lemma multiset_induct2_size[consumes 1, case_names empty add]:
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3516
  assumes c: "size M = size N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3517
    and empty: "P {#} {#}"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3518
    and add: "\<And>a b M N a b. P M N \<Longrightarrow> P (add_mset a M) (add_mset b N)"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3519
  shows "P M N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3520
  using c
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3521
proof (induct M arbitrary: N rule: measure_induct_rule[of size])
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3522
  case (less M)
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3523
  show ?case
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3524
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3525
    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
  3526
    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
  3527
  next
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3528
    case False then obtain M1 a where M: "M = add_mset a M1" by (metis multi_nonempty_split)
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3529
    have "N \<noteq> {#}" using False less.prems by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3530
    then obtain N1 b where N: "N = add_mset b N1" by (metis multi_nonempty_split)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  3531
    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
  3532
    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
  3533
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3534
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3535
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3536
lemma msed_map_invL:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3537
  assumes "image_mset f (add_mset a M) = N"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3538
  shows "\<exists>N1. N = add_mset (f a) N1 \<and> image_mset f M = N1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3539
proof -
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3540
  have "f a \<in># N"
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3541
    using assms multiset.set_map[of f "add_mset a M"] by auto
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3542
  then obtain N1 where N: "N = add_mset (f a) N1" using multi_member_split by metis
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3543
  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
  3544
  thus ?thesis using N by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3545
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3546
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3547
lemma msed_map_invR:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3548
  assumes "image_mset f M = add_mset b N"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3549
  shows "\<exists>M1 a. M = add_mset a M1 \<and> f a = b \<and> image_mset f M1 = N"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3550
proof -
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3551
  obtain a where a: "a \<in># M" and fa: "f a = b"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3552
    using multiset.set_map[of f M] unfolding assms
62430
9527ff088c15 more succint formulation of membership for multisets, similar to lists;
haftmann
parents: 62390
diff changeset
  3553
    by (metis image_iff union_single_eq_member)
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3554
  then obtain M1 where M: "M = add_mset a M1" using multi_member_split by metis
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3555
  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
  3556
  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
  3557
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3558
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3559
lemma msed_rel_invL:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3560
  assumes "rel_mset R (add_mset a M) N"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3561
  shows "\<exists>N1 b. N = add_mset b N1 \<and> R a b \<and> rel_mset R M N1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3562
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3563
  obtain K where KM: "image_mset fst K = add_mset a M"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3564
    and KN: "image_mset snd K = N" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3565
    using assms
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3566
    unfolding multiset.rel_compp_Grp Grp_def by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3567
  obtain K1 ab where K: "K = add_mset ab K1" and a: "fst ab = a"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3568
    and K1M: "image_mset fst K1 = M" using msed_map_invR[OF KM] by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3569
  obtain N1 where N: "N = add_mset (snd ab) N1" and K1N1: "image_mset snd K1 = N1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3570
    using msed_map_invL[OF KN[unfolded K]] by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3571
  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
  3572
  have "rel_mset R M N1" using sK K1M K1N1
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3573
    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
  3574
  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
  3575
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3576
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3577
lemma msed_rel_invR:
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3578
  assumes "rel_mset R M (add_mset b N)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3579
  shows "\<exists>M1 a. M = add_mset a M1 \<and> R a b \<and> rel_mset R M1 N"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3580
proof -
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3581
  obtain K where KN: "image_mset snd K = add_mset b N"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3582
    and KM: "image_mset fst K = M" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3583
    using assms
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3584
    unfolding multiset.rel_compp_Grp Grp_def by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3585
  obtain K1 ab where K: "K = add_mset ab K1" and b: "snd ab = b"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3586
    and K1N: "image_mset snd K1 = N" using msed_map_invR[OF KN] by auto
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3587
  obtain M1 where M: "M = add_mset (fst ab) M1" and K1M1: "image_mset fst K1 = M1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3588
    using msed_map_invL[OF KM[unfolded K]] by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3589
  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
  3590
  have "rel_mset R M1 N" using sK K1N K1M1
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3591
    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
  3592
  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
  3593
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3594
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3595
lemma rel_mset_imp_rel_mset':
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3596
  assumes "rel_mset R M N"
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3597
  shows "rel_mset' R M N"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  3598
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
  3599
  case (less M)
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 59815
diff changeset
  3600
  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
  3601
  show ?case
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3602
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3603
    case True hence "N = {#}" using c by simp
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3604
    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
  3605
  next
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3606
    case False then obtain M1 a where M: "M = add_mset a M1" by (metis multi_nonempty_split)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63689
diff changeset
  3607
    obtain N1 b where N: "N = add_mset b N1" and R: "R a b" and ms: "rel_mset R M1 N1"
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3608
      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
  3609
    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
  3610
    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
  3611
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3612
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3613
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3614
lemma rel_mset_rel_mset': "rel_mset R M N = rel_mset' R M N"
60678
17ba2df56dee tuned proofs;
wenzelm
parents: 60613
diff changeset
  3615
  using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto
57966
6fab7e95587d use 'image_mset' as BNF map function
blanchet
parents: 57518
diff changeset
  3616
60613
f11e9fd70b7d fix tex-output for rel_mset
hoelzl
parents: 60608
diff changeset
  3617
text \<open>The main end product for @{const rel_mset}: inductive characterization:\<close>
61337
4645502c3c64 fewer aliases for toplevel theorem statements;
wenzelm
parents: 61188
diff changeset
  3618
lemmas rel_mset_induct[case_names empty add, induct pred: rel_mset] =
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3619
  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
  3620
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3621
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3622
subsection \<open>Size setup\<close>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3623
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3624
lemma multiset_size_o_map:
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3625
  "size_multiset g \<circ> image_mset f = size_multiset (g \<circ> f)"
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3626
apply (rule ext)
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3627
subgoal for x by (induct x) auto
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63795
diff changeset
  3628
done
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3629
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3630
setup \<open>
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3631
  BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset}
62082
614ef6d7a6b6 nicer 'Spec_Rules' for size function
blanchet
parents: 61955
diff changeset
  3632
    @{thm size_multiset_overloaded_def}
60606
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3633
    @{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3634
      size_union}
e5cb9271e339 more symbols;
wenzelm
parents: 60515
diff changeset
  3635
    @{thms multiset_size_o_map}
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60400
diff changeset
  3636
\<close>
56656
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3637
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3638
hide_const (open) wcount
7f9b686bf30a size function for multisets
blanchet
parents: 55945
diff changeset
  3639
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3640
end