src/HOL/Library/Multiset.thy
author wenzelm
Sat, 07 Apr 2012 16:41:59 +0200
changeset 47389 e8552cba702d
parent 47308 9caab698dbe4
child 47429 ec64d94cbf9c
permissions -rw-r--r--
explicit checks stable_finished_theory/stable_command allow parallel asynchronous command transactions; tuned;
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
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     3
*)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     4
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
     5
header {* (Finite) multisets *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
     6
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
     7
theory Multiset
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
     8
imports Main DAList
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15072
diff changeset
     9
begin
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    10
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    11
subsection {* The type of multisets *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    12
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    13
definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    14
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    15
typedef (open) '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
    16
  morphisms count Abs_multiset
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    17
  unfolding multiset_def
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    18
proof
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45608
diff changeset
    19
  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
    20
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    21
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    22
lemmas multiset_typedef = Abs_multiset_inverse count_inverse count
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18730
diff changeset
    23
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
    24
abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
    25
  "a :# M == 0 < count M a"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
    26
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    27
notation (xsymbols)
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
    28
  Melem (infix "\<in>#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    29
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    30
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
    31
  "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
    32
  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
    33
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
    34
lemma multiset_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
    35
  "(\<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
    36
  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
    37
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
    39
 \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
    40
*}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
    43
  "(\<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
    44
  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
    45
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    46
lemma 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
    47
  "(\<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
    48
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    49
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    50
lemma 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
    51
  "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
    52
  by (simp add: multiset_def)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    53
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    54
lemma 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
    55
  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
    56
  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
    57
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
    58
  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
    59
    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
    60
  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
    61
    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
    62
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
    63
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
    64
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
    65
  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
    66
  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
    67
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
    68
  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
    69
    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
    70
  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
    71
    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
    72
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
    73
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
    75
  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
    76
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
    79
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
    81
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
instantiation multiset :: (type) "{zero, plus}"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    83
begin
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    84
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    85
definition Mempty_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
    86
  "0 = Abs_multiset (\<lambda>a. 0)"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    87
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    88
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
    89
  "Mempty \<equiv> 0"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    90
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    91
definition union_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
    92
  "M + N = Abs_multiset (\<lambda>a. count M a + count N a)"
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    93
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    94
instance ..
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    95
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25507
diff changeset
    96
end
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
    97
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
    98
definition single :: "'a => '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
    99
  "single a = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   100
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   101
syntax
26176
038baad81209 tuned syntax declaration;
wenzelm
parents: 26145
diff changeset
   102
  "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
25507
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   103
translations
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   104
  "{#x, xs#}" == "{#x#} + {#xs#}"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   105
  "{#x#}" == "CONST single x"
d13468d40131 added {#.,.,...#}
nipkow
parents: 25208
diff changeset
   106
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   107
lemma count_empty [simp]: "count {#} 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
   108
  by (simp add: Mempty_def in_multiset multiset_typedef)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   109
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   110
lemma count_single [simp]: "count {#b#} a = (if b = a 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
   111
  by (simp add: single_def in_multiset multiset_typedef)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29509
diff changeset
   112
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   113
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   114
subsection {* Basic operations *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   115
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   116
subsubsection {* Union *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   117
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   118
lemma count_union [simp]: "count (M + N) a = count M a + count 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
   119
  by (simp add: union_def in_multiset multiset_typedef)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   120
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   121
instance multiset :: (type) cancel_comm_monoid_add
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   122
  by default (simp_all add: multiset_eq_iff)
10277
081c8641aa11 improved typedef;
wenzelm
parents: 10249
diff changeset
   123
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   124
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   125
subsubsection {* Difference *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   126
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
instantiation multiset :: (type) minus
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   129
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   130
definition diff_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
   131
  "M - N = Abs_multiset (\<lambda>a. count M a - count 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
   132
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   133
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
   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"
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   138
  by (simp add: diff_def in_multiset multiset_typedef)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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 = {#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   141
by(simp add: multiset_eq_iff)
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 = {#}"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   144
by (rule multiset_eqI) simp
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)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   147
by(simp add: multiset_eq_iff)
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)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   150
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
   151
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   152
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
   153
  "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
   154
  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
   155
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   156
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
   157
  "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
   158
  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
   159
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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 diff_right_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
   161
  "(M::'a multiset) - N - Q = M - Q - 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
   162
  by (auto simp add: multiset_eq_iff)
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   163
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   164
lemma diff_add:
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   165
  "(M::'a multiset) - (N + Q) = M - N - Q"
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 (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
   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
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   261
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   262
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
   263
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   264
instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   265
begin
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   266
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   267
definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   268
  mset_le_def: "A \<le> B \<longleftrightarrow> (\<forall>a. count A a \<le> count B 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
   269
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   270
definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   271
  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
   272
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   273
instance
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   274
  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
   275
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   276
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
   277
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   278
lemma mset_less_eqI:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   279
  "(\<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
   280
  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
   281
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   282
lemma mset_le_exists_conv:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   283
  "(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
   284
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
   285
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
   286
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
   287
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   288
lemma mset_le_mono_add_right_cancel [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   289
  "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   290
  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
   291
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   292
lemma mset_le_mono_add_left_cancel [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   293
  "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   294
  by (fact add_le_cancel_left)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   295
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   296
lemma mset_le_mono_add:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   297
  "(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
   298
  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
   299
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   300
lemma mset_le_add_left [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   301
  "(A::'a multiset) \<le> A + B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   302
  unfolding mset_le_def by auto
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_add_right [simp]:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   305
  "B \<le> (A::'a multiset) + B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   306
  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
   307
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   308
lemma mset_le_single:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   309
  "a :# B \<Longrightarrow> {#a#} \<le> B"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   310
  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
   311
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   312
lemma multiset_diff_union_assoc:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   313
  "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
   314
  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
   315
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   316
lemma mset_le_multiset_union_diff_commute:
36867
6c28c702ed22 simplified proof
nipkow
parents: 36635
diff changeset
   317
  "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
   318
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
   319
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
   320
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M"
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
   321
by(simp add: mset_le_def)
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
   322
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   323
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
   324
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
   325
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
   326
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
   327
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
   328
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   329
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
   330
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
   331
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
   332
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
   333
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
   334
  
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   335
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
   336
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
   337
 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
   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 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
   340
 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
   341
 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
   342
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
   343
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   344
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
   345
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
   346
 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
   347
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
   348
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
   349
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   350
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
   351
  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
   352
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   353
lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   354
  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
   355
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   356
lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   357
  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
   358
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   359
lemma mset_less_add_bothsides:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   360
  "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   361
  by (fact add_less_imp_less_right)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   362
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   363
lemma mset_less_empty_nonempty:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   364
  "{#} < S \<longleftrightarrow> S \<noteq> {#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   365
  by (auto simp: mset_le_def mset_less_def)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   366
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   367
lemma mset_less_diff_self:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   368
  "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
   369
  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
   370
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   371
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   372
subsubsection {* Intersection *}
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   373
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   374
instantiation multiset :: (type) semilattice_inf
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   375
begin
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
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
   378
  multiset_inter_def: "inf_multiset A B = A - (A - B)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   379
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   380
instance
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   381
proof -
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   382
  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
   383
  show "OFCLASS('a multiset, semilattice_inf_class)"
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   384
    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
   385
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   386
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   387
end
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   388
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   389
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
   390
  "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
   391
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   392
lemma multiset_inter_count [simp]:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   393
  "count (A #\<inter> B) x = min (count A x) (count B x)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   394
  by (simp add: multiset_inter_def multiset_typedef)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   395
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   396
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   397
  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
   398
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   399
lemma multiset_union_diff_commute:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   400
  assumes "B #\<inter> C = {#}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   401
  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
   402
proof (rule multiset_eqI)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   403
  fix x
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   404
  from assms have "min (count B x) (count C x) = 0"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   405
    by (auto simp add: multiset_eq_iff)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   406
  then have "count B x = 0 \<or> count C x = 0"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   407
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   408
  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
   409
    by auto
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   410
qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   411
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   412
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   413
subsubsection {* Filter (with comprehension syntax) *}
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   414
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   415
text {* Multiset comprehension *}
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   416
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   417
definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   418
  "filter P M = Abs_multiset (\<lambda>x. if P x then count M x else 0)"
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   419
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   420
hide_const (open) filter
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   421
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   422
lemma count_filter [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   423
  "count (Multiset.filter P M) a = (if P a then count M a else 0)"
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   424
  by (simp add: filter_def in_multiset multiset_typedef)
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   425
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   426
lemma filter_empty [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   427
  "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
   428
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   429
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   430
lemma filter_single [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   431
  "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
   432
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   433
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   434
lemma filter_union [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   435
  "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
   436
  by (rule multiset_eqI) simp
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   437
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   438
lemma filter_diff [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   439
  "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
   440
  by (rule multiset_eqI) simp
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   441
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   442
lemma filter_inter [simp]:
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   443
  "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
   444
  by (rule multiset_eqI) simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   445
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   446
syntax
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   447
  "_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
   448
syntax (xsymbol)
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   449
  "_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
   450
translations
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   451
  "{#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
   452
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   453
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   454
subsubsection {* Set of elements *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   455
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   456
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
   457
  "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
   458
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   459
lemma set_of_empty [simp]: "set_of {#} = {}"
26178
nipkow
parents: 26176
diff changeset
   460
by (simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   461
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   462
lemma set_of_single [simp]: "set_of {#b#} = {b}"
26178
nipkow
parents: 26176
diff changeset
   463
by (simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   464
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   465
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
26178
nipkow
parents: 26176
diff changeset
   466
by (auto simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   467
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   468
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
   469
by (auto simp add: set_of_def multiset_eq_iff)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   470
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   471
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
26178
nipkow
parents: 26176
diff changeset
   472
by (auto simp add: set_of_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   473
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   474
lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
26178
nipkow
parents: 26176
diff changeset
   475
by (auto simp add: set_of_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   476
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   477
lemma 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
   478
  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
   479
46756
faf62905cd53 adding finiteness of intervals on integer sets; adding another finiteness theorem for multisets
bulwahn
parents: 46730
diff changeset
   480
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
   481
  unfolding set_of_def[symmetric] by simp
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   482
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   483
subsubsection {* Size *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   484
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   485
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
   486
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
   487
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   488
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
   489
  "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
   490
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   491
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
   492
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   493
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
   494
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   495
lemma size_empty [simp]: "size {#} = 0"
26178
nipkow
parents: 26176
diff changeset
   496
by (simp add: size_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   497
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   498
lemma size_single [simp]: "size {#b#} = 1"
26178
nipkow
parents: 26176
diff changeset
   499
by (simp add: size_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   500
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   501
lemma setsum_count_Int:
26178
nipkow
parents: 26176
diff changeset
   502
  "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
nipkow
parents: 26176
diff changeset
   503
apply (induct rule: finite_induct)
nipkow
parents: 26176
diff changeset
   504
 apply simp
nipkow
parents: 26176
diff changeset
   505
apply (simp add: Int_insert_left set_of_def)
nipkow
parents: 26176
diff changeset
   506
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   507
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   508
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
26178
nipkow
parents: 26176
diff changeset
   509
apply (unfold size_def)
nipkow
parents: 26176
diff changeset
   510
apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
nipkow
parents: 26176
diff changeset
   511
 prefer 2
nipkow
parents: 26176
diff changeset
   512
 apply (rule ext, simp)
nipkow
parents: 26176
diff changeset
   513
apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
nipkow
parents: 26176
diff changeset
   514
apply (subst Int_commute)
nipkow
parents: 26176
diff changeset
   515
apply (simp (no_asm_simp) add: setsum_count_Int)
nipkow
parents: 26176
diff changeset
   516
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   517
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   518
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
   519
by (auto simp add: size_def multiset_eq_iff)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   520
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   521
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
26178
nipkow
parents: 26176
diff changeset
   522
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
   523
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   524
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
26178
nipkow
parents: 26176
diff changeset
   525
apply (unfold size_def)
nipkow
parents: 26176
diff changeset
   526
apply (drule setsum_SucD)
nipkow
parents: 26176
diff changeset
   527
apply auto
nipkow
parents: 26176
diff changeset
   528
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   529
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   530
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
   531
  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
   532
  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
   533
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
   534
  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
   535
    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
   536
  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
   537
  then show ?thesis by blast
23611
65b168646309 more interpretations
nipkow
parents: 23373
diff changeset
   538
qed
15869
3aca7f05cd12 intersection
kleing
parents: 15867
diff changeset
   539
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   540
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
   541
subsection {* Induction and case splits *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   542
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   543
lemma setsum_decr:
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11655
diff changeset
   544
  "finite F ==> (0::nat) < f a ==>
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
   545
    setsum (f (a := f a - 1)) F = (if a\<in>F then setsum f F - 1 else setsum f F)"
26178
nipkow
parents: 26176
diff changeset
   546
apply (induct rule: finite_induct)
nipkow
parents: 26176
diff changeset
   547
 apply auto
nipkow
parents: 26176
diff changeset
   548
apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow
parents: 26176
diff changeset
   549
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   550
10313
51e830bb7abe intro_classes by default;
wenzelm
parents: 10277
diff changeset
   551
lemma rep_multiset_induct_aux:
26178
nipkow
parents: 26176
diff changeset
   552
assumes 1: "P (\<lambda>a. (0::nat))"
nipkow
parents: 26176
diff changeset
   553
  and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))"
nipkow
parents: 26176
diff changeset
   554
shows "\<forall>f. f \<in> multiset --> setsum f {x. f x \<noteq> 0} = n --> P f"
nipkow
parents: 26176
diff changeset
   555
apply (unfold multiset_def)
nipkow
parents: 26176
diff changeset
   556
apply (induct_tac n, simp, clarify)
nipkow
parents: 26176
diff changeset
   557
 apply (subgoal_tac "f = (\<lambda>a.0)")
nipkow
parents: 26176
diff changeset
   558
  apply simp
nipkow
parents: 26176
diff changeset
   559
  apply (rule 1)
nipkow
parents: 26176
diff changeset
   560
 apply (rule ext, force, clarify)
nipkow
parents: 26176
diff changeset
   561
apply (frule setsum_SucD, clarify)
nipkow
parents: 26176
diff changeset
   562
apply (rename_tac a)
nipkow
parents: 26176
diff changeset
   563
apply (subgoal_tac "finite {x. (f (a := f a - 1)) x > 0}")
nipkow
parents: 26176
diff changeset
   564
 prefer 2
nipkow
parents: 26176
diff changeset
   565
 apply (rule finite_subset)
nipkow
parents: 26176
diff changeset
   566
  prefer 2
nipkow
parents: 26176
diff changeset
   567
  apply assumption
nipkow
parents: 26176
diff changeset
   568
 apply simp
nipkow
parents: 26176
diff changeset
   569
 apply blast
nipkow
parents: 26176
diff changeset
   570
apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
nipkow
parents: 26176
diff changeset
   571
 prefer 2
nipkow
parents: 26176
diff changeset
   572
 apply (rule ext)
nipkow
parents: 26176
diff changeset
   573
 apply (simp (no_asm_simp))
nipkow
parents: 26176
diff changeset
   574
 apply (erule ssubst, rule 2 [unfolded multiset_def], blast)
nipkow
parents: 26176
diff changeset
   575
apply (erule allE, erule impE, erule_tac [2] mp, blast)
nipkow
parents: 26176
diff changeset
   576
apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
nipkow
parents: 26176
diff changeset
   577
apply (subgoal_tac "{x. x \<noteq> a --> f x \<noteq> 0} = {x. f x \<noteq> 0}")
nipkow
parents: 26176
diff changeset
   578
 prefer 2
nipkow
parents: 26176
diff changeset
   579
 apply blast
nipkow
parents: 26176
diff changeset
   580
apply (subgoal_tac "{x. x \<noteq> a \<and> f x \<noteq> 0} = {x. f x \<noteq> 0} - {a}")
nipkow
parents: 26176
diff changeset
   581
 prefer 2
nipkow
parents: 26176
diff changeset
   582
 apply blast
nipkow
parents: 26176
diff changeset
   583
apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong)
nipkow
parents: 26176
diff changeset
   584
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   585
10313
51e830bb7abe intro_classes by default;
wenzelm
parents: 10277
diff changeset
   586
theorem rep_multiset_induct:
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
   587
  "f \<in> multiset ==> P (\<lambda>a. 0) ==>
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11655
diff changeset
   588
    (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
26178
nipkow
parents: 26176
diff changeset
   589
using rep_multiset_induct_aux by blast
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   590
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
   591
theorem multiset_induct [case_names empty add, induct type: multiset]:
26178
nipkow
parents: 26176
diff changeset
   592
assumes empty: "P {#}"
nipkow
parents: 26176
diff changeset
   593
  and add: "!!M x. P M ==> P (M + {#x#})"
nipkow
parents: 26176
diff changeset
   594
shows "P M"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   595
proof -
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   596
  note defns = union_def single_def Mempty_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
   597
  note add' = add [unfolded defns, simplified]
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   598
  have aux: "\<And>a::'a. count (Abs_multiset (\<lambda>b. if b = a 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
   599
    (\<lambda>b. if b = a then 1 else 0)" by (simp add: Abs_multiset_inverse in_multiset) 
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   600
  show ?thesis
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   601
    apply (rule count_inverse [THEN subst])
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   602
    apply (rule count [THEN rep_multiset_induct])
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
   603
     apply (rule empty [unfolded defns])
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
   604
    apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))")
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   605
     prefer 2
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
   606
     apply (simp add: fun_eq_iff)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   607
    apply (erule ssubst)
17200
3a4d03d1a31b tuned presentation;
wenzelm
parents: 17161
diff changeset
   608
    apply (erule Abs_multiset_inverse [THEN subst])
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   609
    apply (drule add')
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   610
    apply (simp add: aux)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   611
    done
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   612
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   613
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   614
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
26178
nipkow
parents: 26176
diff changeset
   615
by (induct M) auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   616
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   617
lemma multiset_cases [cases type, case_names empty add]:
26178
nipkow
parents: 26176
diff changeset
   618
assumes em:  "M = {#} \<Longrightarrow> P"
nipkow
parents: 26176
diff changeset
   619
assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P"
nipkow
parents: 26176
diff changeset
   620
shows "P"
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   621
proof (cases "M = {#}")
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   622
  assume "M = {#}" then show ?thesis using em by simp
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   623
next
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   624
  assume "M \<noteq> {#}"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   625
  then obtain M' m where "M = M' + {#m#}" 
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   626
    by (blast dest: multi_nonempty_split)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   627
  then show ?thesis using add by simp
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   628
qed
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   629
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   630
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
26178
nipkow
parents: 26176
diff changeset
   631
apply (cases M)
nipkow
parents: 26176
diff changeset
   632
 apply simp
nipkow
parents: 26176
diff changeset
   633
apply (rule_tac x="M - {#x#}" in exI, simp)
nipkow
parents: 26176
diff changeset
   634
done
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
   635
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   636
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
   637
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
   638
26033
278025d5282d modified MCollect syntax
nipkow
parents: 26016
diff changeset
   639
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
   640
apply (subst multiset_eq_iff)
26178
nipkow
parents: 26176
diff changeset
   641
apply auto
nipkow
parents: 26176
diff changeset
   642
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   643
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   644
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
   645
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
   646
  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
   647
  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
   648
  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
   649
    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
   650
  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
   651
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
   652
  case (add S x T)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   653
  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
   654
  have SxsubT: "S + {#x#} < T" by fact
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   655
  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
   656
  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
   657
    by (blast dest: multi_member_split)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   658
  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
   659
    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
   660
  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
   661
  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
   662
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
   663
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   666
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   668
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   670
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   672
  mset_less_rel :: "('a multiset * 'a multiset) set" where
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   673
  "mset_less_rel = {(A,B). A < B}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
   674
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   676
  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
   677
  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
   678
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
   679
  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
   680
    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
   681
  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
   682
  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
   683
    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
   684
  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
   685
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
   686
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   688
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
   689
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
   690
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
   691
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
   692
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   693
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
   694
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
lemma full_multiset_induct [case_names less]:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   696
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
   697
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
   698
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
   699
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
   700
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
   701
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
lemma multi_subset_induct [consumes 2, case_names empty add]:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   703
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
   704
  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
   705
  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
   706
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
   707
proof -
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   708
  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
   709
  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
   710
  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
   711
    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
   712
  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
   713
    fix x F
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
   714
    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
   715
    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
   716
    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
   717
      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
   718
      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
   719
      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
   720
    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
   721
  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
   722
qed
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
   723
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
   724
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
subsection {* Alternative representations *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   727
subsubsection {* Lists *}
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and 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
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
   730
  "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
   731
  "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
   732
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   733
lemma in_multiset_in_set:
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   734
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   735
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   736
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   737
lemma count_multiset_of:
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   738
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   739
  by (induct xs) simp_all
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   740
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   741
lemma 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
   742
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
   743
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   744
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
   745
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
   746
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   747
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
   748
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
   749
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   750
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
   751
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
   752
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   753
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
   754
  "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
   755
  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
   756
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   757
lemma multiset_of_filter:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   758
  "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
   759
  by (induct xs) simp_all
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   760
40950
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   761
lemma multiset_of_rev [simp]:
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   762
  "multiset_of (rev xs) = multiset_of xs"
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   763
  by (induct xs) simp_all
a370b0fb6f09 lemma multiset_of_rev
haftmann
parents: 40606
diff changeset
   764
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   765
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
   766
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
   767
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
   768
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
   769
 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
   770
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
   771
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
   772
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
   773
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   774
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
   775
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
   776
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   777
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
   778
  "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
   779
apply (induct x, simp, rule iffI, simp_all)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   780
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
   781
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
   782
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
   783
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
   784
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
   785
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   786
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
   787
  "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
   788
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
   789
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   790
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
   791
  "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
   792
    (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
   793
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
   794
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   795
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
   796
   "(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
   797
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
   798
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
   799
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
   800
      [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
   801
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
   802
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
   803
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   804
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
   805
  "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
   806
  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
   807
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   808
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
   809
  "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
   810
  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
   811
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   812
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
   813
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
   814
 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
   815
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
   816
 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
   817
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
   818
36903
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   819
lemma multiset_of_remove1[simp]:
489c1fbbb028 Multiset: renamed, added and tuned lemmas;
nipkow
parents: 36867
diff changeset
   820
  "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
   821
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
   822
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   823
lemma multiset_of_eq_length:
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   824
  assumes "multiset_of xs = multiset_of ys"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   825
  shows "length xs = length ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   826
using assms
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   827
proof (induct xs arbitrary: ys)
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   828
  case Nil then show ?case by simp
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   829
next
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   830
  case (Cons x xs)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   831
  then have "x \<in># multiset_of ys" by (simp add: union_single_eq_member)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   832
  then have "x \<in> set ys" by (simp add: in_multiset_in_set)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   833
  from Cons.prems [symmetric] have "multiset_of xs = multiset_of (remove1 x ys)"
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   834
    by simp
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   835
  with Cons.hyps have "length xs = length (remove1 x ys)" .
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   836
  with `x \<in> set ys` show ?case
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   837
    by (auto simp add: length_remove1 dest: length_pos_if_in_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
   838
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
   839
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   840
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
   841
  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
   842
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   843
proof (cases "z \<in># multiset_of xs")
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   844
  case False
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   845
  moreover have "\<not> z \<in># multiset_of ys" using assms False by simp
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
   846
  ultimately show ?thesis by (simp add: 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
   847
next
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   848
  case True
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   849
  moreover have "z \<in># multiset_of ys" using assms True by simp
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   850
  show ?thesis using assms
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   851
  proof (induct xs arbitrary: ys)
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   852
    case Nil then show ?case by simp
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   853
  next
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   854
    case (Cons x xs)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   855
    from `multiset_of (x # xs) = multiset_of ys` [symmetric]
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   856
      have *: "multiset_of xs = multiset_of (remove1 x ys)"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   857
      and "x \<in> set ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   858
      by (auto simp add: mem_set_multiset_eq)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   859
    from * have "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) (remove1 x ys))" by (rule Cons.hyps)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   860
    moreover from `x \<in> set ys` have "length (filter (\<lambda>y. x = y) ys) > 0" by (simp add: filter_empty_conv)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   861
    ultimately show ?case using `x \<in> set ys`
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   862
      by (simp add: filter_remove1) (auto simp add: length_remove1)
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   863
  qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   864
qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   865
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   866
lemma fold_multiset_equiv:
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   867
  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
   868
    and equiv: "multiset_of xs = multiset_of ys"
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   869
  shows "fold f xs = fold f ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   870
using f equiv [symmetric]
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   871
proof (induct xs arbitrary: ys)
45989
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   872
  case Nil then show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   873
next
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   874
  case (Cons x xs)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   875
  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
   876
  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
   877
    by (rule Cons.prems(1)) (simp_all add: *)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   878
  moreover from * have "x \<in> set ys" by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   879
  ultimately have "fold f ys = fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   880
  moreover from Cons.prems have "fold f xs = fold f (remove1 x ys)" by (auto intro: Cons.hyps)
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   881
  ultimately show ?case by simp
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   882
qed
b39256df5f8a moved theorem requiring multisets from More_List to Multiset
haftmann
parents: 45866
diff changeset
   883
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   884
context linorder
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   885
begin
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   886
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
   887
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
   888
  "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   889
  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
   890
 
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
   891
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
   892
  "multiset_of (sort_key k xs) = multiset_of xs"
37107
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   893
  by (induct xs) (simp_all add: ac_simps)
1535aa1c943a more lemmas
haftmann
parents: 37074
diff changeset
   894
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
   895
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
   896
  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
   897
  @{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
   898
*}
37074
322d065ebef7 localized properties_for_sort
haftmann
parents: 36903
diff changeset
   899
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   900
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
   901
  assumes "multiset_of ys = multiset_of xs"
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   902
  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
   903
  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
   904
  shows "sort_key f xs = ys"
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   905
using assms
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
   906
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
   907
  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
   908
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
   909
  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
   910
  from Cons.prems(2) have
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   911
    "\<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
   912
    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
   913
  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
   914
    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
   915
  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
   916
    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
   917
  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
   918
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
   919
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   920
lemma properties_for_sort:
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   921
  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
   922
  and "sorted ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   923
  shows "sort xs = ys"
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   924
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
   925
  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
   926
  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
   927
  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
   928
    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
   929
  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
   930
    by simp
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   931
  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
   932
    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
   933
qed
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
   934
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   935
lemma sort_key_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   936
  "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
   937
    @ [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
   938
    @ 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
   939
proof (rule properties_for_sort_key)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   940
  show "multiset_of ?rhs = multiset_of ?lhs"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   941
    by (rule multiset_eqI) (auto simp add: multiset_of_filter)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   942
next
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   943
  show "sorted (map f ?rhs)"
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   944
    by (auto simp add: sorted_append intro: sorted_map_same)
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   945
next
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   946
  fix l
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   947
  assume "l \<in> set ?rhs"
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
   948
  let ?pivot = "f (xs ! (length xs div 2))"
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
   949
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
   950
  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
   951
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
   952
  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
   953
  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
   954
  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
   955
    [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
   956
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
   957
  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
   958
  proof (cases "f l" ?pivot rule: linorder_cases)
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   959
    case less
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   960
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   961
    with less show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
   962
      by (simp add: filter_sort [symmetric] ** ***)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   963
  next
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
   964
    case equal then show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
   965
      by (simp add: * less_le)
40305
41833242cc42 tuned lemma proposition of properties_for_sort_key
haftmann
parents: 40303
diff changeset
   966
  next
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   967
    case greater
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   968
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
   969
    with greater show ?thesis
40346
58af2b8327b7 tuned proof
haftmann
parents: 40307
diff changeset
   970
      by (simp add: filter_sort [symmetric] ** ***)
40306
e4461b9854a5 tuned proof
haftmann
parents: 40305
diff changeset
   971
  qed
40303
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   972
qed
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   973
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   974
lemma sort_by_quicksort:
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   975
  "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
   976
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
2d507370e879 lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents: 40250
diff changeset
   977
    @ 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
   978
  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
   979
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   980
text {* A stable parametrized quicksort *}
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   981
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   982
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
   983
  "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
   984
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   985
lemma part_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   986
  "part f pivot [] = ([], [], [])"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   987
  "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
   988
     if x' < pivot then (x # lts, eqs, gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   989
     else if x' > pivot then (lts, eqs, x # gts)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   990
     else (lts, x # eqs, gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   991
  by (auto simp add: part_def Let_def split_def)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   992
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   993
lemma sort_key_by_quicksort_code [code]:
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   994
  "sort_key f xs = (case xs of [] \<Rightarrow> []
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   995
    | [x] \<Rightarrow> xs
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   996
    | [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
   997
    | _ \<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
   998
       in sort_key f lts @ eqs @ sort_key f gts))"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
   999
proof (cases xs)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1000
  case Nil then show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1001
next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1002
  case (Cons _ ys) note hyps = Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1003
  proof (cases ys)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1004
    case Nil with hyps show ?thesis by simp
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1005
  next
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1006
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1007
    proof (cases zs)
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1008
      case Nil with hyps show ?thesis by auto
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1009
    next
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1010
      case Cons 
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1011
      from sort_key_by_quicksort [of f xs]
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1012
      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
  1013
        in sort_key f lts @ eqs @ sort_key f gts)"
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1014
      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
  1015
      with hyps Cons show ?thesis by (simp only: list.cases)
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1016
    qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1017
  qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1018
qed
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1019
39533
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1020
end
91a0ff0ff237 generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents: 39314
diff changeset
  1021
40347
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1022
hide_const (open) part
429bf4388b2f added code lemmas for stable parametrized quicksort
haftmann
parents: 40346
diff changeset
  1023
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1024
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
  1025
  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
  1026
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1027
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
  1028
  "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
  1029
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
  1030
  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
  1031
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
  1032
  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
  1033
  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
  1034
  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
  1035
    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
  1036
  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
  1037
    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
  1038
    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
  1039
      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
  1040
      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
  1041
      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
  1042
      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
  1043
      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
  1044
      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
  1045
      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
  1046
      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
  1047
  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
  1048
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
  1049
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1050
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
  1051
  "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
  1052
    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
  1053
  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
  1054
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1055
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1056
subsubsection {* Association lists -- including code generation *}
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1057
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1058
text {* Preliminaries *}
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1059
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1060
text {* Raw operations on lists *}
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1061
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1062
definition join_raw :: "('key \<Rightarrow> 'val \<times> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1063
where
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1064
  "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (%v'. f k (v', v))) ys xs"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1065
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1066
lemma join_raw_Nil [simp]:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1067
  "join_raw f xs [] = xs"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1068
by (simp add: join_raw_def)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1069
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1070
lemma join_raw_Cons [simp]:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1071
  "join_raw f xs ((k, v) # ys) = map_default k v (%v'. f k (v', v)) (join_raw f xs ys)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1072
by (simp add: join_raw_def)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1073
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1074
lemma map_of_join_raw:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1075
  assumes "distinct (map fst ys)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1076
  shows "map_of (join_raw f xs ys) x = (case map_of xs x of None => map_of ys x | Some v => (case map_of ys x of None => Some v | Some v' => Some (f x (v, v'))))"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1077
using assms
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1078
apply (induct ys)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1079
apply (auto simp add: map_of_map_default split: option.split)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1080
apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1081
by (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2))
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1082
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1083
lemma distinct_join_raw:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1084
  assumes "distinct (map fst xs)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1085
  shows "distinct (map fst (join_raw f xs ys))"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1086
using assms
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1087
proof (induct ys)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1088
  case (Cons y ys)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1089
  thus ?case by (cases y) (simp add: distinct_map_default)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1090
qed auto
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1091
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1092
definition
46238
9ace9e5b79be renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents: 46237
diff changeset
  1093
  "subtract_entries_raw xs ys = foldr (%(k, v). AList.map_entry k (%v'. v' - v)) ys xs"
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1094
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1095
lemma map_of_subtract_entries_raw:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1096
  "distinct (map fst ys) ==> map_of (subtract_entries_raw xs ys) x = (case map_of xs x of None => None | Some v => (case map_of ys x of None => Some v | Some v' => Some (v - v')))"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1097
unfolding subtract_entries_raw_def
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1098
apply (induct ys)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1099
apply auto
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1100
apply (simp split: option.split)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1101
apply (simp add: map_of_map_entry)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1102
apply (auto split: option.split)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1103
apply (metis map_of_eq_None_iff option.simps(3) option.simps(4))
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1104
by (metis map_of_eq_None_iff option.simps(4) option.simps(5))
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1105
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1106
lemma distinct_subtract_entries_raw:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1107
  assumes "distinct (map fst xs)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1108
  shows "distinct (map fst (subtract_entries_raw xs ys))"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1109
using assms
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1110
unfolding subtract_entries_raw_def by (induct ys) (auto simp add: distinct_map_entry)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1111
47179
54b38de0620e changing more definitions to quotient_definition
bulwahn
parents: 47177
diff changeset
  1112
text {* Operations on alists with distinct keys *}
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1113
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47198
diff changeset
  1114
lift_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist" 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47198
diff changeset
  1115
is join_raw
47179
54b38de0620e changing more definitions to quotient_definition
bulwahn
parents: 47177
diff changeset
  1116
by (simp add: distinct_join_raw)
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1117
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47198
diff changeset
  1118
lift_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 47198
diff changeset
  1119
is subtract_entries_raw 
47179
54b38de0620e changing more definitions to quotient_definition
bulwahn
parents: 47177
diff changeset
  1120
by (simp add: distinct_subtract_entries_raw)
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1121
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1122
text {* Implementing multisets by means of association lists *}
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1123
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1124
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" 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
  1125
  "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> 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
  1126
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1127
lemma count_of_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
  1128
  "count_of xs \<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
  1129
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
  1130
  let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> 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
  1131
  have "?A \<subseteq> dom (map_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
  1132
  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
  1133
    fix 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
  1134
    assume "x \<in> ?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
  1135
    then have "0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)" 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
  1136
    then have "map_of xs x \<noteq> None" by (cases "map_of xs 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
  1137
    then show "x \<in> dom (map_of xs)" 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
  1138
  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
  1139
  with finite_dom_map_of [of xs] have "finite ?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
  1140
    by (auto 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
  1141
  then show ?thesis
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1142
    by (simp add: count_of_def fun_eq_iff 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
  1143
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
  1144
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1145
lemma count_simps [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
  1146
  "count_of [] = (\<lambda>_. 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
  1147
  "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs 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
  1148
  by (simp_all add: count_of_def 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
  1149
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1150
lemma count_of_empty:
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1151
  "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 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
  1152
  by (induct xs) (simp_all add: count_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
  1153
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1154
lemma count_of_filter:
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1155
  "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1156
  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
  1157
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1158
lemma count_of_map_default [simp]:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1159
  "count_of (map_default x b (%x. x + b) xs) y = (if x = y then count_of xs x + b else count_of xs y)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1160
unfolding count_of_def by (simp add: map_of_map_default split: option.split)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1161
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1162
lemma count_of_join_raw:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1163
  "distinct (map fst ys) ==> count_of xs x + count_of ys x = count_of (join_raw (%x (x, y). x + y) xs ys) x"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1164
unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1165
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1166
lemma count_of_subtract_entries_raw:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1167
  "distinct (map fst ys) ==> count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1168
unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1169
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1170
text {* Code equations for multiset operations *}
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1171
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1172
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset" where
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
  1173
  "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1174
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1175
code_datatype Bag
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1176
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1177
lemma count_Bag [simp, code]:
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
  1178
  "count (Bag xs) = count_of (DAList.impl_of xs)"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1179
  by (simp add: Bag_def count_of_multiset Abs_multiset_inverse)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1180
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1181
lemma Mempty_Bag [code]:
46394
68f973ffd763 improving code equations for multisets that violated the distinct AList abstraction
bulwahn
parents: 46238
diff changeset
  1182
  "{#} = Bag (DAList.empty)"
68f973ffd763 improving code equations for multisets that violated the distinct AList abstraction
bulwahn
parents: 46238
diff changeset
  1183
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
47143
212f7a975d49 association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents: 46921
diff changeset
  1184
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1185
lemma single_Bag [code]:
46394
68f973ffd763 improving code equations for multisets that violated the distinct AList abstraction
bulwahn
parents: 46238
diff changeset
  1186
  "{#x#} = Bag (DAList.update x 1 DAList.empty)"
47198
cfd8ff62eab1 use qualified names for rsp and rep_eq theorems in quotient_def
kuncar
parents: 47179
diff changeset
  1187
  by (simp add: multiset_eq_iff alist.Alist_inverse update.rep_eq empty.rep_eq)
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1188
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1189
lemma union_Bag [code]:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1190
  "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1191
by (rule multiset_eqI) (simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1192
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1193
lemma minus_Bag [code]:
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1194
  "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1195
by (rule multiset_eqI)
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1196
  (simp add: count_of_subtract_entries_raw alist.Alist_inverse distinct_subtract_entries_raw subtract_entries_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
  1197
41069
6fabc0414055 name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents: 40968
diff changeset
  1198
lemma filter_Bag [code]:
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
  1199
  "Multiset.filter P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
47198
cfd8ff62eab1 use qualified names for rsp and rep_eq theorems in quotient_def
kuncar
parents: 47179
diff changeset
  1200
by (rule multiset_eqI) (simp add: count_of_filter filter.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
  1201
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1202
lemma mset_less_eq_Bag [code]:
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
  1203
  "Bag xs \<le> A \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). count_of (DAList.impl_of xs) x \<le> count A 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
  1204
    (is "?lhs \<longleftrightarrow> ?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
  1205
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
  1206
  assume ?lhs then show ?rhs
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1207
    by (auto 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
  1208
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
  1209
  assume ?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
  1210
  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
  1211
  proof (rule mset_less_eqI)
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1212
    fix x
46237
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
  1213
    from `?rhs` have "count_of (DAList.impl_of xs) x \<le> count A x"
99c80c2f841a renamed theory AList to DAList
bulwahn
parents: 46168
diff changeset
  1214
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1215
    then show "count (Bag xs) x \<le> count A x"
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1216
      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
  1217
  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
  1218
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
  1219
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38287
diff changeset
  1220
instantiation multiset :: (equal) equal
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1221
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
  1222
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1223
definition
45866
e62b319c7696 adding code attribute to enable evaluation of equality on multisets
bulwahn
parents: 45694
diff changeset
  1224
  [code]: "HOL.equal A B \<longleftrightarrow> (A::'a multiset) \<le> B \<and> B \<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
  1225
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1226
instance
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1227
  by default (simp add: equal_multiset_def 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
  1228
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1229
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
  1230
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1231
text {* Quickcheck generators *}
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38287
diff changeset
  1232
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1233
definition (in term_syntax)
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1234
  bagify :: "('a\<Colon>typerep, nat) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1235
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" 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
  1236
  [code_unfold]: "bagify xs = Code_Evaluation.valtermify Bag {\<cdot>} 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
  1237
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37261
diff changeset
  1238
notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37261
diff changeset
  1239
notation scomp (infixl "\<circ>\<rightarrow>" 60)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1240
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1241
instantiation multiset :: (random) random
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1242
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
  1243
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1244
definition
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37261
diff changeset
  1245
  "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (bagify xs))"
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1246
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1247
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
  1248
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1249
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
  1250
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37261
diff changeset
  1251
no_notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37261
diff changeset
  1252
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1253
46168
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1254
instantiation multiset :: (exhaustive) exhaustive
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1255
begin
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1256
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1257
definition exhaustive_multiset :: "('a multiset => (bool * term list) option) => code_numeral => (bool * term list) option"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1258
where
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1259
  "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (%xs. f (Bag xs)) i"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1260
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1261
instance ..
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1262
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1263
end
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1264
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1265
instantiation multiset :: (full_exhaustive) full_exhaustive
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1266
begin
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1267
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1268
definition full_exhaustive_multiset :: "('a multiset * (unit => term) => (bool * term list) option) => code_numeral => (bool * term list) option"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1269
where
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1270
  "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (%xs. f (bagify xs)) i"
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1271
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1272
instance ..
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1273
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1274
end
bef8c811df20 improving code generation for multisets; adding exhaustive quickcheck generators for multisets
bulwahn
parents: 45989
diff changeset
  1275
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 35712
diff changeset
  1276
hide_const (open) bagify
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1277
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1278
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1279
subsection {* The multiset order *}
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1280
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1281
subsubsection {* Well-foundedness *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1282
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1283
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
  1284
  "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
  1285
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1286
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1287
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
  1288
  "mult r = (mult1 r)\<^sup>+"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1289
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1290
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1291
by (simp add: mult1_def)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1292
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1293
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1294
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1295
    (\<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
  1296
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1297
proof (unfold mult1_def)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1298
  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
  1299
  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
  1300
  let ?case1 = "?case1 {(N, M). ?R N M}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1301
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1302
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1303
  then have "\<exists>a' M0' K.
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1304
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1305
  then show "?case1 \<or> ?case2"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1306
  proof (elim exE conjE)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1307
    fix a' M0' K
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1308
    assume N: "N = M0' + K" and r: "?r K a'"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1309
    assume "M0 + {#a#} = M0' + {#a'#}"
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1310
    then have "M0 = M0' \<and> a = a' \<or>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1311
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1312
      by (simp only: add_eq_conv_ex)
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1313
    then show ?thesis
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1314
    proof (elim disjE conjE exE)
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1315
      assume "M0 = M0'" "a = a'"
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1316
      with N r have "?r K a \<and> N = M0 + K" by simp
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1317
      then have ?case2 .. then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1318
    next
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1319
      fix K'
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1320
      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
  1321
      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
  1322
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1323
      assume "M0 = K' + {#a'#}"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1324
      with r have "?R (K' + K) M0" by blast
18258
836491e9b7d5 tuned induct proofs;
wenzelm
parents: 17778
diff changeset
  1325
      with n have ?case1 by simp then show ?thesis ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1326
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1327
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1328
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1329
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1330
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1331
proof
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1332
  let ?R = "mult1 r"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1333
  let ?W = "acc ?R"
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1334
  {
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1335
    fix M M0 a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1336
    assume M0: "M0 \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1337
      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
  1338
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1339
    have "M0 + {#a#} \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1340
    proof (rule accI [of "M0 + {#a#}"])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1341
      fix N
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1342
      assume "(N, M0 + {#a#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1343
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1344
          (\<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
  1345
        by (rule less_add)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1346
      then show "N \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1347
      proof (elim exE disjE conjE)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1348
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1349
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1350
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1351
        then show "N \<in> ?W" by (simp only: N)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1352
      next
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1353
        fix K
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1354
        assume N: "N = M0 + K"
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1355
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1356
        then have "M0 + K \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1357
        proof (induct K)
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1358
          case empty
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1359
          from M0 show "M0 + {#} \<in> ?W" by simp
18730
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1360
        next
843da46f89ac tuned proofs;
wenzelm
parents: 18258
diff changeset
  1361
          case (add K x)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1362
          from add.prems have "(x, a) \<in> r" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1363
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1364
          moreover from add have "M0 + K \<in> ?W" by simp
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1365
          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
  1366
          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
  1367
        qed
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1368
        then show "N \<in> ?W" by (simp only: N)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1369
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1370
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1371
  } note tedious_reasoning = this
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1372
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1373
  assume wf: "wf r"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1374
  fix M
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1375
  show "M \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1376
  proof (induct M)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1377
    show "{#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1378
    proof (rule accI)
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1379
      fix b assume "(b, {#}) \<in> ?R"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1380
      with not_less_empty show "b \<in> ?W" by contradiction
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1381
    qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1382
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1383
    fix M a assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1384
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1385
    proof induct
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1386
      fix a
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1387
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1388
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1389
      proof
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1390
        fix M assume "M \<in> ?W"
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1391
        then show "M + {#a#} \<in> ?W"
23373
ead82c82da9e tuned proofs: avoid implicit prems;
wenzelm
parents: 23281
diff changeset
  1392
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1393
      qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1394
    qed
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1395
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1396
  qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1397
qed
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1398
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1399
theorem wf_mult1: "wf r ==> wf (mult1 r)"
26178
nipkow
parents: 26176
diff changeset
  1400
by (rule acc_wfI) (rule all_accessible)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1401
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1402
theorem wf_mult: "wf r ==> wf (mult r)"
26178
nipkow
parents: 26176
diff changeset
  1403
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1404
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1405
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1406
subsubsection {* Closure-free presentation *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1407
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1408
text {* One direction. *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1409
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1410
lemma mult_implies_one_step:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1411
  "trans r ==> (M, N) \<in> mult r ==>
11464
ddea204de5bc turned translation for 1::nat into def.
nipkow
parents: 10714
diff changeset
  1412
    \<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
  1413
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
26178
nipkow
parents: 26176
diff changeset
  1414
apply (unfold mult_def mult1_def set_of_def)
nipkow
parents: 26176
diff changeset
  1415
apply (erule converse_trancl_induct, clarify)
nipkow
parents: 26176
diff changeset
  1416
 apply (rule_tac x = M0 in exI, simp, clarify)
nipkow
parents: 26176
diff changeset
  1417
apply (case_tac "a :# K")
nipkow
parents: 26176
diff changeset
  1418
 apply (rule_tac x = I in exI)
nipkow
parents: 26176
diff changeset
  1419
 apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  1420
 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
  1421
 apply (simp (no_asm_simp) add: add_assoc [symmetric])
26178
nipkow
parents: 26176
diff changeset
  1422
 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
nipkow
parents: 26176
diff changeset
  1423
 apply (simp add: diff_union_single_conv)
nipkow
parents: 26176
diff changeset
  1424
 apply (simp (no_asm_use) add: trans_def)
nipkow
parents: 26176
diff changeset
  1425
 apply blast
nipkow
parents: 26176
diff changeset
  1426
apply (subgoal_tac "a :# I")
nipkow
parents: 26176
diff changeset
  1427
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  1428
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow
parents: 26176
diff changeset
  1429
 apply (rule_tac x = "K + Ka" in exI)
nipkow
parents: 26176
diff changeset
  1430
 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
  1431
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  1432
 apply (rule conjI)
nipkow
parents: 26176
diff changeset
  1433
  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
  1434
  apply (simp add: multiset_eq_iff split: nat_diff_split)
26178
nipkow
parents: 26176
diff changeset
  1435
 apply (simp (no_asm_use) add: trans_def)
nipkow
parents: 26176
diff changeset
  1436
 apply blast
nipkow
parents: 26176
diff changeset
  1437
apply (subgoal_tac "a :# (M0 + {#a#})")
nipkow
parents: 26176
diff changeset
  1438
 apply simp
nipkow
parents: 26176
diff changeset
  1439
apply (simp (no_asm))
nipkow
parents: 26176
diff changeset
  1440
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1441
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1442
lemma one_step_implies_mult_aux:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1443
  "trans r ==>
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1444
    \<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
  1445
      --> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  1446
apply (induct_tac n, auto)
nipkow
parents: 26176
diff changeset
  1447
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow
parents: 26176
diff changeset
  1448
apply (rename_tac "J'", simp)
nipkow
parents: 26176
diff changeset
  1449
apply (erule notE, auto)
nipkow
parents: 26176
diff changeset
  1450
apply (case_tac "J' = {#}")
nipkow
parents: 26176
diff changeset
  1451
 apply (simp add: mult_def)
nipkow
parents: 26176
diff changeset
  1452
 apply (rule r_into_trancl)
nipkow
parents: 26176
diff changeset
  1453
 apply (simp add: mult1_def set_of_def, blast)
nipkow
parents: 26176
diff changeset
  1454
txt {* Now we know @{term "J' \<noteq> {#}"}. *}
nipkow
parents: 26176
diff changeset
  1455
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow
parents: 26176
diff changeset
  1456
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
nipkow
parents: 26176
diff changeset
  1457
apply (erule ssubst)
nipkow
parents: 26176
diff changeset
  1458
apply (simp add: Ball_def, auto)
nipkow
parents: 26176
diff changeset
  1459
apply (subgoal_tac
nipkow
parents: 26176
diff changeset
  1460
  "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
nipkow
parents: 26176
diff changeset
  1461
    (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow
parents: 26176
diff changeset
  1462
 prefer 2
nipkow
parents: 26176
diff changeset
  1463
 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
  1464
apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
26178
nipkow
parents: 26176
diff changeset
  1465
apply (erule trancl_trans)
nipkow
parents: 26176
diff changeset
  1466
apply (rule r_into_trancl)
nipkow
parents: 26176
diff changeset
  1467
apply (simp add: mult1_def set_of_def)
nipkow
parents: 26176
diff changeset
  1468
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  1469
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
  1470
apply (simp add: add_ac)
26178
nipkow
parents: 26176
diff changeset
  1471
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1472
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1473
lemma one_step_implies_mult:
23751
a7c7edf2c5ad Restored set notation.
berghofe
parents: 23611
diff changeset
  1474
  "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
  1475
    ==> (I + K, I + J) \<in> mult r"
26178
nipkow
parents: 26176
diff changeset
  1476
using one_step_implies_mult_aux by blast
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1477
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1478
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1479
subsubsection {* Partial-order properties *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1480
35273
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1481
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
  1482
  "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1483
35273
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1484
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
  1485
  "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
51692ec1b220 ascii syntax for multiset order
haftmann
parents: 35268
diff changeset
  1486
35308
359e0fd38a92 mind the "s"
haftmann
parents: 35273
diff changeset
  1487
notation (xsymbols) less_multiset (infix "\<subset>#" 50)
359e0fd38a92 mind the "s"
haftmann
parents: 35273
diff changeset
  1488
notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1489
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1490
interpretation multiset_order: order le_multiset less_multiset
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1491
proof -
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1492
  have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1493
  proof
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1494
    fix M :: "'a multiset"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1495
    assume "M \<subset># M"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1496
    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
  1497
    have "trans {(x'::'a, x). x' < x}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1498
      by (rule transI) simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1499
    moreover note MM
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1500
    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
  1501
      \<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
  1502
      by (rule mult_implies_one_step)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1503
    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
  1504
      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
  1505
    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
  1506
    have "finite (set_of K)" by simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1507
    moreover note aux2
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1508
    ultimately have "set_of K = {}"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1509
      by (induct rule: finite_induct) (auto intro: order_less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1510
    with aux1 show False by simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1511
  qed
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1512
  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
  1513
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1514
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1515
    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
  1516
qed
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1517
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1518
lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R"
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1519
  by simp
26567
7bcebb8c2d33 instantiation replacing primitive instance plus overloaded defs; more conservative type arities
haftmann
parents: 26178
diff changeset
  1520
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1521
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1522
subsubsection {* Monotonicity of multiset union *}
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1523
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1524
lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
26178
nipkow
parents: 26176
diff changeset
  1525
apply (unfold mult1_def)
nipkow
parents: 26176
diff changeset
  1526
apply auto
nipkow
parents: 26176
diff changeset
  1527
apply (rule_tac x = a in exI)
nipkow
parents: 26176
diff changeset
  1528
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
  1529
apply (simp add: add_assoc)
26178
nipkow
parents: 26176
diff changeset
  1530
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1531
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1532
lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
26178
nipkow
parents: 26176
diff changeset
  1533
apply (unfold less_multiset_def mult_def)
nipkow
parents: 26176
diff changeset
  1534
apply (erule trancl_induct)
40249
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  1535
 apply (blast intro: mult1_union)
cd404ecb9107 Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents: 39533
diff changeset
  1536
apply (blast intro: mult1_union trancl_trans)
26178
nipkow
parents: 26176
diff changeset
  1537
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1538
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1539
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
  1540
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
  1541
apply (subst add_commute [of D C])
26178
nipkow
parents: 26176
diff changeset
  1542
apply (erule union_less_mono2)
nipkow
parents: 26176
diff changeset
  1543
done
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1544
17161
57c69627d71a tuned some proofs;
wenzelm
parents: 15869
diff changeset
  1545
lemma union_less_mono:
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1546
  "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
  1547
  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
  1548
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1549
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
  1550
proof
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1551
qed (auto simp add: le_multiset_def intro: union_less_mono2)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1552
15072
4861bf6af0b4 new material courtesy of Norbert Voelker
paulson
parents: 14981
diff changeset
  1553
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1554
subsection {* The fold combinator *}
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1555
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1556
text {*
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1557
  The intended behaviour is
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1558
  @{text "fold_mset f z {#x\<^isub>1, ..., x\<^isub>n#} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1559
  if @{text f} is associative-commutative. 
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1560
*}
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1561
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1562
text {*
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1563
  The graph of @{text "fold_mset"}, @{text "z"}: the start element,
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1564
  @{text "f"}: folding function, @{text "A"}: the multiset, @{text
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1565
  "y"}: the result.
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1566
*}
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1567
inductive 
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1568
  fold_msetG :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b \<Rightarrow> bool" 
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1569
  for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" 
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1570
  and z :: 'b
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1571
where
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1572
  emptyI [intro]:  "fold_msetG f z {#} z"
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1573
| insertI [intro]: "fold_msetG f z A y \<Longrightarrow> fold_msetG f z (A + {#x#}) (f x y)"
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1574
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1575
inductive_cases empty_fold_msetGE [elim!]: "fold_msetG f z {#} x"
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1576
inductive_cases insert_fold_msetGE: "fold_msetG f z (A + {#}) y" 
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1577
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1578
definition
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1579
  fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b" where
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1580
  "fold_mset f z A = (THE x. fold_msetG f z A x)"
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1581
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1582
lemma Diff1_fold_msetG:
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1583
  "fold_msetG f z (A - {#x#}) y \<Longrightarrow> x \<in># A \<Longrightarrow> fold_msetG f z A (f x y)"
26178
nipkow
parents: 26176
diff changeset
  1584
apply (frule_tac x = x in fold_msetG.insertI)
nipkow
parents: 26176
diff changeset
  1585
apply auto
nipkow
parents: 26176
diff changeset
  1586
done
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1587
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1588
lemma fold_msetG_nonempty: "\<exists>x. fold_msetG f z A x"
26178
nipkow
parents: 26176
diff changeset
  1589
apply (induct A)
nipkow
parents: 26176
diff changeset
  1590
 apply blast
nipkow
parents: 26176
diff changeset
  1591
apply clarsimp
nipkow
parents: 26176
diff changeset
  1592
apply (drule_tac x = x in fold_msetG.insertI)
nipkow
parents: 26176
diff changeset
  1593
apply auto
nipkow
parents: 26176
diff changeset
  1594
done
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1595
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1596
lemma fold_mset_empty[simp]: "fold_mset f z {#} = z"
26178
nipkow
parents: 26176
diff changeset
  1597
unfolding fold_mset_def by blast
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1598
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42809
diff changeset
  1599
context comp_fun_commute
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1600
begin
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1601
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1602
lemma fold_msetG_determ:
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1603
  "fold_msetG f z A x \<Longrightarrow> fold_msetG f z A y \<Longrightarrow> y = x"
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1604
proof (induct arbitrary: x y z rule: full_multiset_induct)
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1605
  case (less M x\<^isub>1 x\<^isub>2 Z)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1606
  have IH: "\<forall>A. A < M \<longrightarrow> 
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1607
    (\<forall>x x' x''. fold_msetG f x'' A x \<longrightarrow> fold_msetG f x'' A x'
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1608
               \<longrightarrow> x' = x)" by fact
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1609
  have Mfoldx\<^isub>1: "fold_msetG f Z M x\<^isub>1" and Mfoldx\<^isub>2: "fold_msetG f Z M x\<^isub>2" by fact+
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1610
  show ?case
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1611
  proof (rule fold_msetG.cases [OF Mfoldx\<^isub>1])
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1612
    assume "M = {#}" and "x\<^isub>1 = Z"
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1613
    then show ?case using Mfoldx\<^isub>2 by auto 
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1614
  next
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1615
    fix B b u
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1616
    assume "M = B + {#b#}" and "x\<^isub>1 = f b u" and Bu: "fold_msetG f Z B u"
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1617
    then have MBb: "M = B + {#b#}" and x\<^isub>1: "x\<^isub>1 = f b u" by auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1618
    show ?case
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1619
    proof (rule fold_msetG.cases [OF Mfoldx\<^isub>2])
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1620
      assume "M = {#}" "x\<^isub>2 = Z"
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1621
      then show ?case using Mfoldx\<^isub>1 by auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1622
    next
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1623
      fix C c v
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1624
      assume "M = C + {#c#}" and "x\<^isub>2 = f c v" and Cv: "fold_msetG f Z C v"
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1625
      then have MCc: "M = C + {#c#}" and x\<^isub>2: "x\<^isub>2 = f c v" by auto
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1626
      then have CsubM: "C < M" by simp
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1627
      from MBb have BsubM: "B < M" by simp
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1628
      show ?case
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1629
      proof cases
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1630
        assume *: "b = c"
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1631
        then have "B = C" using MBb MCc by auto
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1632
        with * show ?thesis using Bu Cv x\<^isub>1 x\<^isub>2 CsubM IH by auto
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1633
      next
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1634
        assume diff: "b \<noteq> c"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1635
        let ?D = "B - {#c#}"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1636
        have cinB: "c \<in># B" and binC: "b \<in># C" using MBb MCc diff
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1637
          by (auto intro: insert_noteq_member dest: sym)
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1638
        have "B - {#c#} < B" using cinB by (rule mset_less_diff_self)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1639
        then have DsubM: "?D < M" using BsubM by (blast intro: order_less_trans)
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1640
        from MBb MCc have "B + {#b#} = C + {#c#}" by blast
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1641
        then have [simp]: "B + {#b#} - {#c#} = C"
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1642
          using MBb MCc binC cinB by auto
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1643
        have B: "B = ?D + {#c#}" and C: "C = ?D + {#b#}"
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1644
          using MBb MCc diff binC cinB
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1645
          by (auto simp: multiset_add_sub_el_shuffle)
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1646
        then obtain d where Dfoldd: "fold_msetG f Z ?D d"
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1647
          using fold_msetG_nonempty by iprover
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1648
        then have "fold_msetG f Z B (f c d)" using cinB
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1649
          by (rule Diff1_fold_msetG)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1650
        then have "f c d = u" using IH BsubM Bu by blast
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1651
        moreover 
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1652
        have "fold_msetG f Z C (f b d)" using binC cinB diff Dfoldd
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1653
          by (auto simp: multiset_add_sub_el_shuffle 
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1654
            dest: fold_msetG.insertI [where x=b])
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1655
        then have "f b d = v" using IH CsubM Cv by blast
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1656
        ultimately show ?thesis using x\<^isub>1 x\<^isub>2
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1657
          by (auto simp: fun_left_comm)
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1658
      qed
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1659
    qed
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1660
  qed
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1661
qed
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1662
        
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1663
lemma fold_mset_insert_aux:
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1664
  "(fold_msetG f z (A + {#x#}) v) =
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1665
    (\<exists>y. fold_msetG f z A y \<and> v = f x y)"
26178
nipkow
parents: 26176
diff changeset
  1666
apply (rule iffI)
nipkow
parents: 26176
diff changeset
  1667
 prefer 2
nipkow
parents: 26176
diff changeset
  1668
 apply blast
45608
13b101cee425 eliminated obsolete "standard";
wenzelm
parents: 44890
diff changeset
  1669
apply (rule_tac A1=A and f1=f in fold_msetG_nonempty [THEN exE])
26178
nipkow
parents: 26176
diff changeset
  1670
apply (blast intro: fold_msetG_determ)
nipkow
parents: 26176
diff changeset
  1671
done
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1672
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1673
lemma fold_mset_equality: "fold_msetG f z A y \<Longrightarrow> fold_mset f z A = y"
26178
nipkow
parents: 26176
diff changeset
  1674
unfolding fold_mset_def by (blast intro: fold_msetG_determ)
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1675
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1676
lemma fold_mset_insert:
26178
nipkow
parents: 26176
diff changeset
  1677
  "fold_mset f z (A + {#x#}) = f x (fold_mset f z A)"
nipkow
parents: 26176
diff changeset
  1678
apply (simp add: fold_mset_def fold_mset_insert_aux)
nipkow
parents: 26176
diff changeset
  1679
apply (rule the_equality)
nipkow
parents: 26176
diff changeset
  1680
 apply (auto cong add: conj_cong 
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1681
     simp add: fold_mset_def [symmetric] fold_mset_equality fold_msetG_nonempty)
26178
nipkow
parents: 26176
diff changeset
  1682
done
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1683
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1684
lemma fold_mset_commute: "f x (fold_mset f z A) = fold_mset f (f x z) 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
  1685
by (induct A) (auto simp: fold_mset_insert fun_left_comm [of x])
26178
nipkow
parents: 26176
diff changeset
  1686
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1687
lemma fold_mset_single [simp]: "fold_mset f z {#x#} = f x z"
26178
nipkow
parents: 26176
diff changeset
  1688
using fold_mset_insert [of z "{#}"] by simp
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1689
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1690
lemma fold_mset_union [simp]:
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1691
  "fold_mset f z (A+B) = fold_mset f (fold_mset f z A) B"
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1692
proof (induct A)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1693
  case empty then show ?case by simp
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1694
next
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1695
  case (add A 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
  1696
  have "A + {#x#} + B = (A+B) + {#x#}" by (simp add: add_ac)
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1697
  then have "fold_mset f z (A + {#x#} + B) = f x (fold_mset f z (A + B))" 
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1698
    by (simp add: fold_mset_insert)
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1699
  also have "\<dots> = fold_mset f (fold_mset f z (A + {#x#})) B"
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1700
    by (simp add: fold_mset_commute[of x,symmetric] add fold_mset_insert)
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1701
  finally show ?case .
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1702
qed
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1703
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1704
lemma fold_mset_fusion:
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42809
diff changeset
  1705
  assumes "comp_fun_commute g"
27611
2c01c0bdb385 Removed uses of context element includes.
ballarin
parents: 27487
diff changeset
  1706
  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold_mset g w A) = fold_mset f (h w) A" (is "PROP ?P")
2c01c0bdb385 Removed uses of context element includes.
ballarin
parents: 27487
diff changeset
  1707
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42809
diff changeset
  1708
  interpret comp_fun_commute g by (fact assms)
27611
2c01c0bdb385 Removed uses of context element includes.
ballarin
parents: 27487
diff changeset
  1709
  show "PROP ?P" by (induct A) auto
2c01c0bdb385 Removed uses of context element includes.
ballarin
parents: 27487
diff changeset
  1710
qed
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1711
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1712
lemma fold_mset_rec:
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1713
  assumes "a \<in># A" 
25759
6326138c1bd7 renamed foldM to fold_mset on general request
kleing
parents: 25623
diff changeset
  1714
  shows "fold_mset f z A = f a (fold_mset f z (A - {#a#}))"
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1715
proof -
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1716
  from assms obtain A' where "A = A' + {#a#}"
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1717
    by (blast dest: multi_member_split)
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1718
  then show ?thesis by simp
25610
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1719
qed
72e1563aee09 a fold operation for multisets + more lemmas
kleing
parents: 25595
diff changeset
  1720
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1721
end
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1722
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1723
text {*
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1724
  A note on code generation: When defining some function containing a
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1725
  subterm @{term"fold_mset F"}, code generation is not automatic. When
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1726
  interpreting locale @{text left_commutative} with @{text F}, the
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1727
  would be code thms for @{const fold_mset} become thms like
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1728
  @{term"fold_mset F z {#} = z"} where @{text F} is not a pattern but
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1729
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1730
  constant with its own code thms needs to be introduced for @{text
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1731
  F}. See the image operator below.
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1732
*}
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1733
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1734
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1735
subsection {* Image *}
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1736
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1737
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b 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
  1738
  "image_mset f = fold_mset (op + o single o f) {#}"
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1739
44339
eda6aef75939 odd workaround for odd problem of load order in HOL/ex/ROOT.ML (!??);
wenzelm
parents: 42871
diff changeset
  1740
interpretation image_fun_commute: comp_fun_commute "op + o single o f" for f
42809
5b45125b15ba use pointfree characterisation for fold_set locale
haftmann
parents: 41505
diff changeset
  1741
proof qed (simp add: add_ac fun_eq_iff)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1742
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1743
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
26178
nipkow
parents: 26176
diff changeset
  1744
by (simp add: image_mset_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1745
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1746
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
26178
nipkow
parents: 26176
diff changeset
  1747
by (simp add: image_mset_def)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1748
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1749
lemma image_mset_insert:
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1750
  "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
26178
nipkow
parents: 26176
diff changeset
  1751
by (simp add: image_mset_def add_ac)
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1752
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
  1753
lemma image_mset_union [simp]:
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1754
  "image_mset f (M+N) = image_mset f M + image_mset f N"
26178
nipkow
parents: 26176
diff changeset
  1755
apply (induct N)
nipkow
parents: 26176
diff changeset
  1756
 apply 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
  1757
apply (simp add: add_assoc [symmetric] image_mset_insert)
26178
nipkow
parents: 26176
diff changeset
  1758
done
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1759
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1760
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
26178
nipkow
parents: 26176
diff changeset
  1761
by (induct M) simp_all
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1762
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1763
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
26178
nipkow
parents: 26176
diff changeset
  1764
by (cases M) auto
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1765
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1766
syntax
35352
fa051b504c3f observe standard convention for syntax consts;
wenzelm
parents: 35308
diff changeset
  1767
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1768
      ("({#_/. _ :# _#})")
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1769
translations
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1770
  "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1771
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1772
syntax
35352
fa051b504c3f observe standard convention for syntax consts;
wenzelm
parents: 35308
diff changeset
  1773
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1774
      ("({#_/ | _ :# _./ _#})")
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1775
translations
26033
278025d5282d modified MCollect syntax
nipkow
parents: 26016
diff changeset
  1776
  "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1777
26145
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1778
text {*
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1779
  This allows to write not just filters like @{term "{#x:#M. x<c#}"}
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1780
  but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1781
  "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1782
  @{term "{#x+x|x:#M. x<c#}"}.
95670b6e1fa3 tuned document;
wenzelm
parents: 26143
diff changeset
  1783
*}
26016
f9d1bf2fc59c added multiset comprehension
nipkow
parents: 25759
diff changeset
  1784
46730
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1785
enriched_type image_mset: image_mset
e3b99d0231bc tuned proofs;
wenzelm
parents: 46394
diff changeset
  1786
proof -
46921
aa862ff8a8a9 some proof indentation;
wenzelm
parents: 46756
diff changeset
  1787
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
41372
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1788
  proof
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1789
    fix A
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1790
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1791
      by (induct A) simp_all
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1792
  qed
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1793
  show "image_mset id = id"
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1794
  proof
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1795
    fix A
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1796
    show "image_mset id A = id A"
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1797
      by (induct A) simp_all
551eb49a6e91 tuned type_lifting declarations
haftmann
parents: 41069
diff changeset
  1798
  qed
40606
af1a0b0c6202 mapper for mulitset type
haftmann
parents: 40347
diff changeset
  1799
qed
af1a0b0c6202 mapper for mulitset type
haftmann
parents: 40347
diff changeset
  1800
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1801
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1802
subsection {* Termination proofs with multiset orders *}
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 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
  1805
  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
  1806
  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
  1807
  by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1808
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1809
definition "ms_strict = mult pair_less"
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 37751
diff changeset
  1810
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
  1811
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1812
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
  1813
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
  1814
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
  1815
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1816
lemma smsI:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1817
  "(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
  1818
  unfolding ms_strict_def
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1819
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
  1820
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1821
lemma wmsI:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1822
  "(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
  1823
  \<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
  1824
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
  1825
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
  1826
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1827
inductive pw_leq
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1828
where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1829
  pw_leq_empty: "pw_leq {#} {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1830
| 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
  1831
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1832
lemma pw_leq_lstep:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1833
  "(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
  1834
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
  1835
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1836
lemma pw_leq_split:
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1837
  assumes "pw_leq X Y"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1838
  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
  1839
  using assms
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1840
proof (induct)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1841
  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
  1842
next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1843
  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
  1844
  then obtain A B Z where
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1845
    [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
  1846
      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
  1847
    by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1848
  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
  1849
    unfolding pair_leq_def by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1850
  thus ?case
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1851
  proof
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1852
    assume [simp]: "x = y"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1853
    have
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1854
      "{#x#} + X = A + ({#y#}+Z) 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1855
      \<and> {#y#} + Y = B + ({#y#}+Z)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1856
      \<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
  1857
      by (auto simp: add_ac)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1858
    thus ?case by (intro exI)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1859
  next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1860
    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
  1861
    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
  1862
    have "{#x#} + X = ?A' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1863
      "{#y#} + Y = ?B' + Z"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1864
      by (auto simp add: add_ac)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1865
    moreover have 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1866
      "(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
  1867
      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
  1868
      by (auto elim!: max_ext.cases)
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1869
    ultimately show ?thesis by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1870
  qed
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
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1873
lemma 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1874
  assumes pwleq: "pw_leq Z Z'"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1875
  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
  1876
  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
  1877
  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
  1878
proof -
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1879
  from pw_leq_split[OF pwleq] 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1880
  obtain A' B' Z''
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1881
    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
  1882
    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
  1883
    by blast
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1884
  {
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1885
    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
  1886
    from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1887
    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
  1888
    proof
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1889
      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
  1890
      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
  1891
        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
  1892
      thus ?thesis by (rule smsI) 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1893
    next
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1894
      assume [simp]: "A' = {#} \<and> B' = {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1895
      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
  1896
    qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1897
    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
  1898
    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
  1899
  }
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1900
  from mx_or_empty
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1901
  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
  1902
  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
  1903
qed
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1904
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  1905
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
  1906
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1907
and nonempty_single: "{# x #} \<noteq> {#}"
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1908
by auto
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1909
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1910
setup {*
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1911
let
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1912
  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
  1913
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1914
  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
  1915
    | 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
  1916
    | mk_mset T (x :: xs) =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1917
          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
  1918
                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
  1919
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1920
  fun mset_member_tac m i =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1921
      (if m <= 0 then
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1922
           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
  1923
       else
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1924
           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
  1925
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1926
  val mset_nonempty_tac =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1927
      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
  1928
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1929
  val regroup_munion_conv =
35402
115a5a95710a clarified @{const_name} vs. @{const_abbrev};
wenzelm
parents: 35352
diff changeset
  1930
      Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
39301
e1bd8a54c40f added and renamed lemmas
nipkow
parents: 39198
diff changeset
  1931
        (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
  1932
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1933
  fun unfold_pwleq_tac i =
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1934
    (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
  1935
      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
  1936
      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
  1937
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1938
  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
  1939
                      @{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
  1940
in
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1941
  ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset 
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1942
  {
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1943
    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
  1944
    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
  1945
    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
  1946
    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
  1947
    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
  1948
  })
10249
e4d13d8a9011 Multisets (from HOL/Induct/Multiset and friends);
wenzelm
parents:
diff changeset
  1949
end
29125
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1950
*}
d41182a8135c method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents: 28823
diff changeset
  1951
34943
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1952
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1953
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
  1954
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39301
diff changeset
  1955
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
  1956
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1957
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
  1958
  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
  1959
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1960
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
  1961
  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
  1962
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1963
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
  1964
  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
  1965
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1966
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
  1967
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1968
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
  1969
  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
  1970
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1971
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
  1972
  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
  1973
e97b22500a5c cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents: 33102
diff changeset
  1974
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
  1975
  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
  1976
35268
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1977
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
  1978
  by (fact order_less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1979
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1980
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1981
  by (fact inf.commute)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1982
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1983
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
  1984
  by (fact inf.assoc [symmetric])
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1985
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1986
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
  1987
  by (fact inf.left_commute)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1988
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1989
lemmas multiset_inter_ac =
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1990
  multiset_inter_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1991
  multiset_inter_assoc
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1992
  multiset_inter_left_commute
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1993
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1994
lemma mult_less_not_refl:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1995
  "\<not> M \<subset># (M::'a::order multiset)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1996
  by (fact multiset_order.less_irrefl)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1997
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1998
lemma mult_less_trans:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  1999
  "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
  2000
  by (fact multiset_order.less_trans)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2001
    
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2002
lemma mult_less_not_sym:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2003
  "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2004
  by (fact multiset_order.less_not_sym)
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2005
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2006
lemma mult_less_asym:
04673275441a switched notations for pointwise and multiset order
haftmann
parents: 35028
diff changeset
  2007
  "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
  2008
  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
  2009
35712
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2010
ML {*
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2011
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
  2012
                      (Const _ $ t') =
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2013
    let
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2014
      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
  2015
        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
  2016
        ||> 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
  2017
      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
  2018
        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
  2019
          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
  2020
        | 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
  2021
    in
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2022
      case maps elems_for (all_values elem_T) @
37261
8a89fd40ed0b honor xsymbols in Nitpick
blanchet
parents: 37169
diff changeset
  2023
           (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
8a89fd40ed0b honor xsymbols in Nitpick
blanchet
parents: 37169
diff changeset
  2024
            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
  2025
        [] => 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
  2026
      | 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
  2027
                         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
  2028
                         $ t1 $ t2)
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2029
                     (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
  2030
                                                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
  2031
    end
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2032
  | 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
  2033
*}
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2034
38287
796302ca3611 replace "setup" with "declaration"
blanchet
parents: 38242
diff changeset
  2035
declaration {*
796302ca3611 replace "setup" with "declaration"
blanchet
parents: 38242
diff changeset
  2036
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
38242
f26d590dce0f adapt occurrences of renamed Nitpick functions
blanchet
parents: 37765
diff changeset
  2037
    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
  2038
*}
77aa29bf14ee added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
blanchet
parents: 35402
diff changeset
  2039
37169
f69efa106feb make Nitpick "show_all" option behave less surprisingly
blanchet
parents: 37107
diff changeset
  2040
end