src/HOL/Library/Multiset.thy
author paulson <lp15@cam.ac.uk>
Fri, 04 Apr 2014 17:58:25 +0100
changeset 56410 a14831ac3023
parent 55945 e96383acecf9
child 56656 7f9b686bf30a
permissions -rw-r--r--
divide_minus_left divide_minus_right are in field_simps but are not default simprules
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
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     4
*)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     5
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
     6
header {* (Finite) multisets *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     7
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
     8
theory Multiset
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents: 51548
diff changeset
     9
imports Main
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
    10
begin
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    11
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    12
subsection {* The type of multisets *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    13
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    14
definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    15
49834
b27bbb021df1 discontinued obsolete typedef (open) syntax;
wenzelm
parents: 49823
diff changeset
    16
typedef 'a multiset = "multiset :: ('a => nat) set"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    17
  morphisms count Abs_multiset
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    18
  unfolding multiset_def
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    19
proof
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    20
  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
    21
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    22
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    23
setup_lifting type_definition_multiset
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
    24
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
    25
abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
    26
  "a :# M == 0 < count M a"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
    27
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    28
notation (xsymbols)
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    29
  Melem (infix "\<in>#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    30
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    31
lemma multiset_eq_iff:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    32
  "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
    33
  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
    34
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    35
lemma multiset_eqI:
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    36
  "(\<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
    37
  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
    38
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    39
text {*
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
 \medskip Preservation of the representing set @{term 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
    41
*}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    42
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
lemma const0_in_multiset:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    44
  "(\<lambda>a. 0) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    45
  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
    46
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
lemma only1_in_multiset:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    48
  "(\<lambda>b. if b = a then n else 0) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    49
  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
    50
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
lemma union_preserves_multiset:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    52
  "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    53
  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
    54
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    55
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
    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>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
    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. 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
    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
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    65
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
    66
  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
    67
  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
    68
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
    69
  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
    70
    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
    71
  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
    72
    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
    73
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
    74
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    75
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
    76
  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
    77
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    78
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
subsection {* Representing multisets *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    80
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    81
text {* Multiset enumeration *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    82
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    83
instantiation multiset :: (type) cancel_comm_monoid_add
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    84
begin
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    85
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    86
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    87
by (rule const0_in_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    88
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    89
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
    90
  "Mempty \<equiv> 0"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    91
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    92
lift_definition plus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
    93
by (rule union_preserves_multiset)
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    94
48008
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    95
instance
846ff14337a4 use transfer method for instance proof
huffman
parents: 47429
diff changeset
    96
by default (transfer, simp add: fun_eq_iff)+
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    97
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    98
end
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    99
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   100
lift_definition single :: "'a => 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   101
by (rule only1_in_multiset)
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   102
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   103
syntax
26176
038baad81209 tuned syntax declaration;
wenzelm
parents: 26145
diff changeset
   104
  "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
25507
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   105
translations
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   106
  "{#x, xs#}" == "{#x#} + {#xs#}"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   107
  "{#x#}" == "CONST single x"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   108
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   109
lemma count_empty [simp]: "count {#} a = 0"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   110
  by (simp add: zero_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   111
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   112
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   113
  by (simp add: single.rep_eq)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29509
diff changeset
   114
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
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
subsection {* Basic operations *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   117
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   118
subsubsection {* Union *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   119
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   120
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   121
  by (simp add: plus_multiset.rep_eq)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   122
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   123
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   124
subsubsection {* Difference *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   125
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
   126
instantiation multiset :: (type) comm_monoid_diff
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   127
begin
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   128
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   129
lift_definition minus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   130
by (rule diff_preserves_multiset)
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   131
 
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
   132
instance
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
   133
by default (transfer, simp add: fun_eq_iff)+
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   134
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   135
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
   136
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   137
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   138
  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
   139
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   140
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   141
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   142
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   143
lemma diff_cancel[simp]: "A - A = {#}"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   144
  by (fact Groups.diff_cancel)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   145
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   146
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   147
  by (fact add_diff_cancel_right')
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   148
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   149
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   150
  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
   151
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   152
lemma diff_right_commute:
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   153
  "(M::'a multiset) - N - Q = M - Q - N"
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   154
  by (fact diff_right_commute)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   155
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   156
lemma diff_add:
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   157
  "(M::'a multiset) - (N + Q) = M - N - Q"
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   158
  by (rule sym) (fact diff_diff_add)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   159
  
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   160
lemma insert_DiffM:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   161
  "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   162
  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
   163
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   164
lemma insert_DiffM2 [simp]:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   165
  "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   166
  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
   167
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   168
lemma diff_union_swap:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   169
  "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   170
  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
   171
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   172
lemma diff_union_single_conv:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   173
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   174
  by (simp add: multiset_eq_iff)
26143
314c0bcb7df7 Added useful general lemmas from the work with the HeapMonad
bulwahn
parents: 26033
diff changeset
   175
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   176
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   177
subsubsection {* Equality of multisets *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   178
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   179
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   180
  by (simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   181
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   182
lemma single_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
   183
  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
   184
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   185
lemma 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
   186
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   187
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   188
lemma 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
   189
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   190
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   191
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   192
  by (auto simp add: multiset_eq_iff)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   193
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   194
lemma diff_single_trivial:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   195
  "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   196
  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
   197
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   198
lemma diff_single_eq_union:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   199
  "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   200
  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
   201
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   202
lemma union_single_eq_diff:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   203
  "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   204
  by (auto dest: sym)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   205
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   206
lemma union_single_eq_member:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   207
  "M + {#x#} = N \<Longrightarrow> x \<in># N"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   208
  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
   209
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   210
lemma union_is_single:
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   211
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   212
proof
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   213
  assume ?rhs then show ?lhs by auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   214
next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   215
  assume ?lhs then show ?rhs
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   216
    by (simp add: multiset_eq_iff split:if_splits) (metis add_is_1)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   217
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
   218
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   219
lemma single_is_union:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   220
  "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   221
  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
   222
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   223
lemma add_eq_conv_diff:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   224
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"  (is "?lhs = ?rhs")
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44339
diff changeset
   225
(* 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
   226
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
   227
  assume ?rhs then show ?lhs
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   228
  by (auto simp add: add_assoc add_commute [of "{#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
   229
    (drule sym, simp add: add_assoc [symmetric])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   230
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
   231
  assume ?lhs
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   232
  show ?rhs
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   233
  proof (cases "a = 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
   234
    case True with `?lhs` 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
   235
  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
   236
    case False
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   237
    from `?lhs` have "a \<in># N + {#b#}" by (rule union_single_eq_member)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   238
    with False have "a \<in># N" 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
   239
    moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   240
    moreover note False
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   241
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   242
  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
   243
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
   244
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   245
lemma insert_noteq_member: 
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   246
  assumes BC: "B + {#b#} = C + {#c#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   247
   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
   248
  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
   249
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
   250
  have "c \<in># C + {#c#}" by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   251
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   252
  then have "c \<in># B + {#b#}" using BC by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   253
  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
   254
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
   255
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   256
lemma add_eq_conv_ex:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   257
  "(M + {#a#} = N + {#b#}) =
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   258
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   259
  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
   260
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   261
lemma multi_member_split:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   262
  "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   263
  by (rule_tac x = "M - {#x#}" in exI, simp)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   264
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   265
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   266
subsubsection {* Pointwise ordering induced by count *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   267
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   268
instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   269
begin
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   270
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 55467
diff changeset
   271
lift_definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" is "\<lambda> A B. (\<forall>a. A a \<le> B a)" .
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 55467
diff changeset
   272
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   273
lemmas mset_le_def = less_eq_multiset_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
   274
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   275
definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   276
  mset_less_def: "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> 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
   277
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   278
instance
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   279
  by default (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   280
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   281
end
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   282
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   283
lemma mset_less_eqI:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   284
  "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le> B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   285
  by (simp add: mset_le_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
   286
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   287
lemma mset_le_exists_conv:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   288
  "(A::'a multiset) \<le> B \<longleftrightarrow> (\<exists>C. B = A + 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
   289
apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   290
apply (auto intro: multiset_eq_iff [THEN iffD2])
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   291
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
   292
52289
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   293
instance multiset :: (type) ordered_cancel_comm_monoid_diff
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   294
  by default (simp, fact mset_le_exists_conv)
83ce5d2841e7 type class for confined subtraction
haftmann
parents: 51623
diff changeset
   295
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   296
lemma mset_le_mono_add_right_cancel [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   297
  "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   298
  by (fact add_le_cancel_right)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   299
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   300
lemma mset_le_mono_add_left_cancel [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   301
  "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   302
  by (fact add_le_cancel_left)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   303
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   304
lemma mset_le_mono_add:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   305
  "(A::'a multiset) \<le> B \<Longrightarrow> C \<le> D \<Longrightarrow> A + C \<le> B + D"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   306
  by (fact add_mono)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   307
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   308
lemma mset_le_add_left [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   309
  "(A::'a multiset) \<le> A + B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   310
  unfolding mset_le_def by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   311
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   312
lemma mset_le_add_right [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   313
  "B \<le> (A::'a multiset) + B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   314
  unfolding mset_le_def by auto
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   315
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   316
lemma mset_le_single:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   317
  "a :# B \<Longrightarrow> {#a#} \<le> B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   318
  by (simp add: mset_le_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
   319
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   320
lemma multiset_diff_union_assoc:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   321
  "C \<le> B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   322
  by (simp add: multiset_eq_iff mset_le_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
   323
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   324
lemma mset_le_multiset_union_diff_commute:
36867
6c28c702ed22 simplified proof
nipkow
parents: 36635
diff changeset
   325
  "B \<le> A \<Longrightarrow> (A::'a multiset) - 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
   326
by (simp add: multiset_eq_iff mset_le_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
   327
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
   328
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M"
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
   329
by(simp add: mset_le_def)
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
   330
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   331
lemma mset_lessD: "A < B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># 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
   332
apply (clarsimp simp: mset_le_def mset_less_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
   333
apply (erule_tac x=x in allE)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   334
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
   335
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
   336
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   337
lemma mset_leD: "A \<le> B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># 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
   338
apply (clarsimp simp: mset_le_def mset_less_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
   339
apply (erule_tac x = x in allE)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   340
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
   341
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
   342
  
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   343
lemma mset_less_insertD: "(A + {#x#} < B) \<Longrightarrow> (x \<in># B \<and> A < B)"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   344
apply (rule conjI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   345
 apply (simp add: mset_lessD)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   346
apply (clarsimp simp: mset_le_def mset_less_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
   347
apply safe
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   348
 apply (erule_tac x = a in allE)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   349
 apply (auto split: split_if_asm)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   350
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
   351
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   352
lemma mset_le_insertD: "(A + {#x#} \<le> B) \<Longrightarrow> (x \<in># B \<and> A \<le> B)"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   353
apply (rule conjI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   354
 apply (simp add: mset_leD)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   355
apply (force simp: mset_le_def mset_less_def split: split_if_asm)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   356
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
   357
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   358
lemma mset_less_of_empty[simp]: "A < {#} \<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
   359
  by (auto simp add: mset_less_def mset_le_def 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
   360
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   361
lemma empty_le[simp]: "{#} \<le> A"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   362
  unfolding mset_le_exists_conv by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   363
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   364
lemma le_empty[simp]: "(M \<le> {#}) = (M = {#})"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   365
  unfolding mset_le_exists_conv by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   366
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   367
lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   368
  by (auto simp: mset_le_def mset_less_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
   369
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   370
lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   371
  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
   372
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   373
lemma mset_less_add_bothsides:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   374
  "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   375
  by (fact add_less_imp_less_right)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   376
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   377
lemma mset_less_empty_nonempty:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   378
  "{#} < S \<longleftrightarrow> S \<noteq> {#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   379
  by (auto simp: mset_le_def mset_less_def)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   380
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   381
lemma mset_less_diff_self:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   382
  "c \<in># B \<Longrightarrow> B - {#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
   383
  by (auto simp: mset_le_def mset_less_def multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   384
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   385
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   386
subsubsection {* Intersection *}
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   387
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   388
instantiation multiset :: (type) semilattice_inf
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   389
begin
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   390
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   391
definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   392
  multiset_inter_def: "inf_multiset A B = A - (A - B)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   393
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   394
instance
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   395
proof -
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   396
  have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   397
  show "OFCLASS('a multiset, semilattice_inf_class)"
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   398
    by default (auto simp add: multiset_inter_def mset_le_def aux)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   399
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   400
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   401
end
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   402
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   403
abbreviation multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   404
  "multiset_inter \<equiv> inf"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   405
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   406
lemma multiset_inter_count [simp]:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   407
  "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
   408
  by (simp add: multiset_inter_def)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   409
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   410
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   411
  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
   412
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   413
lemma multiset_union_diff_commute:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   414
  assumes "B #\<inter> C = {#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   415
  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
   416
proof (rule multiset_eqI)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   417
  fix x
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   418
  from assms have "min (count B x) (count C x) = 0"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   419
    by (auto simp add: multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   420
  then have "count B x = 0 \<or> count C x = 0"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   421
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   422
  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
   423
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   424
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   425
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   426
lemma empty_inter [simp]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   427
  "{#} #\<inter> M = {#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   428
  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
   429
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   430
lemma inter_empty [simp]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   431
  "M #\<inter> {#} = {#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   432
  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
   433
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   434
lemma inter_add_left1:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   435
  "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   436
  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
   437
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   438
lemma inter_add_left2:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   439
  "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   440
  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
   441
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   442
lemma inter_add_right1:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   443
  "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   444
  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
   445
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   446
lemma inter_add_right2:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   447
  "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   448
  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
   449
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   450
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   451
subsubsection {* Bounded union *}
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   452
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   453
instantiation multiset :: (type) semilattice_sup
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   454
begin
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   455
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   456
definition sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   457
  "sup_multiset A B = A + (B - A)"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   458
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   459
instance
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   460
proof -
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   461
  have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" by arith
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   462
  show "OFCLASS('a multiset, semilattice_sup_class)"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   463
    by default (auto simp add: sup_multiset_def mset_le_def aux)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   464
qed
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   465
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   466
end
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   467
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   468
abbreviation sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<union>" 70) where
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   469
  "sup_multiset \<equiv> sup"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   470
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   471
lemma sup_multiset_count [simp]:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   472
  "count (A #\<union> B) x = max (count A x) (count B x)"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   473
  by (simp add: sup_multiset_def)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   474
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   475
lemma empty_sup [simp]:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   476
  "{#} #\<union> M = M"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   477
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   478
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   479
lemma sup_empty [simp]:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   480
  "M #\<union> {#} = M"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   481
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   482
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   483
lemma sup_add_left1:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   484
  "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   485
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   486
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   487
lemma sup_add_left2:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   488
  "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   489
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   490
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   491
lemma sup_add_right1:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   492
  "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   493
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   494
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   495
lemma sup_add_right2:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   496
  "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   497
  by (simp add: multiset_eq_iff)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   498
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   499
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   500
subsubsection {* Filter (with comprehension syntax) *}
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   501
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   502
text {* Multiset comprehension *}
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   503
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   504
lift_definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   505
by (rule filter_preserves_multiset)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   506
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   507
hide_const (open) filter
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   508
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   509
lemma count_filter [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   510
  "count (Multiset.filter P M) a = (if P a then count M a else 0)"
47429
ec64d94cbf9c multiset operations are defined with lift_definitions;
bulwahn
parents: 47308
diff changeset
   511
  by (simp add: filter.rep_eq)
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   512
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   513
lemma filter_empty [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   514
  "Multiset.filter P {#} = {#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   515
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   516
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   517
lemma filter_single [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   518
  "Multiset.filter P {#x#} = (if P x then {#x#} else {#})"
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   519
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   520
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   521
lemma filter_union [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   522
  "Multiset.filter P (M + N) = Multiset.filter P M + Multiset.filter 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
   523
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   524
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   525
lemma filter_diff [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   526
  "Multiset.filter P (M - N) = Multiset.filter P M - Multiset.filter P N"
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   527
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   528
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   529
lemma filter_inter [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   530
  "Multiset.filter P (M #\<inter> N) = Multiset.filter P M #\<inter> Multiset.filter 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
   531
  by (rule multiset_eqI) simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   532
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   533
syntax
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   534
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   535
syntax (xsymbol)
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   536
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   537
translations
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   538
  "{#x \<in># M. P#}" == "CONST Multiset.filter (\<lambda>x. P) M"
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   539
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   540
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   541
subsubsection {* Set of elements *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   542
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   543
definition set_of :: "'a multiset => 'a set" 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
   544
  "set_of M = {x. x :# 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
   545
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   546
lemma set_of_empty [simp]: "set_of {#} = {}"
26178
nipkow
parents: 26176
diff changeset
   547
by (simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   548
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   549
lemma set_of_single [simp]: "set_of {#b#} = {b}"
26178
nipkow
parents: 26176
diff changeset
   550
by (simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   551
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   552
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
26178
nipkow
parents: 26176
diff changeset
   553
by (auto simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   554
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   555
lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (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
   556
by (auto simp add: set_of_def multiset_eq_iff)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   557
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   558
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
26178
nipkow
parents: 26176
diff changeset
   559
by (auto simp add: set_of_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   560
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   561
lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
26178
nipkow
parents: 26176
diff changeset
   562
by (auto simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   563
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   564
lemma finite_set_of [iff]: "finite (set_of 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
   565
  using count [of M] by (simp add: multiset_def set_of_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
   566
46756
faf62905cd53 adding finiteness of intervals on integer sets; adding another finiteness theorem for multisets
bulwahn
parents: 46730
diff changeset
   567
lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
faf62905cd53 adding finiteness of intervals on integer sets; adding another finiteness theorem for multisets
bulwahn
parents: 46730
diff changeset
   568
  unfolding set_of_def[symmetric] by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   569
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   570
lemma set_of_mono: "A \<le> B \<Longrightarrow> set_of A \<subseteq> set_of B"  
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   571
  by (metis mset_leD subsetI mem_set_of_iff)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
   572
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   573
subsubsection {* Size *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   574
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   575
instantiation multiset :: (type) size
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   576
begin
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   577
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   578
definition size_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
   579
  "size M = setsum (count M) (set_of 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
   580
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   581
instance ..
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   582
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   583
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
   584
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   585
lemma size_empty [simp]: "size {#} = 0"
26178
nipkow
parents: 26176
diff changeset
   586
by (simp add: size_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   587
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   588
lemma size_single [simp]: "size {#b#} = 1"
26178
nipkow
parents: 26176
diff changeset
   589
by (simp add: size_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   590
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   591
lemma setsum_count_Int:
26178
nipkow
parents: 26176
diff changeset
   592
  "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
nipkow
parents: 26176
diff changeset
   593
apply (induct rule: finite_induct)
nipkow
parents: 26176
diff changeset
   594
 apply simp
nipkow
parents: 26176
diff changeset
   595
apply (simp add: Int_insert_left set_of_def)
nipkow
parents: 26176
diff changeset
   596
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   597
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   598
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
26178
nipkow
parents: 26176
diff changeset
   599
apply (unfold size_def)
nipkow
parents: 26176
diff changeset
   600
apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
nipkow
parents: 26176
diff changeset
   601
 prefer 2
nipkow
parents: 26176
diff changeset
   602
 apply (rule ext, simp)
nipkow
parents: 26176
diff changeset
   603
apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
nipkow
parents: 26176
diff changeset
   604
apply (subst Int_commute)
nipkow
parents: 26176
diff changeset
   605
apply (simp (no_asm_simp) add: setsum_count_Int)
nipkow
parents: 26176
diff changeset
   606
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   607
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   608
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (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
   609
by (auto simp add: size_def multiset_eq_iff)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   610
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   611
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
26178
nipkow
parents: 26176
diff changeset
   612
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
   613
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   614
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
26178
nipkow
parents: 26176
diff changeset
   615
apply (unfold size_def)
nipkow
parents: 26176
diff changeset
   616
apply (drule setsum_SucD)
nipkow
parents: 26176
diff changeset
   617
apply auto
nipkow
parents: 26176
diff changeset
   618
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   619
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   620
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
   621
  assumes "size M = Suc n"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   622
  shows "\<exists>a N. M = N + {#a#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   623
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
   624
  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
   625
    by (erule size_eq_Suc_imp_elem [THEN exE])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   626
  then have "M = M - {#a#} + {#a#}" by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   627
  then show ?thesis by blast
23611
65b168646309 more interpretations
nipkow
parents: 23373
diff changeset
   628
qed
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   629
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   630
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   631
subsection {* Induction and case splits *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   632
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
   633
theorem multiset_induct [case_names empty add, induct type: multiset]:
48009
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   634
  assumes empty: "P {#}"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   635
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   636
  shows "P M"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   637
proof (induct n \<equiv> "size M" arbitrary: M)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   638
  case 0 thus "P M" by (simp add: empty)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   639
next
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   640
  case (Suc k)
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   641
  obtain N x where "M = N + {#x#}"
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   642
    using `Suc k = size M` [symmetric]
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   643
    using size_eq_Suc_imp_eq_union by fast
9b9150033b5a shortened some proofs
huffman
parents: 48008
diff changeset
   644
  with Suc add show "P M" by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   645
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   646
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   647
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
26178
nipkow
parents: 26176
diff changeset
   648
by (induct M) auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   649
55913
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   650
lemma multiset_cases [cases type]:
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   651
  obtains (empty) "M = {#}"
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   652
    | (add) N x where "M = N + {#x#}"
c1409c103b77 proper UTF-8;
wenzelm
parents: 55811
diff changeset
   653
  using assms by (induct M) simp_all
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   654
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   655
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
   656
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
   657
26033
278025d5282d modified MCollect syntax
nipkow
parents: 26016
diff changeset
   658
lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   659
apply (subst multiset_eq_iff)
26178
nipkow
parents: 26176
diff changeset
   660
apply auto
nipkow
parents: 26176
diff changeset
   661
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   662
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   663
lemma mset_less_size: "(A::'a multiset) < B \<Longrightarrow> size A < size B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   664
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
   665
  case (empty M)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   666
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   667
  then obtain M' x where "M = 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
   668
    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
   669
  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
   670
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
   671
  case (add S x T)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   672
  have IH: "\<And>B. S < B \<Longrightarrow> size S < size B" by fact
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   673
  have SxsubT: "S + {#x#} < T" by fact
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   674
  then have "x \<in># T" and "S < T" by (auto dest: mset_less_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
   675
  then obtain T' where T: "T = T' + {#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
   676
    by (blast dest: multi_member_split)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   677
  then have "S < T'" using SxsubT 
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   678
    by (blast intro: mset_less_add_bothsides)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   679
  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
   680
  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
   681
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
   682
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   683
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   684
subsubsection {* Strong induction and subset induction for multisets *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   685
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   686
text {* Well-foundedness of proper subset operator: *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   687
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   688
text {* proper multiset 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
   689
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   690
definition
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   691
  mset_less_rel :: "('a multiset * 'a multiset) set" where
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   692
  "mset_less_rel = {(A,B). A < B}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   693
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   694
lemma multiset_add_sub_el_shuffle: 
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   695
  assumes "c \<in># B" and "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
   696
  shows "B - {#c#} + {#b#} = B + {#b#} - {#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
   697
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
   698
  from `c \<in># B` obtain A where B: "B = A + {#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
   699
    by (blast 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
   700
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   701
  then have "A + {#b#} = A + {#c#} + {#b#} - {#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
   702
    by (simp add: add_ac)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   703
  then show ?thesis using B 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
   704
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
   705
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   706
lemma wf_mset_less_rel: "wf mset_less_rel"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   707
apply (unfold mset_less_rel_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
   708
apply (rule wf_measure [THEN wf_subset, where f1=size])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   709
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   710
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
   711
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   712
text {* The induction rules: *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   713
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   714
lemma full_multiset_induct [case_names less]:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   715
assumes ih: "\<And>B. \<forall>(A::'a multiset). A < B \<longrightarrow> P A \<Longrightarrow> P B"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   716
shows "P 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
   717
apply (rule wf_mset_less_rel [THEN wf_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
   718
apply (rule ih, auto simp: mset_less_rel_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
   719
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
   720
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   721
lemma multi_subset_induct [consumes 2, case_names empty add]:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   722
assumes "F \<le> A"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   723
  and empty: "P {#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   724
  and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   725
shows "P F"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   726
proof -
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   727
  from `F \<le> A`
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   728
  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
   729
  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
   730
    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
   731
  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
   732
    fix x F
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   733
    assume P: "F \<le> A \<Longrightarrow> P F" and i: "F + {#x#} \<le> A"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   734
    show "P (F + {#x#})"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   735
    proof (rule insert)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   736
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   737
      from i have "F \<le> A" by (auto dest: mset_le_insertD)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   738
      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
   739
    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
   740
  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
   741
qed
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   742
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   743
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   744
subsection {* The fold combinator *}
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   745
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   746
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   747
where
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   748
  "fold f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_of M)"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   749
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   750
lemma fold_mset_empty [simp]:
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   751
  "fold f s {#} = s"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   752
  by (simp add: fold_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   753
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   754
context comp_fun_commute
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   755
begin
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   756
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   757
lemma fold_mset_insert:
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   758
  "fold f s (M + {#x#}) = f x (fold f s M)"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   759
proof -
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   760
  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
   761
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   762
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   763
    by (fact comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   764
  show ?thesis
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   765
  proof (cases "x \<in> set_of M")
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   766
    case False
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   767
    then have *: "count (M + {#x#}) x = 1" by simp
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   768
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_of M) =
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   769
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_of M)"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   770
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   771
    with False * show ?thesis
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   772
      by (simp add: fold_def del: count_union)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   773
  next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   774
    case True
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   775
    def N \<equiv> "set_of M - {x}"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   776
    from N_def True have *: "set_of M = insert x N" "x \<notin> N" "finite N" by auto
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   777
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   778
      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
   779
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   780
    with * show ?thesis by (simp add: fold_def del: count_union) simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   781
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   782
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   783
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   784
corollary fold_mset_single [simp]:
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   785
  "fold f s {#x#} = f x s"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   786
proof -
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   787
  have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   788
  then show ?thesis by simp
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   789
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   790
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
   791
lemma fold_mset_fun_left_comm:
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   792
  "f x (fold f s M) = fold f (f x s) M"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   793
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   794
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   795
lemma fold_mset_union [simp]:
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   796
  "fold f s (M + N) = fold f (fold f s M) N"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   797
proof (induct M)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   798
  case empty then show ?case by simp
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   799
next
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   800
  case (add M x)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   801
  have "M + {#x#} + N = (M + N) + {#x#}"
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   802
    by (simp add: add_ac)
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
   803
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   804
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   805
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   806
lemma fold_mset_fusion:
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   807
  assumes "comp_fun_commute g"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   808
  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold g w A) = fold f (h w) A" (is "PROP ?P")
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   809
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   810
  interpret comp_fun_commute g by (fact assms)
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   811
  show "PROP ?P" by (induct A) auto
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   812
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   813
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   814
end
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   815
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   816
text {*
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   817
  A note on code generation: When defining some function containing a
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   818
  subterm @{term "fold F"}, code generation is not automatic. When
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   819
  interpreting locale @{text left_commutative} with @{text F}, the
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   820
  would be code thms for @{const fold} become thms like
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   821
  @{term "fold F z {#} = z"} where @{text F} is not a pattern but
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   822
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   823
  constant with its own code thms needs to be introduced for @{text
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   824
  F}. See the image operator below.
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   825
*}
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   826
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   827
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   828
subsection {* Image *}
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   829
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   830
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
   831
  "image_mset f = fold (plus o single o f) {#}"
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   832
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   833
lemma comp_fun_commute_mset_image:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   834
  "comp_fun_commute (plus o single o f)"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   835
proof
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   836
qed (simp add: add_ac fun_eq_iff)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   837
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   838
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   839
  by (simp add: image_mset_def)
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   840
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   841
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   842
proof -
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   843
  interpret comp_fun_commute "plus o single o f"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   844
    by (fact comp_fun_commute_mset_image)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   845
  show ?thesis by (simp add: image_mset_def)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   846
qed
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   847
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   848
lemma image_mset_union [simp]:
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   849
  "image_mset f (M + N) = image_mset f M + image_mset f N"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   850
proof -
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   851
  interpret comp_fun_commute "plus o single o f"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   852
    by (fact comp_fun_commute_mset_image)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   853
  show ?thesis by (induct N) (simp_all add: image_mset_def add_ac)
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   854
qed
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   855
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   856
corollary image_mset_insert:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   857
  "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   858
  by simp
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   859
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   860
lemma set_of_image_mset [simp]:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   861
  "set_of (image_mset f M) = image f (set_of M)"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   862
  by (induct M) simp_all
48040
4caf6cd063be add lemma set_of_image_mset
huffman
parents: 48023
diff changeset
   863
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   864
lemma size_image_mset [simp]:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   865
  "size (image_mset f M) = size M"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   866
  by (induct M) simp_all
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   867
49823
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   868
lemma image_mset_is_empty_iff [simp]:
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   869
  "image_mset f M = {#} \<longleftrightarrow> M = {#}"
1c146fa7701e avoid global interpretation
haftmann
parents: 49822
diff changeset
   870
  by (cases M) auto
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   871
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   872
syntax
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   873
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   874
      ("({#_/. _ :# _#})")
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   875
translations
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   876
  "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   877
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   878
syntax
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   879
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   880
      ("({#_/ | _ :# _./ _#})")
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   881
translations
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   882
  "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   883
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   884
text {*
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   885
  This allows to write not just filters like @{term "{#x:#M. x<c#}"}
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   886
  but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   887
  "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   888
  @{term "{#x+x|x:#M. x<c#}"}.
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   889
*}
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   890
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
   891
functor image_mset: image_mset
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   892
proof -
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   893
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   894
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   895
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   896
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   897
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   898
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   899
  show "image_mset id = id"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   900
  proof
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   901
    fix A
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   902
    show "image_mset id A = id A"
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   903
      by (induct A) simp_all
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   904
  qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   905
qed
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   906
49717
56494eedf493 default simp rule for image under identity
haftmann
parents: 49394
diff changeset
   907
declare image_mset.identity [simp]
56494eedf493 default simp rule for image under identity
haftmann
parents: 49394
diff changeset
   908
48023
6dfe5e774012 reordered sections
huffman
parents: 48012
diff changeset
   909
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
   910
subsection {* Further conversions *}
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   911
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   912
primrec multiset_of :: "'a list \<Rightarrow> '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
   913
  "multiset_of [] = {#}" |
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   914
  "multiset_of (a # x) = multiset_of x + {# a #}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   915
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   916
lemma in_multiset_in_set:
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   917
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   918
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   919
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   920
lemma count_multiset_of:
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   921
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   922
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   923
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   924
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (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
   925
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
   926
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   927
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (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
   928
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
   929
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   930
lemma set_of_multiset_of[simp]: "set_of (multiset_of 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
   931
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
   932
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   933
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of 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
   934
by (induct xs) auto
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   935
48012
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
   936
lemma size_multiset_of [simp]: "size (multiset_of xs) = length xs"
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
   937
  by (induct xs) simp_all
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
   938
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   939
lemma multiset_of_append [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
   940
  "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   941
  by (induct xs arbitrary: ys) (auto simp: add_ac)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   942
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   943
lemma multiset_of_filter:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   944
  "multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   945
  by (induct xs) simp_all
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   946
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   947
lemma multiset_of_rev [simp]:
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   948
  "multiset_of (rev xs) = multiset_of xs"
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   949
  by (induct xs) simp_all
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   950
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   951
lemma surj_multiset_of: "surj multiset_of"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   952
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
   953
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
   954
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
   955
 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
   956
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
   957
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
   958
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
   959
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   960
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   961
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
   962
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   963
lemma distinct_count_atmost_1:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   964
  "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   965
apply (induct x, simp, rule iffI, simp_all)
55417
01fbfb60c33e adapted to 'xxx_{case,rec}' renaming, to new theorem names, and to new variable names in theorems
blanchet
parents: 55129
diff changeset
   966
apply (rename_tac a b)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   967
apply (rule conjI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   968
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   969
apply (erule_tac x = a in allE, simp, clarify)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   970
apply (erule_tac x = aa in allE, simp)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   971
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
   972
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   973
lemma multiset_of_eq_setD:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   974
  "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   975
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   976
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   977
lemma set_eq_iff_multiset_of_eq_distinct:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   978
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   979
    (set x = set y) = (multiset_of x = multiset_of 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
   980
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
   981
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   982
lemma set_eq_iff_multiset_of_remdups_eq:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   983
   "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   984
apply (rule iffI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   985
apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   986
apply (drule distinct_remdups [THEN distinct_remdups
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   987
      [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   988
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
   989
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
   990
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   991
lemma multiset_of_compl_union [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
   992
  "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of 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
   993
  by (induct xs) (auto simp: add_ac)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   994
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   995
lemma count_multiset_of_length_filter:
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   996
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   997
  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
   998
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   999
lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1000
apply (induct ls arbitrary: i)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1001
 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
  1002
apply (case_tac i)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1003
 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
  1004
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
  1005
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
  1006
lemma multiset_of_remove1[simp]:
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
  1007
  "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1008
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
  1009
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1010
lemma multiset_of_eq_length:
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1011
  assumes "multiset_of xs = multiset_of ys"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1012
  shows "length xs = length ys"
48012
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
  1013
  using assms by (metis size_multiset_of)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1014
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1015
lemma multiset_of_eq_length_filter:
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1016
  assumes "multiset_of xs = multiset_of ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1017
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
48012
b6e5e86a7303 shortened yet more multiset proofs;
huffman
parents: 48011
diff changeset
  1018
  using assms by (metis count_multiset_of)
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1019
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1020
lemma fold_multiset_equiv:
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1021
  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"
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1022
    and equiv: "multiset_of xs = multiset_of ys"
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1023
  shows "List.fold f xs = List.fold f ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1024
using f equiv [symmetric]
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1025
proof (induct xs arbitrary: ys)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1026
  case Nil then show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1027
next
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1028
  case (Cons x xs)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1029
  then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1030
  have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" 
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1031
    by (rule Cons.prems(1)) (simp_all add: *)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1032
  moreover from * have "x \<in> set ys" by simp
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1033
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  1034
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1035
  ultimately show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1036
qed
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
  1037
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1038
lemma multiset_of_insort [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1039
  "multiset_of (insort x xs) = multiset_of xs + {#x#}"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1040
  by (induct xs) (simp_all add: ac_simps)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1041
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1042
lemma in_multiset_of:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1043
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1044
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1045
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1046
lemma multiset_of_map:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1047
  "multiset_of (map f xs) = image_mset f (multiset_of xs)"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1048
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1049
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1050
definition multiset_of_set :: "'a set \<Rightarrow> 'a multiset"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1051
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1052
  "multiset_of_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1053
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1054
interpretation multiset_of_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1055
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1056
  "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1057
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1058
  interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1059
  show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1060
  from multiset_of_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1061
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1062
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1063
lemma count_multiset_of_set [simp]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1064
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (multiset_of_set A) x = 1" (is "PROP ?P")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1065
  "\<not> finite A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?Q")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1066
  "x \<notin> A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?R")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1067
proof -
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1068
  { fix A
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1069
    assume "x \<notin> A"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1070
    have "count (multiset_of_set A) x = 0"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1071
    proof (cases "finite A")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1072
      case False then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1073
    next
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1074
      case True from True `x \<notin> A` show ?thesis by (induct A) auto
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1075
    qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1076
  } note * = this
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1077
  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
  1078
  by (auto elim!: Set.set_insert)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1079
qed -- {* TODO: maybe define @{const multiset_of_set} also in terms of @{const Abs_multiset} *}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1080
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1081
context linorder
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1082
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1083
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1084
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1085
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1086
  "sorted_list_of_multiset M = fold insort [] M"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1087
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1088
lemma sorted_list_of_multiset_empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1089
  "sorted_list_of_multiset {#} = []"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1090
  by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1091
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1092
lemma sorted_list_of_multiset_singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1093
  "sorted_list_of_multiset {#x#} = [x]"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1094
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1095
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1096
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1097
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1098
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1099
lemma sorted_list_of_multiset_insert [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1100
  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1101
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1102
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1103
  show ?thesis by (simp add: sorted_list_of_multiset_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1104
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1105
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1106
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1107
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1108
lemma multiset_of_sorted_list_of_multiset [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1109
  "multiset_of (sorted_list_of_multiset M) = M"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1110
  by (induct M) simp_all
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1111
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1112
lemma sorted_list_of_multiset_multiset_of [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1113
  "sorted_list_of_multiset (multiset_of xs) = sort xs"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1114
  by (induct xs) simp_all
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1115
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1116
lemma finite_set_of_multiset_of_set:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1117
  assumes "finite A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1118
  shows "set_of (multiset_of_set A) = A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1119
  using assms by (induct A) simp_all
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1120
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1121
lemma infinite_set_of_multiset_of_set:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1122
  assumes "\<not> finite A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1123
  shows "set_of (multiset_of_set A) = {}"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1124
  using assms by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1125
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1126
lemma set_sorted_list_of_multiset [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1127
  "set (sorted_list_of_multiset M) = set_of M"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1128
  by (induct M) (simp_all add: set_insort)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1129
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1130
lemma sorted_list_of_multiset_of_set [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1131
  "sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1132
  by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1133
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1134
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1135
subsection {* Big operators *}
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1136
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1137
no_notation times (infixl "*" 70)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1138
no_notation Groups.one ("1")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1139
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1140
locale comm_monoid_mset = comm_monoid
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1141
begin
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1142
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1143
definition F :: "'a multiset \<Rightarrow> 'a"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1144
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1145
  eq_fold: "F M = Multiset.fold f 1 M"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1146
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1147
lemma empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1148
  "F {#} = 1"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1149
  by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1150
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1151
lemma singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1152
  "F {#x#} = x"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1153
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1154
  interpret comp_fun_commute
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1155
    by default (simp add: fun_eq_iff left_commute)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1156
  show ?thesis by (simp add: eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1157
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1158
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1159
lemma union [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1160
  "F (M + N) = F M * F N"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1161
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1162
  interpret comp_fun_commute f
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1163
    by default (simp add: fun_eq_iff left_commute)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1164
  show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1165
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1166
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1167
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1168
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1169
notation times (infixl "*" 70)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1170
notation Groups.one ("1")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1171
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1172
context comm_monoid_add
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1173
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1174
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1175
definition msetsum :: "'a multiset \<Rightarrow> 'a"
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1176
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1177
  "msetsum = comm_monoid_mset.F plus 0"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1178
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1179
sublocale msetsum!: comm_monoid_mset plus 0
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1180
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1181
  "comm_monoid_mset.F plus 0 = msetsum"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1182
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1183
  show "comm_monoid_mset plus 0" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1184
  from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1185
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1186
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1187
lemma setsum_unfold_msetsum:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1188
  "setsum f A = msetsum (image_mset f (multiset_of_set A))"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1189
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1190
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1191
abbreviation msetsum_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1192
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1193
  "msetsum_image f M \<equiv> msetsum (image_mset f M)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1194
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1195
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1196
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1197
syntax
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1198
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1199
      ("(3SUM _:#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1200
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1201
syntax (xsymbols)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1202
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1203
      ("(3\<Sum>_:#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1204
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1205
syntax (HTML output)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1206
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1207
      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1208
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1209
translations
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1210
  "SUM i :# A. b" == "CONST msetsum_image (\<lambda>i. b) A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1211
54868
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1212
context comm_monoid_mult
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1213
begin
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1214
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1215
definition msetprod :: "'a multiset \<Rightarrow> 'a"
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1216
where
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1217
  "msetprod = comm_monoid_mset.F times 1"
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1218
bab6cade3cc5 prefer target-style syntaxx for sublocale
haftmann
parents: 54295
diff changeset
  1219
sublocale msetprod!: comm_monoid_mset times 1
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1220
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1221
  "comm_monoid_mset.F times 1 = msetprod"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1222
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1223
  show "comm_monoid_mset times 1" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1224
  from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1225
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1226
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1227
lemma msetprod_empty:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1228
  "msetprod {#} = 1"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1229
  by (fact msetprod.empty)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1230
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1231
lemma msetprod_singleton:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1232
  "msetprod {#x#} = x"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1233
  by (fact msetprod.singleton)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1234
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1235
lemma msetprod_Un:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1236
  "msetprod (A + B) = msetprod A * msetprod B" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1237
  by (fact msetprod.union)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1238
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1239
lemma setprod_unfold_msetprod:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1240
  "setprod f A = msetprod (image_mset f (multiset_of_set A))"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1241
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1242
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1243
lemma msetprod_multiplicity:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1244
  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_of M)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1245
  by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1246
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1247
abbreviation msetprod_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1248
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1249
  "msetprod_image f M \<equiv> msetprod (image_mset f M)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1250
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1251
end
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1252
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1253
syntax
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1254
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1255
      ("(3PROD _:#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1256
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1257
syntax (xsymbols)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1258
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1259
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1260
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1261
syntax (HTML output)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1262
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1263
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1264
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1265
translations
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1266
  "PROD i :# A. b" == "CONST msetprod_image (\<lambda>i. b) A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1267
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1268
lemma (in comm_semiring_1) dvd_msetprod:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1269
  assumes "x \<in># A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1270
  shows "x dvd msetprod A"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1271
proof -
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1272
  from assms have "A = (A - {#x#}) + {#x#}" by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1273
  then obtain B where "A = B + {#x#}" ..
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1274
  then show ?thesis by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1275
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1276
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1277
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1278
subsection {* Cardinality *}
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1279
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1280
definition mcard :: "'a multiset \<Rightarrow> nat"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1281
where
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1282
  "mcard = msetsum \<circ> image_mset (\<lambda>_. 1)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1283
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1284
lemma mcard_empty [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1285
  "mcard {#} = 0"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1286
  by (simp add: mcard_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1287
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1288
lemma mcard_singleton [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1289
  "mcard {#a#} = Suc 0"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1290
  by (simp add: mcard_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1291
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1292
lemma mcard_plus [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1293
  "mcard (M + N) = mcard M + mcard N"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1294
  by (simp add: mcard_def)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1295
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1296
lemma mcard_empty_iff [simp]:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1297
  "mcard M = 0 \<longleftrightarrow> M = {#}"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1298
  by (induct M) simp_all
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1299
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1300
lemma mcard_unfold_setsum:
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1301
  "mcard M = setsum (count M) (set_of M)"
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1302
proof (induct M)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1303
  case empty then show ?case by simp
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1304
next
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1305
  case (add M x) then show ?case
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1306
    by (cases "x \<in> set_of M")
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1307
      (simp_all del: mem_set_of_iff add: setsum.distrib setsum.delta' insert_absorb, simp)
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1308
qed
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1309
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1310
lemma size_eq_mcard:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1311
  "size = mcard"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1312
  by (simp add: fun_eq_iff size_def mcard_unfold_setsum)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1313
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1314
lemma mcard_multiset_of:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1315
  "mcard (multiset_of xs) = length xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1316
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  1317
51548
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1318
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1319
subsection {* Alternative representations *}
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1320
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1321
subsubsection {* Lists *}
757fa47af981 centralized various multiset operations in theory multiset;
haftmann
parents: 51161
diff changeset
  1322
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1323
context linorder
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1324
begin
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1325
40210
aee7ef725330 sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents: 39533
diff changeset
  1326
lemma multiset_of_insort [simp]:
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1327
  "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1328
  by (induct xs) (simp_all add: ac_simps)
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1329
 
40210
aee7ef725330 sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents: 39533
diff changeset
  1330
lemma multiset_of_sort [simp]:
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1331
  "multiset_of (sort_key k xs) = multiset_of xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1332
  by (induct xs) (simp_all add: ac_simps)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
  1333
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1334
text {*
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1335
  This lemma shows which properties suffice to show that a function
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1336
  @{text "f"} with @{text "f xs = ys"} behaves like sort.
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1337
*}
37074
322d065ebef7 localized properties_for_sort
haftmann
parents: 36903
diff changeset
  1338
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1339
lemma properties_for_sort_key:
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1340
  assumes "multiset_of ys = multiset_of xs"
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1341
  and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1342
  and "sorted (map f ys)"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1343
  shows "sort_key f xs = ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1344
using assms
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1345
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
  1346
  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
  1347
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
  1348
  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
  1349
  from Cons.prems(2) have
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1350
    "\<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
  1351
    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
  1352
  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
  1353
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1354
  moreover from Cons.prems have "x \<in> set ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1355
    by (auto simp add: mem_set_multiset_eq intro!: ccontr)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1356
  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
  1357
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
  1358
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1359
lemma properties_for_sort:
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1360
  assumes multiset: "multiset_of ys = multiset_of xs"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1361
  and "sorted ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1362
  shows "sort xs = ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1363
proof (rule properties_for_sort_key)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1364
  from multiset show "multiset_of ys = multiset_of xs" .
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1365
  from `sorted ys` show "sorted (map (\<lambda>x. x) ys)" by simp
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1366
  from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1367
    by (rule multiset_of_eq_length_filter)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1368
  then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1369
    by simp
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1370
  then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs"
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1371
    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
  1372
qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1373
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1374
lemma sort_key_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1375
  "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
  1376
    @ [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
  1377
    @ 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
  1378
proof (rule properties_for_sort_key)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1379
  show "multiset_of ?rhs = multiset_of ?lhs"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1380
    by (rule multiset_eqI) (auto simp add: multiset_of_filter)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1381
next
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1382
  show "sorted (map f ?rhs)"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1383
    by (auto simp add: sorted_append intro: sorted_map_same)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1384
next
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1385
  fix l
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1386
  assume "l \<in> set ?rhs"
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1387
  let ?pivot = "f (xs ! (length xs div 2))"
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1388
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1389
  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
  1390
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1391
  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
  1392
  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
  1393
  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
  1394
    [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
  1395
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1396
  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
  1397
  proof (cases "f l" ?pivot rule: linorder_cases)
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1398
    case less
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1399
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1400
    with less show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1401
      by (simp add: filter_sort [symmetric] ** ***)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1402
  next
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1403
    case equal then show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1404
      by (simp add: * less_le)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
  1405
  next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1406
    case greater
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1407
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1408
    with greater show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
  1409
      by (simp add: filter_sort [symmetric] ** ***)
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
  1410
  qed
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1411
qed
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1412
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1413
lemma sort_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1414
  "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
  1415
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
  1416
    @ 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
  1417
  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
  1418
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1419
text {* A stable parametrized quicksort *}
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1420
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1421
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
  1422
  "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
  1423
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1424
lemma part_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1425
  "part f pivot [] = ([], [], [])"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1426
  "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
  1427
     if x' < pivot then (x # lts, eqs, gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1428
     else if x' > pivot then (lts, eqs, x # gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1429
     else (lts, x # eqs, gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1430
  by (auto simp add: part_def Let_def split_def)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1431
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1432
lemma sort_key_by_quicksort_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1433
  "sort_key f xs = (case xs of [] \<Rightarrow> []
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1434
    | [x] \<Rightarrow> xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1435
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1436
    | _ \<Rightarrow> (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1437
       in sort_key f lts @ eqs @ sort_key f gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1438
proof (cases xs)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1439
  case Nil then show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1440
next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1441
  case (Cons _ ys) note hyps = Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1442
  proof (cases ys)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1443
    case Nil with hyps show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1444
  next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1445
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1446
    proof (cases zs)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1447
      case Nil with hyps show ?thesis by auto
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1448
    next
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1449
      case Cons 
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1450
      from sort_key_by_quicksort [of f xs]
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1451
      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
  1452
        in sort_key f lts @ eqs @ sort_key f gts)"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1453
      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
  1454
      with hyps Cons show ?thesis by (simp only: list.cases)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1455
    qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1456
  qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1457
qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1458
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1459
end
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1460
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1461
hide_const (open) part
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1462
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1463
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1464
  by (induct xs) (auto intro: 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
  1465
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1466
lemma multiset_of_update:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1467
  "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
  1469
  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
  1470
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
  1471
  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
  1472
  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
  1473
  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
  1474
    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
  1475
  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
  1476
    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
  1477
    with Cons show ?thesis
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1478
      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
  1479
      apply (subst add_assoc)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
      apply (subst add_commute [of "{#v#}" "{#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
  1481
      apply (subst add_assoc [symmetric])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
      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
  1483
      apply (rule mset_le_multiset_union_diff_commute)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1484
      apply (simp add: mset_le_single nth_mem_multiset_of)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1485
      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
  1486
  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
  1487
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
  1488
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1489
lemma multiset_of_swap:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1490
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1491
    multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1492
  by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1494
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
subsection {* The multiset order *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1496
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1497
subsubsection {* Well-foundedness *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1498
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1499
definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1500
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1501
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1502
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1503
definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1504
  "mult r = (mult1 r)\<^sup>+"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1505
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1506
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1507
by (simp add: mult1_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1508
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1509
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1510
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1511
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
19582
a669c98b9c24 get rid of 'concl is';
wenzelm
parents: 19564
diff changeset
  1512
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1513
proof (unfold mult1_def)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1514
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1515
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1516
  let ?case1 = "?case1 {(N, M). ?R N M}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1517
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1518
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1519
  then have "\<exists>a' M0' K.
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1520
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1521
  then show "?case1 \<or> ?case2"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1522
  proof (elim exE conjE)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1523
    fix a' M0' K
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1524
    assume N: "N = M0' + K" and r: "?r K a'"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1525
    assume "M0 + {#a#} = M0' + {#a'#}"
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1526
    then have "M0 = M0' \<and> a = a' \<or>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1527
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1528
      by (simp only: add_eq_conv_ex)
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1529
    then show ?thesis
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1530
    proof (elim disjE conjE exE)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1531
      assume "M0 = M0'" "a = a'"
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1532
      with N r have "?r K a \<and> N = M0 + K" by simp
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1533
      then have ?case2 .. then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1534
    next
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1535
      fix K'
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1536
      assume "M0' = K' + {#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
  1537
      with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1538
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1539
      assume "M0 = K' + {#a'#}"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1540
      with r have "?R (K' + K) M0" by blast
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1541
      with n have ?case1 by simp then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1542
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1543
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1544
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1545
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 52289
diff changeset
  1546
lemma all_accessible: "wf r ==> \<forall>M. M \<in> Wellfounded.acc (mult1 r)"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1547
proof
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1548
  let ?R = "mult1 r"
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 52289
diff changeset
  1549
  let ?W = "Wellfounded.acc ?R"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1550
  {
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1551
    fix M M0 a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1552
    assume M0: "M0 \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1553
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1554
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1555
    have "M0 + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1556
    proof (rule accI [of "M0 + {#a#}"])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1557
      fix N
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1558
      assume "(N, M0 + {#a#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1559
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1560
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1561
        by (rule less_add)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1562
      then show "N \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1563
      proof (elim exE disjE conjE)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1564
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1565
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1566
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1567
        then show "N \<in> ?W" by (simp only: N)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1568
      next
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1569
        fix K
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1570
        assume N: "N = M0 + K"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1571
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1572
        then have "M0 + K \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1573
        proof (induct K)
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1574
          case empty
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1575
          from M0 show "M0 + {#} \<in> ?W" by simp
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1576
        next
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1577
          case (add K x)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1578
          from add.prems have "(x, a) \<in> r" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1579
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1580
          moreover from add have "M0 + K \<in> ?W" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1581
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1582
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1583
        qed
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1584
        then show "N \<in> ?W" by (simp only: N)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1585
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1586
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1587
  } note tedious_reasoning = this
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1588
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1589
  assume wf: "wf r"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1590
  fix M
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1591
  show "M \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1592
  proof (induct M)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1593
    show "{#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1594
    proof (rule accI)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1595
      fix b assume "(b, {#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1596
      with not_less_empty show "b \<in> ?W" by contradiction
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1597
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1598
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1599
    fix M a assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1600
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1601
    proof induct
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1602
      fix a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1603
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1604
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1605
      proof
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1606
        fix M assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1607
        then show "M + {#a#} \<in> ?W"
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 23281
diff changeset
  1608
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1609
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1610
    qed
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1611
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1612
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1613
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1614
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1615
theorem wf_mult1: "wf r ==> wf (mult1 r)"
26178
nipkow
parents: 26176
diff changeset
  1616
by (rule acc_wfI) (rule all_accessible)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1617
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1618
theorem wf_mult: "wf r ==> wf (mult r)"
26178
nipkow
parents: 26176
diff changeset
  1619
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1620
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1621
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1622
subsubsection {* Closure-free presentation *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1623
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1624
text {* One direction. *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1625
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1626
lemma mult_implies_one_step:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1627
  "trans r ==> (M, N) \<in> mult r ==>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1628
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1629
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
26178
nipkow
parents: 26176
diff changeset
  1630
apply (unfold mult_def mult1_def set_of_def)
nipkow
parents: 26176
diff changeset
  1631
apply (erule converse_trancl_induct, clarify)
nipkow
parents: 26176
diff changeset
  1632
 apply (rule_tac x = M0 in exI, simp, clarify)
nipkow
parents: 26176
diff changeset
  1633
apply (case_tac "a :# K")
nipkow
parents: 26176
diff changeset
  1634
 apply (rule_tac x = I in exI)
nipkow
parents: 26176
diff changeset
  1635
 apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  1636
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1637
 apply (simp (no_asm_simp) add: add_assoc [symmetric])
26178
nipkow
parents: 26176
diff changeset
  1638
 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
nipkow
parents: 26176
diff changeset
  1639
 apply (simp add: diff_union_single_conv)
nipkow
parents: 26176
diff changeset
  1640
 apply (simp (no_asm_use) add: trans_def)
nipkow
parents: 26176
diff changeset
  1641
 apply blast
nipkow
parents: 26176
diff changeset
  1642
apply (subgoal_tac "a :# I")
nipkow
parents: 26176
diff changeset
  1643
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  1644
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  1645
 apply (rule_tac x = "K + Ka" in exI)
nipkow
parents: 26176
diff changeset
  1646
 apply (rule conjI)
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1647
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  1648
 apply (rule conjI)
nipkow
parents: 26176
diff changeset
  1649
  apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1650
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  1651
 apply (simp (no_asm_use) add: trans_def)
nipkow
parents: 26176
diff changeset
  1652
 apply blast
nipkow
parents: 26176
diff changeset
  1653
apply (subgoal_tac "a :# (M0 + {#a#})")
nipkow
parents: 26176
diff changeset
  1654
 apply simp
nipkow
parents: 26176
diff changeset
  1655
apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  1656
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1657
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1658
lemma one_step_implies_mult_aux:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1659
  "trans r ==>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1660
    \<forall>I J K. (size J = n \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r))
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1661
      --> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  1662
apply (induct_tac n, auto)
nipkow
parents: 26176
diff changeset
  1663
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow
parents: 26176
diff changeset
  1664
apply (rename_tac "J'", simp)
nipkow
parents: 26176
diff changeset
  1665
apply (erule notE, auto)
nipkow
parents: 26176
diff changeset
  1666
apply (case_tac "J' = {#}")
nipkow
parents: 26176
diff changeset
  1667
 apply (simp add: mult_def)
nipkow
parents: 26176
diff changeset
  1668
 apply (rule r_into_trancl)
nipkow
parents: 26176
diff changeset
  1669
 apply (simp add: mult1_def set_of_def, blast)
nipkow
parents: 26176
diff changeset
  1670
txt {* Now we know @{term "J' \<noteq> {#}"}. *}
nipkow
parents: 26176
diff changeset
  1671
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow
parents: 26176
diff changeset
  1672
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
nipkow
parents: 26176
diff changeset
  1673
apply (erule ssubst)
nipkow
parents: 26176
diff changeset
  1674
apply (simp add: Ball_def, auto)
nipkow
parents: 26176
diff changeset
  1675
apply (subgoal_tac
nipkow
parents: 26176
diff changeset
  1676
  "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
nipkow
parents: 26176
diff changeset
  1677
    (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow
parents: 26176
diff changeset
  1678
 prefer 2
nipkow
parents: 26176
diff changeset
  1679
 apply force
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1680
apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
26178
nipkow
parents: 26176
diff changeset
  1681
apply (erule trancl_trans)
nipkow
parents: 26176
diff changeset
  1682
apply (rule r_into_trancl)
nipkow
parents: 26176
diff changeset
  1683
apply (simp add: mult1_def set_of_def)
nipkow
parents: 26176
diff changeset
  1684
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  1685
apply (rule_tac x = "I + J'" in exI)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1686
apply (simp add: add_ac)
26178
nipkow
parents: 26176
diff changeset
  1687
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1688
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1689
lemma one_step_implies_mult:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1690
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1691
    ==> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  1692
using one_step_implies_mult_aux by blast
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1693
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1694
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1695
subsubsection {* Partial-order properties *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1696
35273
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1697
definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1698
  "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1699
35273
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1700
definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1701
  "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1702
35308
359e0fd38a92 mind the "s"
haftmann
parents: 35273
diff changeset
  1703
notation (xsymbols) less_multiset (infix "\<subset>#" 50)
359e0fd38a92 mind the "s"
haftmann
parents: 35273
diff changeset
  1704
notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1705
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1706
interpretation multiset_order: order le_multiset less_multiset
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1707
proof -
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1708
  have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1709
  proof
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1710
    fix M :: "'a multiset"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1711
    assume "M \<subset># M"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1712
    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
  1713
    have "trans {(x'::'a, x). x' < x}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1714
      by (rule transI) simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1715
    moreover note MM
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1716
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1717
      \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1718
      by (rule mult_implies_one_step)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1719
    then obtain I J K where "M = I + J" and "M = I + K"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1720
      and "J \<noteq> {#}" and "(\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})" by blast
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1721
    then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_of K. \<exists>j\<in>set_of K. k < j" by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1722
    have "finite (set_of K)" by simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1723
    moreover note aux2
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1724
    ultimately have "set_of K = {}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1725
      by (induct rule: finite_induct) (auto intro: order_less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1726
    with aux1 show False by simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1727
  qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1728
  have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1729
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1730
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1731
    by default (auto simp add: le_multiset_def irrefl dest: trans)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1732
qed
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1733
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1734
lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R"
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1735
  by simp
26567
7bcebb8c2d33 instantiation replacing primitive instance plus overloaded defs; more conservative type arities
haftmann
parents: 26178
diff changeset
  1736
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1737
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1738
subsubsection {* Monotonicity of multiset union *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1739
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1740
lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1741
apply (unfold mult1_def)
nipkow
parents: 26176
diff changeset
  1742
apply auto
nipkow
parents: 26176
diff changeset
  1743
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  1744
apply (rule_tac x = "C + M0" in exI)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1745
apply (simp add: add_assoc)
26178
nipkow
parents: 26176
diff changeset
  1746
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1747
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1748
lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
26178
nipkow
parents: 26176
diff changeset
  1749
apply (unfold less_multiset_def mult_def)
nipkow
parents: 26176
diff changeset
  1750
apply (erule trancl_induct)
40249
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  1751
 apply (blast intro: mult1_union)
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  1752
apply (blast intro: mult1_union trancl_trans)
26178
nipkow
parents: 26176
diff changeset
  1753
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1754
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1755
lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order 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
  1756
apply (subst add_commute [of B 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
  1757
apply (subst add_commute [of D C])
26178
nipkow
parents: 26176
diff changeset
  1758
apply (erule union_less_mono2)
nipkow
parents: 26176
diff changeset
  1759
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1760
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1761
lemma union_less_mono:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1762
  "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1763
  by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1764
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1765
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1766
proof
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1767
qed (auto simp add: le_multiset_def intro: union_less_mono2)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1768
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
  1769
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1770
subsection {* Termination proofs with multiset orders *}
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1771
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1772
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
  1773
  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
  1774
  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
  1775
  by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1776
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1777
definition "ms_strict = mult pair_less"
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1778
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
  1779
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1780
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
  1781
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
  1782
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
  1783
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1784
lemma smsI:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1785
  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1786
  unfolding ms_strict_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1787
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
  1788
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1789
lemma wmsI:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1790
  "(set_of A, set_of B) \<in> max_strict \<or> A = {#} \<and> B = {#}
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1791
  \<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
  1792
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
  1793
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
  1794
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1795
inductive pw_leq
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1796
where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1797
  pw_leq_empty: "pw_leq {#} {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1798
| 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
  1799
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1800
lemma pw_leq_lstep:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1801
  "(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
  1802
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
  1803
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1804
lemma pw_leq_split:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1805
  assumes "pw_leq X Y"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1806
  shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1807
  using assms
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1808
proof (induct)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1809
  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
  1810
next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1811
  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
  1812
  then obtain A B Z where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1813
    [simp]: "X = A + Z" "Y = B + Z" 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1814
      and 1[simp]: "(set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#})" 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1815
    by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1816
  from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less" 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1817
    unfolding pair_leq_def by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1818
  thus ?case
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1819
  proof
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1820
    assume [simp]: "x = y"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1821
    have
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1822
      "{#x#} + X = A + ({#y#}+Z) 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1823
      \<and> {#y#} + Y = B + ({#y#}+Z)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1824
      \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1825
      by (auto simp: add_ac)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1826
    thus ?case by (intro exI)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1827
  next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1828
    assume A: "(x, y) \<in> pair_less"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1829
    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
  1830
    have "{#x#} + X = ?A' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1831
      "{#y#} + Y = ?B' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1832
      by (auto simp add: add_ac)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1833
    moreover have 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1834
      "(set_of ?A', set_of ?B') \<in> max_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1835
      using 1 A unfolding max_strict_def 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1836
      by (auto elim!: max_ext.cases)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1837
    ultimately show ?thesis by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1838
  qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1839
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1840
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1841
lemma 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1842
  assumes pwleq: "pw_leq Z Z'"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1843
  shows ms_strictI: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1844
  and   ms_weakI1:  "(set_of A, set_of B) \<in> max_strict \<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
  1845
  and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1846
proof -
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1847
  from pw_leq_split[OF pwleq] 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1848
  obtain A' B' Z''
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1849
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1850
    and mx_or_empty: "(set_of A', set_of B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1851
    by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1852
  {
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1853
    assume max: "(set_of A, set_of B) \<in> max_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1854
    from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1855
    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
  1856
    proof
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1857
      assume max': "(set_of A', set_of B') \<in> max_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1858
      with max have "(set_of (A + A'), set_of (B + B')) \<in> max_strict"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1859
        by (auto simp: max_strict_def intro: max_ext_additive)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1860
      thus ?thesis by (rule smsI) 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1861
    next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1862
      assume [simp]: "A' = {#} \<and> B' = {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1863
      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
  1864
    qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1865
    thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add:add_ac)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1866
    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
  1867
  }
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1868
  from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1869
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1870
  thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:add_ac)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1871
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1872
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  1873
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
  1874
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1875
and nonempty_single: "{# x #} \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1876
by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1877
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1878
setup {*
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1879
let
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1880
  fun msetT T = Type (@{type_name multiset}, [T]);
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1881
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1882
  fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1883
    | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1884
    | mk_mset T (x :: xs) =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1885
          Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1886
                mk_mset T [x] $ mk_mset T xs
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1887
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1888
  fun mset_member_tac m i =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1889
      (if m <= 0 then
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1890
           rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1891
       else
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1892
           rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1893
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1894
  val mset_nonempty_tac =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1895
      rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1896
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1897
  val regroup_munion_conv =
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1898
      Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  1899
        (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1900
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1901
  fun unfold_pwleq_tac i =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1902
    (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1903
      ORELSE (rtac @{thm pw_leq_lstep} i)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1904
      ORELSE (rtac @{thm pw_leq_empty} i)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1905
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1906
  val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1907
                      @{thm Un_insert_left}, @{thm Un_empty_left}]
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1908
in
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1909
  ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1910
  {
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1911
    msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1912
    mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1913
    mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
30595
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30428
diff changeset
  1914
    smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
c87a3350f5a9 proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
wenzelm
parents: 30428
diff changeset
  1915
    reduction_pair= @{thm ms_reduction_pair}
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1916
  })
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1917
end
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1918
*}
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1919
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1920
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1921
subsection {* Legacy theorem bindings *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1922
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1923
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
  1924
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1925
lemma union_commute: "M + N = N + (M::'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
  1926
  by (fact add_commute)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1927
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1928
lemma union_assoc: "(M + N) + K = M + (N + (K::'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
  1929
  by (fact add_assoc)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1930
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1931
lemma union_lcomm: "M + (N + K) = N + (M + (K::'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
  1932
  by (fact add_left_commute)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1933
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1934
lemmas union_ac = union_assoc union_commute union_lcomm
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1935
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1936
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
  1937
  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
  1938
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1939
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
  1940
  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
  1941
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1942
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1943
  by (fact add_imp_eq)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1944
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1945
lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1946
  by (fact order_less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1947
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1948
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1949
  by (fact inf.commute)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1950
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1951
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1952
  by (fact inf.assoc [symmetric])
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1953
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1954
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1955
  by (fact inf.left_commute)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1956
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1957
lemmas multiset_inter_ac =
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1958
  multiset_inter_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1959
  multiset_inter_assoc
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1960
  multiset_inter_left_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1961
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1962
lemma mult_less_not_refl:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1963
  "\<not> M \<subset># (M::'a::order multiset)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1964
  by (fact multiset_order.less_irrefl)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1965
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1966
lemma mult_less_trans:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1967
  "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1968
  by (fact multiset_order.less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1969
    
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1970
lemma mult_less_not_sym:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1971
  "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1972
  by (fact multiset_order.less_not_sym)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1973
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1974
lemma mult_less_asym:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1975
  "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1976
  by (fact multiset_order.less_asym)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1977
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1978
ML {*
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1979
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1980
                      (Const _ $ t') =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1981
    let
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1982
      val (maybe_opt, ps) =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1983
        Nitpick_Model.dest_plain_fun t' ||> op ~~
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1984
        ||> map (apsnd (snd o HOLogic.dest_number))
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1985
      fun elems_for t =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1986
        case AList.lookup (op =) ps t of
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1987
          SOME n => replicate n t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1988
        | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1989
    in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1990
      case maps elems_for (all_values elem_T) @
37261
8a89fd40ed0b honor xsymbols in Nitpick
blanchet
parents: 37169
diff changeset
  1991
           (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
8a89fd40ed0b honor xsymbols in Nitpick
blanchet
parents: 37169
diff changeset
  1992
            else []) of
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1993
        [] => Const (@{const_name zero_class.zero}, T)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1994
      | ts => foldl1 (fn (t1, t2) =>
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1995
                         Const (@{const_name plus_class.plus}, T --> T --> T)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1996
                         $ t1 $ t2)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1997
                     (map (curry (op $) (Const (@{const_name single},
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1998
                                                elem_T --> T))) ts)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  1999
    end
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2000
  | multiset_postproc _ _ _ _ t = t
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2001
*}
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2002
38287
796302ca3611 replace "setup" with "declaration"
blanchet
parents: 38242
diff changeset
  2003
declaration {*
796302ca3611 replace "setup" with "declaration"
blanchet
parents: 38242
diff changeset
  2004
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
38242
f26d590dce0f adapt occurrences of renamed Nitpick functions
blanchet
parents: 37765
diff changeset
  2005
    multiset_postproc
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2006
*}
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2007
49822
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  2008
hide_const (open) fold
0cfc1651be25 simplified construction of fold combinator on multisets;
haftmann
parents: 49717
diff changeset
  2009
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2010
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2011
subsection {* Naive implementation using lists *}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2012
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2013
code_datatype multiset_of
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2014
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2015
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2016
  "{#} = multiset_of []"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2017
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2018
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2019
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2020
  "{#x#} = multiset_of [x]"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2021
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2022
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2023
lemma union_code [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2024
  "multiset_of xs + multiset_of ys = multiset_of (xs @ ys)"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2025
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2026
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2027
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2028
  "image_mset f (multiset_of xs) = multiset_of (map f xs)"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2029
  by (simp add: multiset_of_map)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2030
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2031
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2032
  "Multiset.filter f (multiset_of xs) = multiset_of (filter f xs)"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2033
  by (simp add: multiset_of_filter)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2034
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2035
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2036
  "multiset_of xs - multiset_of ys = multiset_of (fold remove1 ys xs)"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2037
  by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2038
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2039
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2040
  "multiset_of xs #\<inter> multiset_of ys =
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2041
    multiset_of (snd (fold (\<lambda>x (ys, zs).
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2042
      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
  2043
proof -
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2044
  have "\<And>zs. multiset_of (snd (fold (\<lambda>x (ys, zs).
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2045
    if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2046
      (multiset_of xs #\<inter> multiset_of ys) + multiset_of zs"
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2047
    by (induct xs arbitrary: ys)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2048
      (auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps)
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2049
  then show ?thesis by simp
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2050
qed
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2051
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2052
lemma [code]:
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2053
  "multiset_of xs #\<union> multiset_of ys =
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2054
    multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2055
proof -
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2056
  have "\<And>zs. multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2057
      (multiset_of xs #\<union> multiset_of ys) + multiset_of zs"
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
  2058
    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
  2059
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2060
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2061
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2062
lemma [code_unfold]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2063
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2064
  by (simp add: in_multiset_of)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2065
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2066
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2067
  "count (multiset_of xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2068
proof -
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2069
  have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (multiset_of xs) x + n"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2070
    by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2071
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2072
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2073
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2074
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2075
  "set_of (multiset_of xs) = set xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2076
  by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2077
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2078
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2079
  "sorted_list_of_multiset (multiset_of xs) = sort xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2080
  by (induct xs) simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2081
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2082
lemma [code]: -- {* not very efficient, but representation-ignorant! *}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2083
  "multiset_of_set A = multiset_of (sorted_list_of_set A)"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2084
  apply (cases "finite A")
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2085
  apply simp_all
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2086
  apply (induct A rule: finite_induct)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2087
  apply (simp_all add: union_commute)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2088
  done
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2089
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2090
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2091
  "mcard (multiset_of xs) = length xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2092
  by (simp add: mcard_multiset_of)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2093
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2094
fun ms_lesseq_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where 
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2095
  "ms_lesseq_impl [] ys = Some (ys \<noteq> [])"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2096
| "ms_lesseq_impl (Cons x xs) ys = (case List.extract (op = x) ys of 
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2097
     None \<Rightarrow> None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2098
   | Some (ys1,_,ys2) \<Rightarrow> ms_lesseq_impl xs (ys1 @ ys2))"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2099
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2100
lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None \<longleftrightarrow> \<not> multiset_of xs \<le> multiset_of ys) \<and>
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2101
  (ms_lesseq_impl xs ys = Some True \<longleftrightarrow> multiset_of xs < multiset_of ys) \<and>
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2102
  (ms_lesseq_impl xs ys = Some False \<longrightarrow> multiset_of xs = multiset_of ys)"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2103
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
  2104
  case (Nil ys)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2105
  show ?case by (auto simp: mset_less_empty_nonempty)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2106
next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2107
  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
  2108
  show ?case
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2109
  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
  2110
    case None
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2111
    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
  2112
    {
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2113
      assume "multiset_of (x # xs) \<le> multiset_of ys"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2114
      from set_of_mono[OF this] x have False by simp
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2115
    } note nle = this
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2116
    moreover
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2117
    {
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2118
      assume "multiset_of (x # xs) < multiset_of ys"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2119
      hence "multiset_of (x # xs) \<le> multiset_of ys" by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2120
      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
  2121
    }
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2122
    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
  2123
  next
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2124
    case (Some res)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2125
    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
  2126
    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
  2127
    from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2128
    hence id: "multiset_of ys = multiset_of (ys1 @ ys2) + {#x#}" 
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2129
      by (auto simp: ac_simps)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2130
    show ?thesis unfolding ms_lesseq_impl.simps
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2131
      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
  2132
      unfolding id
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2133
      using Cons[of "ys1 @ ys2"]
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2134
      unfolding mset_le_def mset_less_def by auto
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2135
  qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2136
qed
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2137
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2138
lemma [code]: "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> ms_lesseq_impl xs ys \<noteq> None"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2139
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2140
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2141
lemma [code]: "multiset_of xs < multiset_of ys \<longleftrightarrow> ms_lesseq_impl xs ys = Some True"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2142
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2143
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2144
instantiation multiset :: (equal) equal
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2145
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2146
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2147
definition
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2148
  [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2149
lemma [code]: "HOL.equal (multiset_of xs) (multiset_of ys) \<longleftrightarrow> ms_lesseq_impl xs ys = Some False"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2150
  unfolding equal_multiset_def
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2151
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2152
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2153
instance
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 55565
diff changeset
  2154
  by default (simp add: equal_multiset_def)
37169
f69efa106feb make Nitpick "show_all" option behave less surprisingly
blanchet
parents: 37107
diff changeset
  2155
end
49388
1ffd5a055acf typeclass formalising bounded subtraction
haftmann
parents: 48040
diff changeset
  2156
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2157
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2158
  "msetsum (multiset_of xs) = listsum xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2159
  by (induct xs) (simp_all add: add.commute)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2160
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2161
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2162
  "msetprod (multiset_of xs) = fold times xs 1"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2163
proof -
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2164
  have "\<And>x. fold times xs x = msetprod (multiset_of xs) * x"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2165
    by (induct xs) (simp_all add: mult.assoc)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2166
  then show ?thesis by simp
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2167
qed
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2168
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2169
lemma [code]:
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2170
  "size = mcard"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2171
  by (fact size_eq_mcard)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2172
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2173
text {*
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2174
  Exercise for the casual reader: add implementations for @{const le_multiset}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2175
  and @{const less_multiset} (multiset order).
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2176
*}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2177
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2178
text {* Quickcheck generators *}
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2179
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2180
definition (in term_syntax)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2181
  msetify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2182
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2183
  [code_unfold]: "msetify xs = Code_Evaluation.valtermify multiset_of {\<cdot>} xs"
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2184
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2185
notation fcomp (infixl "\<circ>>" 60)
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2186
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
  2187
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2188
instantiation multiset :: (random) random
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2189
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2190
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2191
definition
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2192
  "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
  2193
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2194
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2195
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2196
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2197
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2198
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
  2199
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
  2200
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2201
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
  2202
begin
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2203
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2204
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
  2205
where
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2206
  "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
  2207
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2208
instance ..
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2209
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2210
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2211
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2212
hide_const (open) msetify
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2213
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2214
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2215
subsection {* BNF setup *}
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2216
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2217
lemma setsum_gt_0_iff:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2218
fixes f :: "'a \<Rightarrow> nat" assumes "finite A"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2219
shows "setsum f A > 0 \<longleftrightarrow> (\<exists> a \<in> A. f a > 0)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2220
(is "?L \<longleftrightarrow> ?R")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2221
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2222
  have "?L \<longleftrightarrow> \<not> setsum f A = 0" by fast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2223
  also have "... \<longleftrightarrow> (\<exists> a \<in> A. f a \<noteq> 0)" using assms by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2224
  also have "... \<longleftrightarrow> ?R" by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2225
  finally show ?thesis .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2226
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2227
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2228
lift_definition mmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" is
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2229
  "\<lambda>h f b. setsum f {a. h a = b \<and> f a > 0} :: nat"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2230
unfolding multiset_def proof safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2231
  fix h :: "'a \<Rightarrow> 'b" and f :: "'a \<Rightarrow> nat"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2232
  assume fin: "finite {a. 0 < f a}"  (is "finite ?A")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2233
  show "finite {b. 0 < setsum f {a. h a = b \<and> 0 < f a}}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2234
  (is "finite {b. 0 < setsum f (?As b)}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2235
  proof- let ?B = "{b. 0 < setsum f (?As b)}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2236
    have "\<And> b. finite (?As b)" using fin by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2237
    hence B: "?B = {b. ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2238
    hence "?B \<subseteq> h ` ?A" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2239
    thus ?thesis using finite_surj[OF fin] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2240
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2241
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2242
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2243
lemma mmap_id0: "mmap id = id"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2244
proof (intro ext multiset_eqI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2245
  fix f a show "count (mmap id f) a = count (id f) a"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2246
  proof (cases "count f a = 0")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2247
    case False
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2248
    hence 1: "{aa. aa = a \<and> aa \<in># f} = {a}" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2249
    thus ?thesis by transfer auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2250
  qed (transfer, simp)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2251
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2252
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2253
lemma inj_on_setsum_inv:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2254
assumes 1: "(0::nat) < setsum (count f) {a. h a = b' \<and> a \<in># f}" (is "0 < setsum (count f) ?A'")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2255
and     2: "{a. h a = b \<and> a \<in># f} = {a. h a = b' \<and> a \<in># f}" (is "?A = ?A'")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2256
shows "b = b'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2257
using assms by (auto simp add: setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2258
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2259
lemma mmap_comp:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2260
fixes h1 :: "'a \<Rightarrow> 'b" and h2 :: "'b \<Rightarrow> 'c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2261
shows "mmap (h2 o h1) = mmap h2 o mmap h1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2262
proof (intro ext multiset_eqI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2263
  fix f :: "'a multiset" fix c :: 'c
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2264
  let ?A = "{a. h2 (h1 a) = c \<and> a \<in># f}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2265
  let ?As = "\<lambda> b. {a. h1 a = b \<and> a \<in># f}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2266
  let ?B = "{b. h2 b = c \<and> 0 < setsum (count f) (?As b)}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2267
  have 0: "{?As b | b.  b \<in> ?B} = ?As ` ?B" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2268
  have "\<And> b. finite (?As b)" by transfer (simp add: multiset_def)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2269
  hence "?B = {b. h2 b = c \<and> ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2270
  hence A: "?A = \<Union> {?As b | b.  b \<in> ?B}" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2271
  have "setsum (count f) ?A = setsum (setsum (count f)) {?As b | b.  b \<in> ?B}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2272
    unfolding A by transfer (intro setsum_Union_disjoint, auto simp: multiset_def)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2273
  also have "... = setsum (setsum (count f)) (?As ` ?B)" unfolding 0 ..
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2274
  also have "... = setsum (setsum (count f) o ?As) ?B"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2275
    by(intro setsum_reindex) (auto simp add: setsum_gt_0_iff inj_on_def)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2276
  also have "... = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" unfolding comp_def ..
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2277
  finally have "setsum (count f) ?A = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2278
  thus "count (mmap (h2 \<circ> h1) f) c = count ((mmap h2 \<circ> mmap h1) f) c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2279
    by transfer (unfold comp_apply, blast)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2280
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2281
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2282
lemma mmap_cong:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2283
assumes "\<And>a. a \<in># M \<Longrightarrow> f a = g a"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2284
shows "mmap f M = mmap g M"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2285
using assms by transfer (auto intro!: setsum_cong)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2286
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2287
context
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2288
begin
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2289
interpretation lifting_syntax .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2290
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2291
lemma set_of_transfer[transfer_rule]: "(pcr_multiset op = ===> op =) (\<lambda>f. {a. 0 < f a}) set_of"
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55913
diff changeset
  2292
  unfolding set_of_def pcr_multiset_def cr_multiset_def rel_fun_def by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2293
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2294
end
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
  2295
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2296
lemma set_of_mmap: "set_of o mmap h = image h o set_of"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2297
proof (rule ext, unfold comp_apply)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2298
  fix M show "set_of (mmap h M) = h ` set_of M"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2299
    by transfer (auto simp add: multiset_def setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2300
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2301
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2302
lemma multiset_of_surj:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2303
  "multiset_of ` {as. set as \<subseteq> A} = {M. set_of M \<subseteq> A}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2304
proof safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2305
  fix M assume M: "set_of M \<subseteq> A"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2306
  obtain as where eq: "M = multiset_of as" using surj_multiset_of unfolding surj_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2307
  hence "set as \<subseteq> A" using M by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2308
  thus "M \<in> multiset_of ` {as. set as \<subseteq> A}" using eq by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2309
next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2310
  show "\<And>x xa xb. \<lbrakk>set xa \<subseteq> A; xb \<in> set_of (multiset_of xa)\<rbrakk> \<Longrightarrow> xb \<in> A"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2311
  by (erule set_mp) (unfold set_of_multiset_of)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2312
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2313
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2314
lemma card_of_set_of:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2315
"(card_of {M. set_of M \<subseteq> A}, card_of {as. set as \<subseteq> A}) \<in> ordLeq"
55811
aa1acc25126b load Metis a little later
traytel
parents: 55808
diff changeset
  2316
apply(rule surj_imp_ordLeq[of _ multiset_of]) using multiset_of_surj by auto
55129
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2317
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2318
lemma nat_sum_induct:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2319
assumes "\<And>n1 n2. (\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow> phi m1 m2) \<Longrightarrow> phi n1 n2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2320
shows "phi (n1::nat) (n2::nat)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2321
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2322
  let ?chi = "\<lambda> n1n2 :: nat * nat. phi (fst n1n2) (snd n1n2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2323
  have "?chi (n1,n2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2324
  apply(induct rule: measure_induct[of "\<lambda> n1n2. fst n1n2 + snd n1n2" ?chi])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2325
  using assms by (metis fstI sndI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2326
  thus ?thesis by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2327
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2328
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2329
lemma matrix_count:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2330
fixes ct1 ct2 :: "nat \<Rightarrow> nat"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2331
assumes "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2332
shows
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2333
"\<exists> ct. (\<forall> i1 \<le> n1. setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ct1 i1) \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2334
       (\<forall> i2 \<le> n2. setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ct2 i2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2335
(is "?phi ct1 ct2 n1 n2")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2336
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2337
  have "\<forall> ct1 ct2 :: nat \<Rightarrow> nat.
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2338
        setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2339
  proof(induct rule: nat_sum_induct[of
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2340
"\<lambda> n1 n2. \<forall> ct1 ct2 :: nat \<Rightarrow> nat.
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2341
     setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"],
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2342
      clarify)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2343
  fix n1 n2 :: nat and ct1 ct2 :: "nat \<Rightarrow> nat"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2344
  assume IH: "\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2345
                \<forall> dt1 dt2 :: nat \<Rightarrow> nat.
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2346
                setsum dt1 {..<Suc m1} = setsum dt2 {..<Suc m2} \<longrightarrow> ?phi dt1 dt2 m1 m2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2347
  and ss: "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2348
  show "?phi ct1 ct2 n1 n2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2349
  proof(cases n1)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2350
    case 0 note n1 = 0
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2351
    show ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2352
    proof(cases n2)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2353
      case 0 note n2 = 0
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2354
      let ?ct = "\<lambda> i1 i2. ct2 0"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2355
      show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2356
    next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2357
      case (Suc m2) note n2 = Suc
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2358
      let ?ct = "\<lambda> i1 i2. ct2 i2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2359
      show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2360
    qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2361
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2362
    case (Suc m1) note n1 = Suc
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2363
    show ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2364
    proof(cases n2)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2365
      case 0 note n2 = 0
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2366
      let ?ct = "\<lambda> i1 i2. ct1 i1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2367
      show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2368
    next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2369
      case (Suc m2) note n2 = Suc
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2370
      show ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2371
      proof(cases "ct1 n1 \<le> ct2 n2")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2372
        case True
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2373
        def dt2 \<equiv> "\<lambda> i2. if i2 = n2 then ct2 i2 - ct1 n1 else ct2 i2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2374
        have "setsum ct1 {..<Suc m1} = setsum dt2 {..<Suc n2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2375
        unfolding dt2_def using ss n1 True by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2376
        hence "?phi ct1 dt2 m1 n2" using IH[of m1 n2] n1 by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2377
        then obtain dt where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2378
        1: "\<And> i1. i1 \<le> m1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc n2} = ct1 i1" and
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2379
        2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc m1} = dt2 i2" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2380
        let ?ct = "\<lambda> i1 i2. if i1 = n1 then (if i2 = n2 then ct1 n1 else 0)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2381
                                       else dt i1 i2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2382
        show ?thesis apply(rule exI[of _ ?ct])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2383
        using n1 n2 1 2 True unfolding dt2_def by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2384
      next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2385
        case False
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2386
        hence False: "ct2 n2 < ct1 n1" by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2387
        def dt1 \<equiv> "\<lambda> i1. if i1 = n1 then ct1 i1 - ct2 n2 else ct1 i1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2388
        have "setsum dt1 {..<Suc n1} = setsum ct2 {..<Suc m2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2389
        unfolding dt1_def using ss n2 False by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2390
        hence "?phi dt1 ct2 n1 m2" using IH[of n1 m2] n2 by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2391
        then obtain dt where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2392
        1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc m2} = dt1 i1" and
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2393
        2: "\<And> i2. i2 \<le> m2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc n1} = ct2 i2" by force
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2394
        let ?ct = "\<lambda> i1 i2. if i2 = n2 then (if i1 = n1 then ct2 n2 else 0)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2395
                                       else dt i1 i2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2396
        show ?thesis apply(rule exI[of _ ?ct])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2397
        using n1 n2 1 2 False unfolding dt1_def by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2398
      qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2399
    qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2400
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2401
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2402
  thus ?thesis using assms by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2403
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2404
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2405
definition
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2406
"inj2 u B1 B2 \<equiv>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2407
 \<forall> b1 b1' b2 b2'. {b1,b1'} \<subseteq> B1 \<and> {b2,b2'} \<subseteq> B2 \<and> u b1 b2 = u b1' b2'
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2408
                  \<longrightarrow> b1 = b1' \<and> b2 = b2'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2409
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2410
lemma matrix_setsum_finite:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2411
assumes B1: "B1 \<noteq> {}" "finite B1" and B2: "B2 \<noteq> {}" "finite B2" and u: "inj2 u B1 B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2412
and ss: "setsum N1 B1 = setsum N2 B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2413
shows "\<exists> M :: 'a \<Rightarrow> nat.
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2414
            (\<forall> b1 \<in> B1. setsum (\<lambda> b2. M (u b1 b2)) B2 = N1 b1) \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2415
            (\<forall> b2 \<in> B2. setsum (\<lambda> b1. M (u b1 b2)) B1 = N2 b2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2416
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2417
  obtain n1 where "card B1 = Suc n1" using B1 by (metis card_insert finite.simps)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2418
  then obtain e1 where e1: "bij_betw e1 {..<Suc n1} B1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2419
  using ex_bij_betw_finite_nat[OF B1(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2420
  hence e1_inj: "inj_on e1 {..<Suc n1}" and e1_surj: "e1 ` {..<Suc n1} = B1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2421
  unfolding bij_betw_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2422
  def f1 \<equiv> "inv_into {..<Suc n1} e1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2423
  have f1: "bij_betw f1 B1 {..<Suc n1}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2424
  and f1e1[simp]: "\<And> i1. i1 < Suc n1 \<Longrightarrow> f1 (e1 i1) = i1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2425
  and e1f1[simp]: "\<And> b1. b1 \<in> B1 \<Longrightarrow> e1 (f1 b1) = b1" unfolding f1_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2426
  apply (metis bij_betw_inv_into e1, metis bij_betw_inv_into_left e1 lessThan_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2427
  by (metis e1_surj f_inv_into_f)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2428
  (*  *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2429
  obtain n2 where "card B2 = Suc n2" using B2 by (metis card_insert finite.simps)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2430
  then obtain e2 where e2: "bij_betw e2 {..<Suc n2} B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2431
  using ex_bij_betw_finite_nat[OF B2(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2432
  hence e2_inj: "inj_on e2 {..<Suc n2}" and e2_surj: "e2 ` {..<Suc n2} = B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2433
  unfolding bij_betw_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2434
  def f2 \<equiv> "inv_into {..<Suc n2} e2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2435
  have f2: "bij_betw f2 B2 {..<Suc n2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2436
  and f2e2[simp]: "\<And> i2. i2 < Suc n2 \<Longrightarrow> f2 (e2 i2) = i2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2437
  and e2f2[simp]: "\<And> b2. b2 \<in> B2 \<Longrightarrow> e2 (f2 b2) = b2" unfolding f2_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2438
  apply (metis bij_betw_inv_into e2, metis bij_betw_inv_into_left e2 lessThan_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2439
  by (metis e2_surj f_inv_into_f)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2440
  (*  *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2441
  let ?ct1 = "N1 o e1"  let ?ct2 = "N2 o e2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2442
  have ss: "setsum ?ct1 {..<Suc n1} = setsum ?ct2 {..<Suc n2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2443
  unfolding setsum_reindex[OF e1_inj, symmetric] setsum_reindex[OF e2_inj, symmetric]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2444
  e1_surj e2_surj using ss .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2445
  obtain ct where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2446
  ct1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ?ct1 i1" and
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2447
  ct2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ?ct2 i2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2448
  using matrix_count[OF ss] by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2449
  (*  *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2450
  def A \<equiv> "{u b1 b2 | b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2451
  have "\<forall> a \<in> A. \<exists> b1b2 \<in> B1 <*> B2. u (fst b1b2) (snd b1b2) = a"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2452
  unfolding A_def Ball_def mem_Collect_eq by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2453
  then obtain h1h2 where h12:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2454
  "\<And>a. a \<in> A \<Longrightarrow> u (fst (h1h2 a)) (snd (h1h2 a)) = a \<and> h1h2 a \<in> B1 <*> B2" by metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2455
  def h1 \<equiv> "fst o h1h2"  def h2 \<equiv> "snd o h1h2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2456
  have h12[simp]: "\<And>a. a \<in> A \<Longrightarrow> u (h1 a) (h2 a) = a"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2457
                  "\<And> a. a \<in> A \<Longrightarrow> h1 a \<in> B1"  "\<And> a. a \<in> A \<Longrightarrow> h2 a \<in> B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2458
  using h12 unfolding h1_def h2_def by force+
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2459
  {fix b1 b2 assume b1: "b1 \<in> B1" and b2: "b2 \<in> B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2460
   hence inA: "u b1 b2 \<in> A" unfolding A_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2461
   hence "u b1 b2 = u (h1 (u b1 b2)) (h2 (u b1 b2))" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2462
   moreover have "h1 (u b1 b2) \<in> B1" "h2 (u b1 b2) \<in> B2" using inA by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2463
   ultimately have "h1 (u b1 b2) = b1 \<and> h2 (u b1 b2) = b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2464
   using u b1 b2 unfolding inj2_def by fastforce
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2465
  }
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2466
  hence h1[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h1 (u b1 b2) = b1" and
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2467
        h2[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h2 (u b1 b2) = b2" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2468
  def M \<equiv> "\<lambda> a. ct (f1 (h1 a)) (f2 (h2 a))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2469
  show ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2470
  apply(rule exI[of _ M]) proof safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2471
    fix b1 assume b1: "b1 \<in> B1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2472
    hence f1b1: "f1 b1 \<le> n1" using f1 unfolding bij_betw_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2473
    by (metis image_eqI lessThan_iff less_Suc_eq_le)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2474
    have "(\<Sum>b2\<in>B2. M (u b1 b2)) = (\<Sum>i2<Suc n2. ct (f1 b1) (f2 (e2 i2)))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2475
    unfolding e2_surj[symmetric] setsum_reindex[OF e2_inj]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2476
    unfolding M_def comp_def apply(intro setsum_cong) apply force
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2477
    by (metis e2_surj b1 h1 h2 imageI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2478
    also have "... = N1 b1" using b1 ct1[OF f1b1] by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2479
    finally show "(\<Sum>b2\<in>B2. M (u b1 b2)) = N1 b1" .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2480
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2481
    fix b2 assume b2: "b2 \<in> B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2482
    hence f2b2: "f2 b2 \<le> n2" using f2 unfolding bij_betw_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2483
    by (metis image_eqI lessThan_iff less_Suc_eq_le)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2484
    have "(\<Sum>b1\<in>B1. M (u b1 b2)) = (\<Sum>i1<Suc n1. ct (f1 (e1 i1)) (f2 b2))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2485
    unfolding e1_surj[symmetric] setsum_reindex[OF e1_inj]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2486
    unfolding M_def comp_def apply(intro setsum_cong) apply force
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2487
    by (metis e1_surj b2 h1 h2 imageI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2488
    also have "... = N2 b2" using b2 ct2[OF f2b2] by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2489
    finally show "(\<Sum>b1\<in>B1. M (u b1 b2)) = N2 b2" .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2490
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2491
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2492
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2493
lemma supp_vimage_mmap: "set_of M \<subseteq> f -` (set_of (mmap f M))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2494
  by transfer (auto simp: multiset_def setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2495
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2496
lemma mmap_ge_0: "b \<in># mmap f M \<longleftrightarrow> (\<exists>a. a \<in># M \<and> f a = b)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2497
  by transfer (auto simp: multiset_def setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2498
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2499
lemma finite_twosets:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2500
assumes "finite B1" and "finite B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2501
shows "finite {u b1 b2 |b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"  (is "finite ?A")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2502
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2503
  have A: "?A = (\<lambda> b1b2. u (fst b1b2) (snd b1b2)) ` (B1 <*> B2)" by force
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2504
  show ?thesis unfolding A using finite_cartesian_product[OF assms] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2505
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2506
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2507
(* Weak pullbacks: *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2508
definition wpull where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2509
"wpull A B1 B2 f1 f2 p1 p2 \<longleftrightarrow>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2510
 (\<forall> b1 b2. b1 \<in> B1 \<and> b2 \<in> B2 \<and> f1 b1 = f2 b2 \<longrightarrow> (\<exists> a \<in> A. p1 a = b1 \<and> p2 a = b2))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2511
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2512
(* Weak pseudo-pullbacks *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2513
definition wppull where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2514
"wppull A B1 B2 f1 f2 e1 e2 p1 p2 \<longleftrightarrow>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2515
 (\<forall> b1 b2. b1 \<in> B1 \<and> b2 \<in> B2 \<and> f1 b1 = f2 b2 \<longrightarrow>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2516
           (\<exists> a \<in> A. e1 (p1 a) = e1 b1 \<and> e2 (p2 a) = e2 b2))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2517
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2518
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2519
(* The pullback of sets *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2520
definition thePull where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2521
"thePull B1 B2 f1 f2 = {(b1,b2). b1 \<in> B1 \<and> b2 \<in> B2 \<and> f1 b1 = f2 b2}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2522
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2523
lemma wpull_thePull:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2524
"wpull (thePull B1 B2 f1 f2) B1 B2 f1 f2 fst snd"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2525
unfolding wpull_def thePull_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2526
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2527
lemma wppull_thePull:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2528
assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2529
shows
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2530
"\<exists> j. \<forall> a' \<in> thePull B1 B2 f1 f2.
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2531
   j a' \<in> A \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2532
   e1 (p1 (j a')) = e1 (fst a') \<and> e2 (p2 (j a')) = e2 (snd a')"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2533
(is "\<exists> j. \<forall> a' \<in> ?A'. ?phi a' (j a')")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2534
proof(rule bchoice[of ?A' ?phi], default)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2535
  fix a' assume a': "a' \<in> ?A'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2536
  hence "fst a' \<in> B1" unfolding thePull_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2537
  moreover
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2538
  from a' have "snd a' \<in> B2" unfolding thePull_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2539
  moreover have "f1 (fst a') = f2 (snd a')"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2540
  using a' unfolding csquare_def thePull_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2541
  ultimately show "\<exists> ja'. ?phi a' ja'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2542
  using assms unfolding wppull_def by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2543
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2544
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2545
lemma wpull_wppull:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2546
assumes wp: "wpull A' B1 B2 f1 f2 p1' p2'" and
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2547
1: "\<forall> a' \<in> A'. j a' \<in> A \<and> e1 (p1 (j a')) = e1 (p1' a') \<and> e2 (p2 (j a')) = e2 (p2' a')"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2548
shows "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2549
unfolding wppull_def proof safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2550
  fix b1 b2
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2551
  assume b1: "b1 \<in> B1" and b2: "b2 \<in> B2" and f: "f1 b1 = f2 b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2552
  then obtain a' where a': "a' \<in> A'" and b1: "b1 = p1' a'" and b2: "b2 = p2' a'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2553
  using wp unfolding wpull_def by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2554
  show "\<exists>a\<in>A. e1 (p1 a) = e1 b1 \<and> e2 (p2 a) = e2 b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2555
  apply (rule bexI[of _ "j a'"]) unfolding b1 b2 using a' 1 by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2556
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2557
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2558
lemma wppull_fstOp_sndOp:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2559
shows "wppull (Collect (split (P OO Q))) (Collect (split P)) (Collect (split Q))
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2560
  snd fst fst snd (BNF_Def.fstOp P Q) (BNF_Def.sndOp P Q)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2561
using pick_middlep unfolding wppull_def fstOp_def sndOp_def relcompp.simps by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2562
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2563
lemma wpull_mmap:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2564
fixes A :: "'a set" and B1 :: "'b1 set" and B2 :: "'b2 set"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2565
assumes wp: "wpull A B1 B2 f1 f2 p1 p2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2566
shows
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2567
"wpull {M. set_of M \<subseteq> A}
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2568
       {N1. set_of N1 \<subseteq> B1} {N2. set_of N2 \<subseteq> B2}
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2569
       (mmap f1) (mmap f2) (mmap p1) (mmap p2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2570
unfolding wpull_def proof (safe, unfold Bex_def mem_Collect_eq)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2571
  fix N1 :: "'b1 multiset" and N2 :: "'b2 multiset"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2572
  assume mmap': "mmap f1 N1 = mmap f2 N2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2573
  and N1[simp]: "set_of N1 \<subseteq> B1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2574
  and N2[simp]: "set_of N2 \<subseteq> B2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2575
  def P \<equiv> "mmap f1 N1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2576
  have P1: "P = mmap f1 N1" and P2: "P = mmap f2 N2" unfolding P_def using mmap' by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2577
  note P = P1 P2
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2578
  have fin_N1[simp]: "finite (set_of N1)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2579
   and fin_N2[simp]: "finite (set_of N2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2580
   and fin_P[simp]: "finite (set_of P)" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2581
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2582
  def set1 \<equiv> "\<lambda> c. {b1 \<in> set_of N1. f1 b1 = c}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2583
  have set1[simp]: "\<And> c b1. b1 \<in> set1 c \<Longrightarrow> f1 b1 = c" unfolding set1_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2584
  have fin_set1: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (set1 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2585
    using N1(1) unfolding set1_def multiset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2586
  have set1_NE: "\<And> c. c \<in> set_of P \<Longrightarrow> set1 c \<noteq> {}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2587
   unfolding set1_def set_of_def P mmap_ge_0 by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2588
  have supp_N1_set1: "set_of N1 = (\<Union> c \<in> set_of P. set1 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2589
    using supp_vimage_mmap[of N1 f1] unfolding set1_def P1 by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2590
  hence set1_inclN1: "\<And>c. c \<in> set_of P \<Longrightarrow> set1 c \<subseteq> set_of N1" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2591
  hence set1_incl: "\<And> c. c \<in> set_of P \<Longrightarrow> set1 c \<subseteq> B1" using N1 by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2592
  have set1_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set1 c \<inter> set1 c' = {}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2593
    unfolding set1_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2594
  have setsum_set1: "\<And> c. setsum (count N1) (set1 c) = count P c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2595
    unfolding P1 set1_def by transfer (auto intro: setsum_cong)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2596
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2597
  def set2 \<equiv> "\<lambda> c. {b2 \<in> set_of N2. f2 b2 = c}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2598
  have set2[simp]: "\<And> c b2. b2 \<in> set2 c \<Longrightarrow> f2 b2 = c" unfolding set2_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2599
  have fin_set2: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (set2 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2600
  using N2(1) unfolding set2_def multiset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2601
  have set2_NE: "\<And> c. c \<in> set_of P \<Longrightarrow> set2 c \<noteq> {}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2602
    unfolding set2_def P2 mmap_ge_0 set_of_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2603
  have supp_N2_set2: "set_of N2 = (\<Union> c \<in> set_of P. set2 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2604
    using supp_vimage_mmap[of N2 f2] unfolding set2_def P2 by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2605
  hence set2_inclN2: "\<And>c. c \<in> set_of P \<Longrightarrow> set2 c \<subseteq> set_of N2" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2606
  hence set2_incl: "\<And> c. c \<in> set_of P \<Longrightarrow> set2 c \<subseteq> B2" using N2 by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2607
  have set2_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set2 c \<inter> set2 c' = {}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2608
    unfolding set2_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2609
  have setsum_set2: "\<And> c. setsum (count N2) (set2 c) = count P c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2610
    unfolding P2 set2_def by transfer (auto intro: setsum_cong)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2611
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2612
  have ss: "\<And> c. c \<in> set_of P \<Longrightarrow> setsum (count N1) (set1 c) = setsum (count N2) (set2 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2613
    unfolding setsum_set1 setsum_set2 ..
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2614
  have "\<forall> c \<in> set_of P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2615
          \<exists> a \<in> A. p1 a = fst b1b2 \<and> p2 a = snd b1b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2616
    using wp set1_incl set2_incl unfolding wpull_def Ball_def mem_Collect_eq
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2617
    by simp (metis set1 set2 set_rev_mp)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2618
  then obtain uu where uu:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2619
  "\<forall> c \<in> set_of P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2620
     uu c b1b2 \<in> A \<and> p1 (uu c b1b2) = fst b1b2 \<and> p2 (uu c b1b2) = snd b1b2" by metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2621
  def u \<equiv> "\<lambda> c b1 b2. uu c (b1,b2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2622
  have u[simp]:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2623
  "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> u c b1 b2 \<in> A"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2624
  "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> p1 (u c b1 b2) = b1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2625
  "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> p2 (u c b1 b2) = b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2626
    using uu unfolding u_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2627
  {fix c assume c: "c \<in> set_of P"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2628
   have "inj2 (u c) (set1 c) (set2 c)" unfolding inj2_def proof clarify
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2629
     fix b1 b1' b2 b2'
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2630
     assume "{b1, b1'} \<subseteq> set1 c" "{b2, b2'} \<subseteq> set2 c" and 0: "u c b1 b2 = u c b1' b2'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2631
     hence "p1 (u c b1 b2) = b1 \<and> p2 (u c b1 b2) = b2 \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2632
            p1 (u c b1' b2') = b1' \<and> p2 (u c b1' b2') = b2'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2633
     using u(2)[OF c] u(3)[OF c] by simp metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2634
     thus "b1 = b1' \<and> b2 = b2'" using 0 by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2635
   qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2636
  } note inj = this
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2637
  def sset \<equiv> "\<lambda> c. {u c b1 b2 | b1 b2. b1 \<in> set1 c \<and> b2 \<in> set2 c}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2638
  have fin_sset[simp]: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (sset c)" unfolding sset_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2639
    using fin_set1 fin_set2 finite_twosets by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2640
  have sset_A: "\<And> c. c \<in> set_of P \<Longrightarrow> sset c \<subseteq> A" unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2641
  {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2642
   then obtain b1 b2 where b1: "b1 \<in> set1 c" and b2: "b2 \<in> set2 c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2643
   and a: "a = u c b1 b2" unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2644
   have "p1 a \<in> set1 c" and p2a: "p2 a \<in> set2 c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2645
   using ac a b1 b2 c u(2) u(3) by simp+
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2646
   hence "u c (p1 a) (p2 a) = a" unfolding a using b1 b2 inj[OF c]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2647
   unfolding inj2_def by (metis c u(2) u(3))
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2648
  } note u_p12[simp] = this
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2649
  {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2650
   hence "p1 a \<in> set1 c" unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2651
  }note p1[simp] = this
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2652
  {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2653
   hence "p2 a \<in> set2 c" unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2654
  }note p2[simp] = this
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2655
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2656
  {fix c assume c: "c \<in> set_of P"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2657
   hence "\<exists> M. (\<forall> b1 \<in> set1 c. setsum (\<lambda> b2. M (u c b1 b2)) (set2 c) = count N1 b1) \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2658
               (\<forall> b2 \<in> set2 c. setsum (\<lambda> b1. M (u c b1 b2)) (set1 c) = count N2 b2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2659
   unfolding sset_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2660
   using matrix_setsum_finite[OF set1_NE[OF c] fin_set1[OF c]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2661
                                 set2_NE[OF c] fin_set2[OF c] inj[OF c] ss[OF c]] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2662
  }
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2663
  then obtain Ms where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2664
  ss1: "\<And> c b1. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c\<rbrakk> \<Longrightarrow>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2665
                   setsum (\<lambda> b2. Ms c (u c b1 b2)) (set2 c) = count N1 b1" and
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2666
  ss2: "\<And> c b2. \<lbrakk>c \<in> set_of P; b2 \<in> set2 c\<rbrakk> \<Longrightarrow>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2667
                   setsum (\<lambda> b1. Ms c (u c b1 b2)) (set1 c) = count N2 b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2668
  by metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2669
  def SET \<equiv> "\<Union> c \<in> set_of P. sset c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2670
  have fin_SET[simp]: "finite SET" unfolding SET_def apply(rule finite_UN_I) by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2671
  have SET_A: "SET \<subseteq> A" unfolding SET_def using sset_A by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2672
  have u_SET[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> u c b1 b2 \<in> SET"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2673
    unfolding SET_def sset_def by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2674
  {fix c a assume c: "c \<in> set_of P" and a: "a \<in> SET" and p1a: "p1 a \<in> set1 c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2675
   then obtain c' where c': "c' \<in> set_of P" and ac': "a \<in> sset c'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2676
    unfolding SET_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2677
   hence "p1 a \<in> set1 c'" unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2678
   hence eq: "c = c'" using p1a c c' set1_disj by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2679
   hence "a \<in> sset c" using ac' by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2680
  } note p1_rev = this
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2681
  {fix c a assume c: "c \<in> set_of P" and a: "a \<in> SET" and p2a: "p2 a \<in> set2 c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2682
   then obtain c' where c': "c' \<in> set_of P" and ac': "a \<in> sset c'"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2683
   unfolding SET_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2684
   hence "p2 a \<in> set2 c'" unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2685
   hence eq: "c = c'" using p2a c c' set2_disj by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2686
   hence "a \<in> sset c" using ac' by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2687
  } note p2_rev = this
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2688
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2689
  have "\<forall> a \<in> SET. \<exists> c \<in> set_of P. a \<in> sset c" unfolding SET_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2690
  then obtain h where h: "\<forall> a \<in> SET. h a \<in> set_of P \<and> a \<in> sset (h a)" by metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2691
  have h_u[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2692
                      \<Longrightarrow> h (u c b1 b2) = c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2693
  by (metis h p2 set2 u(3) u_SET)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2694
  have h_u1: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2695
                      \<Longrightarrow> h (u c b1 b2) = f1 b1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2696
  using h unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2697
  have h_u2: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2698
                      \<Longrightarrow> h (u c b1 b2) = f2 b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2699
  using h unfolding sset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2700
  def M \<equiv>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2701
    "Abs_multiset (\<lambda> a. if a \<in> SET \<and> p1 a \<in> set_of N1 \<and> p2 a \<in> set_of N2 then Ms (h a) a else 0)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2702
  have "(\<lambda> a. if a \<in> SET \<and> p1 a \<in> set_of N1 \<and> p2 a \<in> set_of N2 then Ms (h a) a else 0) \<in> multiset"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2703
    unfolding multiset_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2704
  hence [transfer_rule]: "pcr_multiset op = (\<lambda> a. if a \<in> SET \<and> p1 a \<in> set_of N1 \<and> p2 a \<in> set_of N2 then Ms (h a) a else 0) M"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2705
    unfolding M_def pcr_multiset_def cr_multiset_def by (auto simp: Abs_multiset_inverse)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2706
  have sM: "set_of M \<subseteq> SET" "set_of M \<subseteq> p1 -` (set_of N1)" "set_of M \<subseteq> p2 -` set_of N2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2707
    by (transfer, auto split: split_if_asm)+
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2708
  show "\<exists>M. set_of M \<subseteq> A \<and> mmap p1 M = N1 \<and> mmap p2 M = N2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2709
  proof(rule exI[of _ M], safe)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2710
    fix a assume *: "a \<in> set_of M"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2711
    from SET_A show "a \<in> A"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2712
    proof (cases "a \<in> SET")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2713
      case False thus ?thesis using * by transfer' auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2714
    qed blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2715
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2716
    show "mmap p1 M = N1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2717
    proof(intro multiset_eqI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2718
      fix b1
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2719
      let ?K = "{a. p1 a = b1 \<and> a \<in># M}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2720
      have "setsum (count M) ?K = count N1 b1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2721
      proof(cases "b1 \<in> set_of N1")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2722
        case False
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2723
        hence "?K = {}" using sM(2) by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2724
        thus ?thesis using False by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2725
      next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2726
        case True
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2727
        def c \<equiv> "f1 b1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2728
        have c: "c \<in> set_of P" and b1: "b1 \<in> set1 c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2729
          unfolding set1_def c_def P1 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2730
        with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 \<and> a \<in> SET}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2731
          by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2732
        also have "... = setsum (count M) ((\<lambda> b2. u c b1 b2) ` (set2 c))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2733
          apply(rule setsum_cong) using c b1 proof safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2734
          fix a assume p1a: "p1 a \<in> set1 c" and "c \<in> set_of P" and "a \<in> SET"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2735
          hence ac: "a \<in> sset c" using p1_rev by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2736
          hence "a = u c (p1 a) (p2 a)" using c by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2737
          moreover have "p2 a \<in> set2 c" using ac c by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2738
          ultimately show "a \<in> u c (p1 a) ` set2 c" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2739
        qed auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2740
        also have "... = setsum (\<lambda> b2. count M (u c b1 b2)) (set2 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2741
          unfolding comp_def[symmetric] apply(rule setsum_reindex)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2742
          using inj unfolding inj_on_def inj2_def using b1 c u(3) by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2743
        also have "... = count N1 b1" unfolding ss1[OF c b1, symmetric]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2744
          apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b1 set2)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2745
          using True h_u[OF c b1] set2_def u(2,3)[OF c b1] u_SET[OF c b1] by fastforce
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2746
        finally show ?thesis .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2747
      qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2748
      thus "count (mmap p1 M) b1 = count N1 b1" by transfer
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2749
    qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2750
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2751
    show "mmap p2 M = N2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2752
    proof(intro multiset_eqI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2753
      fix b2
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2754
      let ?K = "{a. p2 a = b2 \<and> a \<in># M}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2755
      have "setsum (count M) ?K = count N2 b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2756
      proof(cases "b2 \<in> set_of N2")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2757
        case False
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2758
        hence "?K = {}" using sM(3) by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2759
        thus ?thesis using False by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2760
      next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2761
        case True
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2762
        def c \<equiv> "f2 b2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2763
        have c: "c \<in> set_of P" and b2: "b2 \<in> set2 c"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2764
          unfolding set2_def c_def P2 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2765
        with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 \<and> a \<in> SET}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2766
          by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2767
        also have "... = setsum (count M) ((\<lambda> b1. u c b1 b2) ` (set1 c))"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2768
          apply(rule setsum_cong) using c b2 proof safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2769
          fix a assume p2a: "p2 a \<in> set2 c" and "c \<in> set_of P" and "a \<in> SET"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2770
          hence ac: "a \<in> sset c" using p2_rev by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2771
          hence "a = u c (p1 a) (p2 a)" using c by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2772
          moreover have "p1 a \<in> set1 c" using ac c by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2773
          ultimately show "a \<in> (\<lambda>x. u c x (p2 a)) ` set1 c" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2774
        qed auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2775
        also have "... = setsum (count M o (\<lambda> b1. u c b1 b2)) (set1 c)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2776
          apply(rule setsum_reindex)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2777
          using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2778
        also have "... = setsum (\<lambda> b1. count M (u c b1 b2)) (set1 c)" by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2779
        also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] comp_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2780
          apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b2 set1)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2781
          using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def by fastforce
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2782
        finally show ?thesis .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2783
      qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2784
      thus "count (mmap p2 M) b2 = count N2 b2" by transfer
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2785
    qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2786
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2787
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2788
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2789
lemma set_of_bd: "(card_of (set_of x), natLeq) \<in> ordLeq"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2790
  by transfer
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2791
    (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2792
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2793
lemma wppull_mmap:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2794
  assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2795
  shows "wppull {M. set_of M \<subseteq> A} {N1. set_of N1 \<subseteq> B1} {N2. set_of N2 \<subseteq> B2}
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2796
    (mmap f1) (mmap f2) (mmap e1) (mmap e2) (mmap p1) (mmap p2)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2797
proof -
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2798
  from assms obtain j where j: "\<forall>a'\<in>thePull B1 B2 f1 f2.
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2799
    j a' \<in> A \<and> e1 (p1 (j a')) = e1 (fst a') \<and> e2 (p2 (j a')) = e2 (snd a')" 
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2800
    by (blast dest: wppull_thePull)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2801
  then show ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2802
    by (intro wpull_wppull[OF wpull_mmap[OF wpull_thePull], of _ _ _ _ "mmap j"])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2803
      (auto simp: comp_eq_dest_lhs[OF mmap_comp[symmetric]] comp_eq_dest[OF set_of_mmap]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2804
        intro!: mmap_cong simp del: mem_set_of_iff simp: mem_set_of_iff[symmetric])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2805
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2806
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2807
bnf "'a multiset"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2808
  map: mmap
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2809
  sets: set_of 
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2810
  bd: natLeq
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2811
  wits: "{#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2812
by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2813
  Grp_def relcompp.simps intro: mmap_cong)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2814
  (metis wppull_mmap[OF wppull_fstOp_sndOp, unfolded wppull_def
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2815
    o_eq_dest_lhs[OF mmap_comp[symmetric]] fstOp_def sndOp_def comp_def, simplified])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2816
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2817
inductive rel_multiset' where
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2818
  Zero[intro]: "rel_multiset' R {#} {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2819
| Plus[intro]: "\<lbrakk>R a b; rel_multiset' R M N\<rbrakk> \<Longrightarrow> rel_multiset' R (M + {#a#}) (N + {#b#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2820
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2821
lemma map_multiset_Zero_iff[simp]: "mmap f M = {#} \<longleftrightarrow> M = {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2822
by (metis image_is_empty multiset.set_map set_of_eq_empty_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2823
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2824
lemma map_multiset_Zero[simp]: "mmap f {#} = {#}" by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2825
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2826
lemma rel_multiset_Zero: "rel_multiset R {#} {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2827
unfolding rel_multiset_def Grp_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2828
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2829
declare multiset.count[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2830
declare Abs_multiset_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2831
declare multiset.count_inverse[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2832
declare union_preserves_multiset[simp]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2833
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2834
lemma map_multiset_Plus[simp]: "mmap f (M1 + M2) = mmap f M1 + mmap f M2"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2835
proof (intro multiset_eqI, transfer fixing: f)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2836
  fix x :: 'a and M1 M2 :: "'b \<Rightarrow> nat"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2837
  assume "M1 \<in> multiset" "M2 \<in> multiset"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2838
  hence "setsum M1 {a. f a = x \<and> 0 < M1 a} = setsum M1 {a. f a = x \<and> 0 < M1 a + M2 a}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2839
        "setsum M2 {a. f a = x \<and> 0 < M2 a} = setsum M2 {a. f a = x \<and> 0 < M1 a + M2 a}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2840
    by (auto simp: multiset_def intro!: setsum_mono_zero_cong_left)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2841
  then show "(\<Sum>a | f a = x \<and> 0 < M1 a + M2 a. M1 a + M2 a) =
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2842
       setsum M1 {a. f a = x \<and> 0 < M1 a} +
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2843
       setsum M2 {a. f a = x \<and> 0 < M2 a}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2844
    by (auto simp: setsum.distrib[symmetric])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2845
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2846
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2847
lemma map_multiset_single[simp]: "mmap f {#a#} = {#f a#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2848
  by transfer auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2849
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2850
lemma rel_multiset_Plus:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2851
assumes ab: "R a b" and MN: "rel_multiset R M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2852
shows "rel_multiset R (M + {#a#}) (N + {#b#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2853
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2854
  {fix y assume "R a b" and "set_of y \<subseteq> {(x, y). R x y}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2855
   hence "\<exists>ya. mmap fst y + {#a#} = mmap fst ya \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2856
               mmap snd y + {#b#} = mmap snd ya \<and>
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2857
               set_of ya \<subseteq> {(x, y). R x y}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2858
   apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2859
  }
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2860
  thus ?thesis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2861
  using assms
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2862
  unfolding rel_multiset_def Grp_def by force
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2863
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2864
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2865
lemma rel_multiset'_imp_rel_multiset:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2866
"rel_multiset' R M N \<Longrightarrow> rel_multiset R M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2867
apply(induct rule: rel_multiset'.induct)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2868
using rel_multiset_Zero rel_multiset_Plus by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2869
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2870
lemma mcard_mmap[simp]: "mcard (mmap f M) = mcard M"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2871
proof -
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2872
  def A \<equiv> "\<lambda> b. {a. f a = b \<and> a \<in># M}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2873
  let ?B = "{b. 0 < setsum (count M) (A b)}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2874
  have "{b. \<exists>a. f a = b \<and> a \<in># M} \<subseteq> f ` {a. a \<in># M}" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2875
  moreover have "finite (f ` {a. a \<in># M})" apply(rule finite_imageI)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2876
  using finite_Collect_mem .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2877
  ultimately have fin: "finite {b. \<exists>a. f a = b \<and> a \<in># M}" by(rule finite_subset)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2878
  have i: "inj_on A ?B" unfolding inj_on_def A_def apply clarsimp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2879
    by (metis (lifting, full_types) mem_Collect_eq neq0_conv setsum.neutral)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2880
  have 0: "\<And> b. 0 < setsum (count M) (A b) \<longleftrightarrow> (\<exists> a \<in> A b. count M a > 0)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2881
  apply safe
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2882
    apply (metis less_not_refl setsum_gt_0_iff setsum_infinite)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2883
    by (metis A_def finite_Collect_conjI finite_Collect_mem setsum_gt_0_iff)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2884
  hence AB: "A ` ?B = {A b | b. \<exists> a \<in> A b. count M a > 0}" by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2885
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2886
  have "setsum (\<lambda> x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2887
  unfolding comp_def ..
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2888
  also have "... = (\<Sum>x\<in> A ` ?B. setsum (count M) x)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2889
  unfolding setsum.reindex [OF i, symmetric] ..
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2890
  also have "... = setsum (count M) (\<Union>x\<in>A ` {b. 0 < setsum (count M) (A b)}. x)"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2891
  (is "_ = setsum (count M) ?J")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2892
  apply(rule setsum.UNION_disjoint[symmetric])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2893
  using 0 fin unfolding A_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2894
  also have "?J = {a. a \<in># M}" unfolding AB unfolding A_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2895
  finally have "setsum (\<lambda> x. setsum (count M) (A x)) ?B =
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2896
                setsum (count M) {a. a \<in># M}" .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2897
  then show ?thesis unfolding mcard_unfold_setsum A_def by transfer
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2898
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2899
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2900
lemma rel_multiset_mcard:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2901
assumes "rel_multiset R M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2902
shows "mcard M = mcard N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2903
using assms unfolding rel_multiset_def Grp_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2904
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2905
lemma multiset_induct2[case_names empty addL addR]:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2906
assumes empty: "P {#} {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2907
and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2908
and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2909
shows "P M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2910
apply(induct N rule: multiset_induct)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2911
  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
  2912
  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
  2913
done
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2914
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2915
lemma multiset_induct2_mcard[consumes 1, case_names empty add]:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2916
assumes c: "mcard M = mcard N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2917
and empty: "P {#} {#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2918
and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2919
shows "P M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2920
using c proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2921
  case (less M)  show ?case
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2922
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2923
    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
  2924
    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
  2925
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2926
    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2927
    have "N \<noteq> {#}" using False less.prems by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2928
    then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2929
    have "mcard M1 = mcard N1" using less.prems unfolding M N by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2930
    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
  2931
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2932
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2933
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2934
lemma msed_map_invL:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2935
assumes "mmap f (M + {#a#}) = N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2936
shows "\<exists> N1. N = N1 + {#f a#} \<and> mmap f M = N1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2937
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2938
  have "f a \<in># N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2939
  using assms multiset.set_map[of f "M + {#a#}"] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2940
  then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2941
  have "mmap f M = N1" using assms unfolding N by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2942
  thus ?thesis using N by blast
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2943
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2944
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2945
lemma msed_map_invR:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2946
assumes "mmap f M = N + {#b#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2947
shows "\<exists> M1 a. M = M1 + {#a#} \<and> f a = b \<and> mmap f M1 = N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2948
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2949
  obtain a where a: "a \<in># M" and fa: "f a = b"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2950
  using multiset.set_map[of f M] unfolding assms
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2951
  by (metis image_iff mem_set_of_iff union_single_eq_member)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2952
  then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2953
  have "mmap f M1 = N" using assms unfolding M fa[symmetric] by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2954
  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
  2955
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2956
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2957
lemma msed_rel_invL:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2958
assumes "rel_multiset R (M + {#a#}) N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2959
shows "\<exists> N1 b. N = N1 + {#b#} \<and> R a b \<and> rel_multiset R M N1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2960
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2961
  obtain K where KM: "mmap fst K = M + {#a#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2962
  and KN: "mmap snd K = N" and sK: "set_of K \<subseteq> {(a, b). R a b}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2963
  using assms
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2964
  unfolding rel_multiset_def Grp_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2965
  obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2966
  and K1M: "mmap fst K1 = M" using msed_map_invR[OF KM] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2967
  obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "mmap snd K1 = N1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2968
  using msed_map_invL[OF KN[unfolded K]] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2969
  have Rab: "R a (snd ab)" using sK a unfolding K by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2970
  have "rel_multiset R M N1" using sK K1M K1N1
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2971
  unfolding K rel_multiset_def Grp_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2972
  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
  2973
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2974
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2975
lemma msed_rel_invR:
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2976
assumes "rel_multiset R M (N + {#b#})"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2977
shows "\<exists> M1 a. M = M1 + {#a#} \<and> R a b \<and> rel_multiset R M1 N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2978
proof-
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2979
  obtain K where KN: "mmap snd K = N + {#b#}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2980
  and KM: "mmap fst K = M" and sK: "set_of K \<subseteq> {(a, b). R a b}"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2981
  using assms
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2982
  unfolding rel_multiset_def Grp_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2983
  obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2984
  and K1N: "mmap snd K1 = N" using msed_map_invR[OF KN] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2985
  obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "mmap fst K1 = M1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2986
  using msed_map_invL[OF KM[unfolded K]] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2987
  have Rab: "R (fst ab) b" using sK b unfolding K by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2988
  have "rel_multiset R M1 N" using sK K1N K1M1
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2989
  unfolding K rel_multiset_def Grp_def by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2990
  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
  2991
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2992
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2993
lemma rel_multiset_imp_rel_multiset':
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2994
assumes "rel_multiset R M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2995
shows "rel_multiset' R M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2996
using assms proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2997
  case (less M)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2998
  have c: "mcard M = mcard N" using rel_multiset_mcard[OF less.prems] .
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  2999
  show ?case
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3000
  proof(cases "M = {#}")
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3001
    case True hence "N = {#}" using c by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3002
    thus ?thesis using True rel_multiset'.Zero by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3003
  next
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3004
    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3005
    obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_multiset R M1 N1"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3006
    using msed_rel_invL[OF less.prems[unfolded M]] by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3007
    have "rel_multiset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3008
    thus ?thesis using rel_multiset'.Plus[of R a b, OF R] unfolding M N by simp
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3009
  qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3010
qed
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3011
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3012
lemma rel_multiset_rel_multiset':
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3013
"rel_multiset R M N = rel_multiset' R M N"
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3014
using  rel_multiset_imp_rel_multiset' rel_multiset'_imp_rel_multiset by auto
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3015
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3016
(* The main end product for rel_multiset: inductive characterization *)
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3017
theorems rel_multiset_induct[case_names empty add, induct pred: rel_multiset] =
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3018
         rel_multiset'.induct[unfolded rel_multiset_rel_multiset'[symmetric]]
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3019
26bd1cba3ab5 killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents: 54868
diff changeset
  3020
end