src/HOL/Finite_Set.thy
author paulson
Sun, 24 Nov 2013 00:31:50 +0000
changeset 54570 002b8729f228
parent 54413 88a036a95967
child 54611 31afce809794
permissions -rw-r--r--
polished some ancient proofs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Finite_Set.thy
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     2
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
16775
c1b87ef4a1c3 added lemmas to OrderedGroup.thy (reasoning about signs, absolute value, triangle inequalities)
avigad
parents: 16760
diff changeset
     3
                with contributions by Jeremy Avigad
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     4
*)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     5
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     6
header {* Finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     7
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15124
diff changeset
     8
theory Finite_Set
38400
9bfcb1507c6b import swap prevents strange failure of SML code generator for datatypes
haftmann
parents: 37770
diff changeset
     9
imports Option Power
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15124
diff changeset
    10
begin
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    11
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
    12
subsection {* Predicate for finite sets *}
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    13
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    14
inductive finite :: "'a set \<Rightarrow> bool"
22262
96ba62dff413 Adapted to new inductive definition package.
berghofe
parents: 21733
diff changeset
    15
  where
96ba62dff413 Adapted to new inductive definition package.
berghofe
parents: 21733
diff changeset
    16
    emptyI [simp, intro!]: "finite {}"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    17
  | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    18
49764
9979d64b8016 moving simproc from Finite_Set to more appropriate Product_Type theory
bulwahn
parents: 49758
diff changeset
    19
simproc_setup finite_Collect ("finite (Collect P)") = {* K Set_Comprehension_Pointfree.simproc *}
48109
0a58f7eefba2 Integrated set comprehension pointfree simproc.
Rafal Kolanski <rafal.kolanski@nicta.com.au>
parents: 48063
diff changeset
    20
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    21
lemma finite_induct [case_names empty insert, induct set: finite]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    22
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    23
  assumes "finite F"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    24
  assumes "P {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    25
    and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    26
  shows "P F"
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
    27
using `finite F`
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
    28
proof induct
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    29
  show "P {}" by fact
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    30
  fix x F assume F: "finite F" and P: "P F"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    31
  show "P (insert x F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    32
  proof cases
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    33
    assume "x \<in> F"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    34
    hence "insert x F = F" by (rule insert_absorb)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    35
    with P show ?thesis by (simp only:)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    36
  next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    37
    assume "x \<notin> F"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    38
    from F this P show ?thesis by (rule insert)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    39
  qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    40
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    41
51622
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    42
lemma infinite_finite_induct [case_names infinite empty insert]:
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    43
  assumes infinite: "\<And>A. \<not> finite A \<Longrightarrow> P A"
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    44
  assumes empty: "P {}"
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    45
  assumes insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    46
  shows "P A"
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    47
proof (cases "finite A")
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    48
  case False with infinite show ?thesis .
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    49
next
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    50
  case True then show ?thesis by (induct A) (fact empty insert)+
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    51
qed
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    52
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    53
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    54
subsubsection {* Choice principles *}
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    55
13737
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    56
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    57
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    58
  shows "\<exists>a::'a. a \<notin> A"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    59
proof -
28823
dcbef866c9e2 tuned unfold_locales invocation
haftmann
parents: 27981
diff changeset
    60
  from assms have "A \<noteq> UNIV" by blast
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    61
  then show ?thesis by blast
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    62
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    63
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    64
text {* A finite choice principle. Does not need the SOME choice operator. *}
15484
2636ec211ec8 fold and fol1 changes
nipkow
parents: 15483
diff changeset
    65
29923
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    66
lemma finite_set_choice:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    67
  "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    68
proof (induct rule: finite_induct)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    69
  case empty then show ?case by simp
29923
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    70
next
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    71
  case (insert a A)
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    72
  then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    73
  show ?case (is "EX f. ?P f")
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    74
  proof
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    75
    show "?P(%x. if x = a then b else f x)" using f ab by auto
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    76
  qed
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    77
qed
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    78
23878
bd651ecd4b8a simplified HOL bootstrap
haftmann
parents: 23736
diff changeset
    79
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    80
subsubsection {* Finite sets are the images of initial segments of natural numbers *}
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
    81
15510
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    82
lemma finite_imp_nat_seg_image_inj_on:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    83
  assumes "finite A" 
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    84
  shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
    85
using assms
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
    86
proof induct
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
    87
  case empty
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    88
  show ?case
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    89
  proof
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    90
    show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}" by simp 
15510
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    91
  qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
    92
next
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
    93
  case (insert a A)
23389
aaca6a8e5414 tuned proofs: avoid implicit prems;
wenzelm
parents: 23277
diff changeset
    94
  have notinA: "a \<notin> A" by fact
15510
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    95
  from insert.hyps obtain n f
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    96
    where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    97
  hence "insert a A = f(n:=a) ` {i. i < Suc n}"
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    98
        "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    99
    by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   100
  thus ?case by blast
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   101
qed
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   102
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   103
lemma nat_seg_image_imp_finite:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   104
  "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   105
proof (induct n arbitrary: A)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   106
  case 0 thus ?case by simp
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   107
next
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   108
  case (Suc n)
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   109
  let ?B = "f ` {i. i < n}"
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   110
  have finB: "finite ?B" by(rule Suc.hyps[OF refl])
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   111
  show ?case
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   112
  proof cases
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   113
    assume "\<exists>k<n. f n = f k"
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   114
    hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   115
    thus ?thesis using finB by simp
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   116
  next
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   117
    assume "\<not>(\<exists> k<n. f n = f k)"
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   118
    hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   119
    thus ?thesis using finB by simp
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   120
  qed
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   121
qed
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   122
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   123
lemma finite_conv_nat_seg_image:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   124
  "finite A \<longleftrightarrow> (\<exists>(n::nat) f. A = f ` {i::nat. i < n})"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   125
  by (blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   126
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   127
lemma finite_imp_inj_to_nat_seg:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   128
  assumes "finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   129
  shows "\<exists>f n::nat. f ` A = {i. i < n} \<and> inj_on f A"
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   130
proof -
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   131
  from finite_imp_nat_seg_image_inj_on[OF `finite A`]
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   132
  obtain f and n::nat where bij: "bij_betw f {i. i<n} A"
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   133
    by (auto simp:bij_betw_def)
33057
764547b68538 inv_onto -> inv_into
nipkow
parents: 32989
diff changeset
   134
  let ?f = "the_inv_into {i. i<n} f"
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   135
  have "inj_on ?f A & ?f ` A = {i. i<n}"
33057
764547b68538 inv_onto -> inv_into
nipkow
parents: 32989
diff changeset
   136
    by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   137
  thus ?thesis by blast
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   138
qed
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   139
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   140
lemma finite_Collect_less_nat [iff]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   141
  "finite {n::nat. n < k}"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44835
diff changeset
   142
  by (fastforce simp: finite_conv_nat_seg_image)
29920
b95f5b8b93dd more finiteness
nipkow
parents: 29918
diff changeset
   143
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   144
lemma finite_Collect_le_nat [iff]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   145
  "finite {n::nat. n \<le> k}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   146
  by (simp add: le_eq_less_or_eq Collect_disj_eq)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   147
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   148
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   149
subsubsection {* Finiteness and common set operations *}
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   150
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   151
lemma rev_finite_subset:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   152
  "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   153
proof (induct arbitrary: A rule: finite_induct)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   154
  case empty
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   155
  then show ?case by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   156
next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   157
  case (insert x F A)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   158
  have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})" by fact+
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   159
  show "finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   160
  proof cases
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   161
    assume x: "x \<in> A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   162
    with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   163
    with r have "finite (A - {x})" .
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   164
    hence "finite (insert x (A - {x}))" ..
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   165
    also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   166
    finally show ?thesis .
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   167
  next
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   168
    show "A \<subseteq> F ==> ?thesis" by fact
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   169
    assume "x \<notin> A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   170
    with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   171
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   172
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   173
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   174
lemma finite_subset:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   175
  "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   176
  by (rule rev_finite_subset)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   177
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   178
lemma finite_UnI:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   179
  assumes "finite F" and "finite G"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   180
  shows "finite (F \<union> G)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   181
  using assms by induct simp_all
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
   182
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   183
lemma finite_Un [iff]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   184
  "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   185
  by (blast intro: finite_UnI finite_subset [of _ "F \<union> G"])
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
   186
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   187
lemma finite_insert [simp]: "finite (insert a A) \<longleftrightarrow> finite A"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   188
proof -
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   189
  have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   190
  then have "finite ({a} \<union> A) \<longleftrightarrow> finite A" by (simp only: finite_Un)
23389
aaca6a8e5414 tuned proofs: avoid implicit prems;
wenzelm
parents: 23277
diff changeset
   191
  then show ?thesis by simp
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   192
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   193
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   194
lemma finite_Int [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   195
  "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   196
  by (blast intro: finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   197
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   198
lemma finite_Collect_conjI [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   199
  "finite {x. P x} \<or> finite {x. Q x} \<Longrightarrow> finite {x. P x \<and> Q x}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   200
  by (simp add: Collect_conj_eq)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   201
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   202
lemma finite_Collect_disjI [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   203
  "finite {x. P x \<or> Q x} \<longleftrightarrow> finite {x. P x} \<and> finite {x. Q x}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   204
  by (simp add: Collect_disj_eq)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   205
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   206
lemma finite_Diff [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   207
  "finite A \<Longrightarrow> finite (A - B)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   208
  by (rule finite_subset, rule Diff_subset)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   209
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   210
lemma finite_Diff2 [simp]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   211
  assumes "finite B"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   212
  shows "finite (A - B) \<longleftrightarrow> finite A"
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   213
proof -
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   214
  have "finite A \<longleftrightarrow> finite((A - B) \<union> (A \<inter> B))" by (simp add: Un_Diff_Int)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   215
  also have "\<dots> \<longleftrightarrow> finite (A - B)" using `finite B` by simp
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   216
  finally show ?thesis ..
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   217
qed
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   218
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   219
lemma finite_Diff_insert [iff]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   220
  "finite (A - insert a B) \<longleftrightarrow> finite (A - B)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   221
proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   222
  have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   223
  moreover have "A - insert a B = A - B - {a}" by auto
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   224
  ultimately show ?thesis by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   225
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   226
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   227
lemma finite_compl[simp]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   228
  "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   229
  by (simp add: Compl_eq_Diff_UNIV)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   230
29916
f24137b42d9b more finiteness
nipkow
parents: 29903
diff changeset
   231
lemma finite_Collect_not[simp]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   232
  "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   233
  by (simp add: Collect_neg_eq)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   234
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   235
lemma finite_Union [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   236
  "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite(\<Union>A)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   237
  by (induct rule: finite_induct) simp_all
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   238
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   239
lemma finite_UN_I [intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   240
  "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (\<Union>a\<in>A. B a)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   241
  by (induct rule: finite_induct) simp_all
29903
2c0046b26f80 more finiteness changes
nipkow
parents: 29901
diff changeset
   242
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   243
lemma finite_UN [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   244
  "finite A \<Longrightarrow> finite (UNION A B) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   245
  by (blast intro: finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   246
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   247
lemma finite_Inter [intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   248
  "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   249
  by (blast intro: Inter_lower finite_subset)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   250
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   251
lemma finite_INT [intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   252
  "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   253
  by (blast intro: INT_lower finite_subset)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   254
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   255
lemma finite_imageI [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   256
  "finite F \<Longrightarrow> finite (h ` F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   257
  by (induct rule: finite_induct) simp_all
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   258
31768
159cd6b5e5d4 lemma finite_image_set by Jeremy Avigad
haftmann
parents: 31465
diff changeset
   259
lemma finite_image_set [simp]:
159cd6b5e5d4 lemma finite_image_set by Jeremy Avigad
haftmann
parents: 31465
diff changeset
   260
  "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
159cd6b5e5d4 lemma finite_image_set by Jeremy Avigad
haftmann
parents: 31465
diff changeset
   261
  by (simp add: image_Collect [symmetric])
159cd6b5e5d4 lemma finite_image_set by Jeremy Avigad
haftmann
parents: 31465
diff changeset
   262
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   263
lemma finite_imageD:
42206
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   264
  assumes "finite (f ` A)" and "inj_on f A"
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   265
  shows "finite A"
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   266
using assms
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   267
proof (induct "f ` A" arbitrary: A)
42206
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   268
  case empty then show ?case by simp
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   269
next
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   270
  case (insert x B)
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   271
  then have B_A: "insert x B = f ` A" by simp
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   272
  then obtain y where "x = f y" and "y \<in> A" by blast
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   273
  from B_A `x \<notin> B` have "B = f ` A - {x}" by blast
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   274
  with B_A `x \<notin> B` `x = f y` `inj_on f A` `y \<in> A` have "B = f ` (A - {y})" by (simp add: inj_on_image_set_diff)
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   275
  moreover from `inj_on f A` have "inj_on f (A - {y})" by (rule inj_on_diff)
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   276
  ultimately have "finite (A - {y})" by (rule insert.hyps)
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   277
  then show "finite A" by simp
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   278
qed
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   279
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   280
lemma finite_surj:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   281
  "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   282
  by (erule finite_subset) (rule finite_imageI)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   283
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   284
lemma finite_range_imageI:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   285
  "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   286
  by (drule finite_imageI) (simp add: range_composition)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   287
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   288
lemma finite_subset_image:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   289
  assumes "finite B"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   290
  shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   291
using assms
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   292
proof induct
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   293
  case empty then show ?case by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   294
next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   295
  case insert then show ?case
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   296
    by (clarsimp simp del: image_insert simp add: image_insert [symmetric])
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   297
       blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   298
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   299
43991
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   300
lemma finite_vimage_IntI:
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   301
  "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   302
  apply (induct rule: finite_induct)
21575
89463ae2612d tuned proofs;
wenzelm
parents: 21409
diff changeset
   303
   apply simp_all
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   304
  apply (subst vimage_insert)
43991
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   305
  apply (simp add: finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   306
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   307
43991
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   308
lemma finite_vimageI:
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   309
  "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)"
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   310
  using finite_vimage_IntI[of F h UNIV] by auto
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   311
34111
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   312
lemma finite_vimageD:
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   313
  assumes fin: "finite (h -` F)" and surj: "surj h"
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   314
  shows "finite F"
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   315
proof -
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   316
  have "finite (h ` (h -` F))" using fin by (rule finite_imageI)
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   317
  also have "h ` (h -` F) = F" using surj by (rule surj_image_vimage_eq)
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   318
  finally show "finite F" .
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   319
qed
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   320
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   321
lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   322
  unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   323
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   324
lemma finite_Collect_bex [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   325
  assumes "finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   326
  shows "finite {x. \<exists>y\<in>A. Q x y} \<longleftrightarrow> (\<forall>y\<in>A. finite {x. Q x y})"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   327
proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   328
  have "{x. \<exists>y\<in>A. Q x y} = (\<Union>y\<in>A. {x. Q x y})" by auto
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   329
  with assms show ?thesis by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   330
qed
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   331
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   332
lemma finite_Collect_bounded_ex [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   333
  assumes "finite {y. P y}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   334
  shows "finite {x. \<exists>y. P y \<and> Q x y} \<longleftrightarrow> (\<forall>y. P y \<longrightarrow> finite {x. Q x y})"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   335
proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   336
  have "{x. EX y. P y & Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})" by auto
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   337
  with assms show ?thesis by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   338
qed
29920
b95f5b8b93dd more finiteness
nipkow
parents: 29918
diff changeset
   339
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   340
lemma finite_Plus:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   341
  "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   342
  by (simp add: Plus_def)
17022
b257300c3a9c added Brian Hufmann's finite instances
nipkow
parents: 16775
diff changeset
   343
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   344
lemma finite_PlusD: 
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   345
  fixes A :: "'a set" and B :: "'b set"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   346
  assumes fin: "finite (A <+> B)"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   347
  shows "finite A" "finite B"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   348
proof -
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   349
  have "Inl ` A \<subseteq> A <+> B" by auto
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   350
  then have "finite (Inl ` A :: ('a + 'b) set)" using fin by (rule finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   351
  then show "finite A" by (rule finite_imageD) (auto intro: inj_onI)
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   352
next
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   353
  have "Inr ` B \<subseteq> A <+> B" by auto
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   354
  then have "finite (Inr ` B :: ('a + 'b) set)" using fin by (rule finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   355
  then show "finite B" by (rule finite_imageD) (auto intro: inj_onI)
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   356
qed
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   357
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   358
lemma finite_Plus_iff [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   359
  "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   360
  by (auto intro: finite_PlusD finite_Plus)
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   361
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   362
lemma finite_Plus_UNIV_iff [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   363
  "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   364
  by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   365
40786
0a54cfc9add3 gave more standard finite set rules simp and intro attribute
nipkow
parents: 40716
diff changeset
   366
lemma finite_SigmaI [simp, intro]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   367
  "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) ==> finite (SIGMA a:A. B a)"
40786
0a54cfc9add3 gave more standard finite set rules simp and intro attribute
nipkow
parents: 40716
diff changeset
   368
  by (unfold Sigma_def) blast
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   369
51290
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   370
lemma finite_SigmaI2:
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   371
  assumes "finite {x\<in>A. B x \<noteq> {}}"
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   372
  and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)"
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   373
  shows "finite (Sigma A B)"
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   374
proof -
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   375
  from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)" by auto
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   376
  also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B" by auto
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   377
  finally show ?thesis .
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   378
qed
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   379
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   380
lemma finite_cartesian_product:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   381
  "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)"
15402
97204f3b4705 REorganized Finite_Set
nipkow
parents: 15392
diff changeset
   382
  by (rule finite_SigmaI)
97204f3b4705 REorganized Finite_Set
nipkow
parents: 15392
diff changeset
   383
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   384
lemma finite_Prod_UNIV:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   385
  "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   386
  by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   387
15409
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   388
lemma finite_cartesian_productD1:
42207
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   389
  assumes "finite (A \<times> B)" and "B \<noteq> {}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   390
  shows "finite A"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   391
proof -
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   392
  from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   393
    by (auto simp add: finite_conv_nat_seg_image)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   394
  then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}" by simp
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   395
  with `B \<noteq> {}` have "A = (fst \<circ> f) ` {i::nat. i < n}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   396
    by (simp add: image_compose)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   397
  then have "\<exists>n f. A = f ` {i::nat. i < n}" by blast
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   398
  then show ?thesis
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   399
    by (auto simp add: finite_conv_nat_seg_image)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   400
qed
15409
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   401
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   402
lemma finite_cartesian_productD2:
42207
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   403
  assumes "finite (A \<times> B)" and "A \<noteq> {}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   404
  shows "finite B"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   405
proof -
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   406
  from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   407
    by (auto simp add: finite_conv_nat_seg_image)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   408
  then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}" by simp
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   409
  with `A \<noteq> {}` have "B = (snd \<circ> f) ` {i::nat. i < n}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   410
    by (simp add: image_compose)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   411
  then have "\<exists>n f. B = f ` {i::nat. i < n}" by blast
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   412
  then show ?thesis
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   413
    by (auto simp add: finite_conv_nat_seg_image)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   414
qed
15409
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   415
48175
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   416
lemma finite_prod: 
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   417
  "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   418
by(auto simp add: UNIV_Times_UNIV[symmetric] simp del: UNIV_Times_UNIV 
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   419
   dest: finite_cartesian_productD1 finite_cartesian_productD2)
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   420
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   421
lemma finite_Pow_iff [iff]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   422
  "finite (Pow A) \<longleftrightarrow> finite A"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   423
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   424
  assume "finite (Pow A)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   425
  then have "finite ((%x. {x}) ` A)" by (blast intro: finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   426
  then show "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   427
next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   428
  assume "finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   429
  then show "finite (Pow A)"
35216
7641e8d831d2 get rid of many duplicate simp rule warnings
huffman
parents: 35171
diff changeset
   430
    by induct (simp_all add: Pow_insert)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   431
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   432
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   433
corollary finite_Collect_subsets [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   434
  "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   435
  by (simp add: Pow_def [symmetric])
29918
214755b03df3 more finiteness
nipkow
parents: 29916
diff changeset
   436
48175
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   437
lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)"
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   438
by(simp only: finite_Pow_iff Pow_UNIV[symmetric])
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   439
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   440
lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   441
  by (blast intro: finite_subset [OF subset_Pow_Union])
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   442
53820
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   443
lemma finite_set_of_finite_funs: assumes "finite A" "finite B"
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   444
shows "finite{f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   445
proof-
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   446
  let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   447
  have "?F ` ?S \<subseteq> Pow(A \<times> B)" by auto
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   448
  from finite_subset[OF this] assms have 1: "finite (?F ` ?S)" by simp
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   449
  have 2: "inj_on ?F ?S"
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   450
    by(fastforce simp add: inj_on_def set_eq_iff fun_eq_iff)
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   451
  show ?thesis by(rule finite_imageD[OF 1 2])
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   452
qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   453
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   454
subsubsection {* Further induction rules on finite sets *}
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   455
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   456
lemma finite_ne_induct [case_names singleton insert, consumes 2]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   457
  assumes "finite F" and "F \<noteq> {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   458
  assumes "\<And>x. P {x}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   459
    and "\<And>x F. finite F \<Longrightarrow> F \<noteq> {} \<Longrightarrow> x \<notin> F \<Longrightarrow> P F  \<Longrightarrow> P (insert x F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   460
  shows "P F"
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   461
using assms
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   462
proof induct
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   463
  case empty then show ?case by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   464
next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   465
  case (insert x F) then show ?case by cases auto
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   466
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   467
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   468
lemma finite_subset_induct [consumes 2, case_names empty insert]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   469
  assumes "finite F" and "F \<subseteq> A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   470
  assumes empty: "P {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   471
    and insert: "\<And>a F. finite F \<Longrightarrow> a \<in> A \<Longrightarrow> a \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert a F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   472
  shows "P F"
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   473
using `finite F` `F \<subseteq> A`
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   474
proof induct
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   475
  show "P {}" by fact
31441
428e4caf2299 finite lemmas
nipkow
parents: 31438
diff changeset
   476
next
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   477
  fix x F
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   478
  assume "finite F" and "x \<notin> F" and
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   479
    P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   480
  show "P (insert x F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   481
  proof (rule insert)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   482
    from i show "x \<in> A" by blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   483
    from i have "F \<subseteq> A" by blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   484
    with P show "P F" .
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   485
    show "finite F" by fact
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   486
    show "x \<notin> F" by fact
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   487
  qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   488
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   489
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   490
lemma finite_empty_induct:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   491
  assumes "finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   492
  assumes "P A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   493
    and remove: "\<And>a A. finite A \<Longrightarrow> a \<in> A \<Longrightarrow> P A \<Longrightarrow> P (A - {a})"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   494
  shows "P {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   495
proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   496
  have "\<And>B. B \<subseteq> A \<Longrightarrow> P (A - B)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   497
  proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   498
    fix B :: "'a set"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   499
    assume "B \<subseteq> A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   500
    with `finite A` have "finite B" by (rule rev_finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   501
    from this `B \<subseteq> A` show "P (A - B)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   502
    proof induct
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   503
      case empty
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   504
      from `P A` show ?case by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   505
    next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   506
      case (insert b B)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   507
      have "P (A - B - {b})"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   508
      proof (rule remove)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   509
        from `finite A` show "finite (A - B)" by induct auto
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   510
        from insert show "b \<in> A - B" by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   511
        from insert show "P (A - B)" by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   512
      qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   513
      also have "A - B - {b} = A - insert b B" by (rule Diff_insert [symmetric])
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   514
      finally show ?case .
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   515
    qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   516
  qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   517
  then have "P (A - A)" by blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   518
  then show ?thesis by simp
31441
428e4caf2299 finite lemmas
nipkow
parents: 31438
diff changeset
   519
qed
428e4caf2299 finite lemmas
nipkow
parents: 31438
diff changeset
   520
26441
7914697ff104 no "attach UNIV" any more
haftmann
parents: 26146
diff changeset
   521
subsection {* Class @{text finite}  *}
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   522
29797
08ef36ed2f8a handling type classes without parameters
haftmann
parents: 29675
diff changeset
   523
class finite =
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   524
  assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
27430
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   525
begin
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   526
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   527
lemma finite [simp]: "finite (A \<Colon> 'a set)"
26441
7914697ff104 no "attach UNIV" any more
haftmann
parents: 26146
diff changeset
   528
  by (rule subset_UNIV finite_UNIV finite_subset)+
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   529
43866
8a50dc70cbff moving UNIV = ... equations to their proper theories
haftmann
parents: 42875
diff changeset
   530
lemma finite_code [code]: "finite (A \<Colon> 'a set) \<longleftrightarrow> True"
40922
4d0f96a54e76 adding code equation for finiteness of finite types
bulwahn
parents: 40786
diff changeset
   531
  by simp
4d0f96a54e76 adding code equation for finiteness of finite types
bulwahn
parents: 40786
diff changeset
   532
27430
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   533
end
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   534
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   535
instance prod :: (finite, finite) finite
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   536
  by default (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
26146
61cb176d0385 tuned proofs
haftmann
parents: 26041
diff changeset
   537
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   538
lemma inj_graph: "inj (%f. {(x, y). y = f x})"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39198
diff changeset
   539
  by (rule inj_onI, auto simp add: set_eq_iff fun_eq_iff)
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   540
26146
61cb176d0385 tuned proofs
haftmann
parents: 26041
diff changeset
   541
instance "fun" :: (finite, finite) finite
61cb176d0385 tuned proofs
haftmann
parents: 26041
diff changeset
   542
proof
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   543
  show "finite (UNIV :: ('a => 'b) set)"
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   544
  proof (rule finite_imageD)
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   545
    let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
26792
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   546
    have "range ?graph \<subseteq> Pow UNIV" by simp
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   547
    moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   548
      by (simp only: finite_Pow_iff finite)
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   549
    ultimately show "finite (range ?graph)"
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   550
      by (rule finite_subset)
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   551
    show "inj ?graph" by (rule inj_graph)
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   552
  qed
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   553
qed
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   554
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   555
instance bool :: finite
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   556
  by default (simp add: UNIV_bool)
44831
haftmann
parents: 43991
diff changeset
   557
45962
fc77947a7db4 finite type class instance for `set`
haftmann
parents: 45166
diff changeset
   558
instance set :: (finite) finite
fc77947a7db4 finite type class instance for `set`
haftmann
parents: 45166
diff changeset
   559
  by default (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
fc77947a7db4 finite type class instance for `set`
haftmann
parents: 45166
diff changeset
   560
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   561
instance unit :: finite
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   562
  by default (simp add: UNIV_unit)
44831
haftmann
parents: 43991
diff changeset
   563
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   564
instance sum :: (finite, finite) finite
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   565
  by default (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
27981
feb0c01cf0fb tuned import order
haftmann
parents: 27611
diff changeset
   566
44831
haftmann
parents: 43991
diff changeset
   567
lemma finite_option_UNIV [simp]:
haftmann
parents: 43991
diff changeset
   568
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
haftmann
parents: 43991
diff changeset
   569
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
haftmann
parents: 43991
diff changeset
   570
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   571
instance option :: (finite) finite
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   572
  by default (simp add: UNIV_option_conv)
44831
haftmann
parents: 43991
diff changeset
   573
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   574
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   575
subsection {* A basic fold functional for finite sets *}
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   576
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   577
text {* The intended behaviour is
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51622
diff changeset
   578
@{text "fold f z {x\<^sub>1, ..., x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)"}
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   579
if @{text f} is ``left-commutative'':
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   580
*}
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   581
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   582
locale comp_fun_commute =
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   583
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   584
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   585
begin
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   586
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   587
lemma fun_left_comm: "f y (f x z) = f x (f y z)"
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   588
  using comp_fun_commute by (simp add: fun_eq_iff)
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   589
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   590
lemma commute_left_comp:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   591
  "f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   592
  by (simp add: o_assoc comp_fun_commute)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   593
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   594
end
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   595
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   596
inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   597
for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   598
  emptyI [intro]: "fold_graph f z {} z" |
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   599
  insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   600
      \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   601
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   602
inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   603
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   604
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   605
  "fold f z A = (if finite A then (THE y. fold_graph f z A y) else z)"
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   606
15498
3988e90613d4 comment
paulson
parents: 15497
diff changeset
   607
text{*A tempting alternative for the definiens is
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   608
@{term "if finite A then THE y. fold_graph f z A y else e"}.
15498
3988e90613d4 comment
paulson
parents: 15497
diff changeset
   609
It allows the removal of finiteness assumptions from the theorems
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   610
@{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   611
The proofs become ugly. It is not worth the effort. (???) *}
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   612
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   613
lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   614
by (induct rule: finite_induct) auto
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   615
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   616
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   617
subsubsection{*From @{const fold_graph} to @{term fold}*}
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   618
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   619
context comp_fun_commute
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   620
begin
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   621
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   622
lemma fold_graph_finite:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   623
  assumes "fold_graph f z A y"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   624
  shows "finite A"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   625
  using assms by induct simp_all
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   626
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   627
lemma fold_graph_insertE_aux:
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   628
  "fold_graph f z A y \<Longrightarrow> a \<in> A \<Longrightarrow> \<exists>y'. y = f a y' \<and> fold_graph f z (A - {a}) y'"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   629
proof (induct set: fold_graph)
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   630
  case (insertI x A y) show ?case
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   631
  proof (cases "x = a")
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   632
    assume "x = a" with insertI show ?case by auto
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   633
  next
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   634
    assume "x \<noteq> a"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   635
    then obtain y' where y: "y = f a y'" and y': "fold_graph f z (A - {a}) y'"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   636
      using insertI by auto
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   637
    have "f x y = f a (f x y')"
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   638
      unfolding y by (rule fun_left_comm)
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   639
    moreover have "fold_graph f z (insert x A - {a}) (f x y')"
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   640
      using y' and `x \<noteq> a` and `x \<notin> A`
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   641
      by (simp add: insert_Diff_if fold_graph.insertI)
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   642
    ultimately show ?case by fast
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   643
  qed
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   644
qed simp
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   645
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   646
lemma fold_graph_insertE:
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   647
  assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   648
  obtains y where "v = f x y" and "fold_graph f z A y"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   649
using assms by (auto dest: fold_graph_insertE_aux [OF _ insertI1])
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   650
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   651
lemma fold_graph_determ:
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   652
  "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   653
proof (induct arbitrary: y set: fold_graph)
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   654
  case (insertI x A y v)
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   655
  from `fold_graph f z (insert x A) v` and `x \<notin> A`
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   656
  obtain y' where "v = f x y'" and "fold_graph f z A y'"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   657
    by (rule fold_graph_insertE)
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   658
  from `fold_graph f z A y'` have "y' = y" by (rule insertI)
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   659
  with `v = f x y'` show "v = f x y" by simp
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   660
qed fast
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   661
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   662
lemma fold_equality:
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   663
  "fold_graph f z A y \<Longrightarrow> fold f z A = y"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   664
  by (cases "finite A") (auto simp add: fold_def intro: fold_graph_determ dest: fold_graph_finite)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   665
42272
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   666
lemma fold_graph_fold:
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   667
  assumes "finite A"
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   668
  shows "fold_graph f z A (fold f z A)"
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   669
proof -
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   670
  from assms have "\<exists>x. fold_graph f z A x" by (rule finite_imp_fold_graph)
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   671
  moreover note fold_graph_determ
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   672
  ultimately have "\<exists>!x. fold_graph f z A x" by (rule ex_ex1I)
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   673
  then have "fold_graph f z A (The (fold_graph f z A))" by (rule theI')
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   674
  with assms show ?thesis by (simp add: fold_def)
42272
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   675
qed
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   676
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   677
text {* The base case for @{text fold}: *}
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   678
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   679
lemma (in -) fold_infinite [simp]:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   680
  assumes "\<not> finite A"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   681
  shows "fold f z A = z"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   682
  using assms by (auto simp add: fold_def)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   683
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   684
lemma (in -) fold_empty [simp]:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   685
  "fold f z {} = z"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   686
  by (auto simp add: fold_def)
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   687
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   688
text{* The various recursion equations for @{const fold}: *}
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   689
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   690
lemma fold_insert [simp]:
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   691
  assumes "finite A" and "x \<notin> A"
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   692
  shows "fold f z (insert x A) = f x (fold f z A)"
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   693
proof (rule fold_equality)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   694
  fix z
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   695
  from `finite A` have "fold_graph f z A (fold f z A)" by (rule fold_graph_fold)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   696
  with `x \<notin> A` have "fold_graph f z (insert x A) (f x (fold f z A))" by (rule fold_graph.insertI)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   697
  then show "fold_graph f z (insert x A) (f x (fold f z A))" by simp
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   698
qed
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   699
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   700
declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   701
  -- {* No more proofs involve these. *}
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   702
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   703
lemma fold_fun_left_comm:
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   704
  "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   705
proof (induct rule: finite_induct)
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   706
  case empty then show ?case by simp
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   707
next
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   708
  case (insert y A) then show ?case
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   709
    by (simp add: fun_left_comm [of x])
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   710
qed
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   711
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   712
lemma fold_insert2:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   713
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A)  = fold f (f x z) A"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   714
  by (simp add: fold_fun_left_comm)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   715
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   716
lemma fold_rec:
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   717
  assumes "finite A" and "x \<in> A"
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   718
  shows "fold f z A = f x (fold f z (A - {x}))"
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   719
proof -
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   720
  have A: "A = insert x (A - {x})" using `x \<in> A` by blast
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   721
  then have "fold f z A = fold f z (insert x (A - {x}))" by simp
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   722
  also have "\<dots> = f x (fold f z (A - {x}))"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   723
    by (rule fold_insert) (simp add: `finite A`)+
15535
nipkow
parents: 15532
diff changeset
   724
  finally show ?thesis .
nipkow
parents: 15532
diff changeset
   725
qed
nipkow
parents: 15532
diff changeset
   726
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   727
lemma fold_insert_remove:
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   728
  assumes "finite A"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   729
  shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   730
proof -
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   731
  from `finite A` have "finite (insert x A)" by auto
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   732
  moreover have "x \<in> insert x A" by auto
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   733
  ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   734
    by (rule fold_rec)
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   735
  then show ?thesis by simp
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   736
qed
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   737
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   738
end
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   739
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   740
text{* Other properties of @{const fold}: *}
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   741
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   742
lemma fold_image:
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   743
  assumes "inj_on g A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   744
  shows "fold f z (g ` A) = fold (f \<circ> g) z A"
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   745
proof (cases "finite A")
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   746
  case False with assms show ?thesis by (auto dest: finite_imageD simp add: fold_def)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   747
next
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   748
  case True
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   749
  have "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   750
  proof
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   751
    fix w
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   752
    show "fold_graph f z (g ` A) w \<longleftrightarrow> fold_graph (f \<circ> g) z A w" (is "?P \<longleftrightarrow> ?Q")
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   753
    proof
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   754
      assume ?P then show ?Q using assms
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   755
      proof (induct "g ` A" w arbitrary: A)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   756
        case emptyI then show ?case by (auto intro: fold_graph.emptyI)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   757
      next
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   758
        case (insertI x A r B)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   759
        from `inj_on g B` `x \<notin> A` `insert x A = image g B` obtain x' A' where
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   760
          "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   761
          by (rule inj_img_insertE)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   762
        from insertI.prems have "fold_graph (f o g) z A' r"
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   763
          by (auto intro: insertI.hyps)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   764
        with `x' \<notin> A'` have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   765
          by (rule fold_graph.insertI)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   766
        then show ?case by simp
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   767
      qed
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   768
    next
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   769
      assume ?Q then show ?P using assms
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   770
      proof induct
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   771
        case emptyI thus ?case by (auto intro: fold_graph.emptyI)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   772
      next
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   773
        case (insertI x A r)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   774
        from `x \<notin> A` insertI.prems have "g x \<notin> g ` A" by auto
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   775
        moreover from insertI have "fold_graph f z (g ` A) r" by simp
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   776
        ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   777
          by (rule fold_graph.insertI)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   778
        then show ?case by simp
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   779
      qed
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   780
    qed
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   781
  qed
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   782
  with True assms show ?thesis by (auto simp add: fold_def)
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   783
qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   784
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   785
lemma fold_cong:
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   786
  assumes "comp_fun_commute f" "comp_fun_commute g"
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   787
  assumes "finite A" and cong: "\<And>x. x \<in> A \<Longrightarrow> f x = g x"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   788
    and "s = t" and "A = B"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   789
  shows "fold f s A = fold g t B"
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   790
proof -
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   791
  have "fold f s A = fold g s A"  
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   792
  using `finite A` cong proof (induct A)
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   793
    case empty then show ?case by simp
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   794
  next
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   795
    case (insert x A)
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   796
    interpret f: comp_fun_commute f by (fact `comp_fun_commute f`)
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   797
    interpret g: comp_fun_commute g by (fact `comp_fun_commute g`)
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   798
    from insert show ?case by simp
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   799
  qed
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   800
  with assms show ?thesis by simp
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   801
qed
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   802
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
   803
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   804
text {* A simplified version for idempotent functions: *}
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
   805
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   806
locale comp_fun_idem = comp_fun_commute +
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   807
  assumes comp_fun_idem: "f x \<circ> f x = f x"
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   808
begin
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   809
42869
43b0f61f56d0 use point-free characterization for locale fun_left_comm_idem
haftmann
parents: 42809
diff changeset
   810
lemma fun_left_idem: "f x (f x z) = f x z"
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   811
  using comp_fun_idem by (simp add: fun_eq_iff)
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   812
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   813
lemma fold_insert_idem:
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   814
  assumes fin: "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   815
  shows "fold f z (insert x A)  = f x (fold f z A)"
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
   816
proof cases
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   817
  assume "x \<in> A"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   818
  then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   819
  then show ?thesis using assms by (simp add: comp_fun_idem fun_left_idem)
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
   820
next
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   821
  assume "x \<notin> A" then show ?thesis using assms by simp
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
   822
qed
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
   823
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   824
declare fold_insert [simp del] fold_insert_idem [simp]
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   825
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   826
lemma fold_insert_idem2:
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   827
  "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   828
  by (simp add: fold_fun_left_comm)
15484
2636ec211ec8 fold and fol1 changes
nipkow
parents: 15483
diff changeset
   829
26041
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   830
end
c2e15e65165f locales ACf, ACIf, ACIfSL and ACIfSLlin have been abandoned in favour of the existing algebraic classes ab_semigroup_mult, ab_semigroup_idem_mult, lower_semilattice (resp. uper_semilattice) and linorder
haftmann
parents: 25571
diff changeset
   831
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   832
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   833
subsubsection {* Liftings to @{text comp_fun_commute} etc. *}
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   834
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   835
lemma (in comp_fun_commute) comp_comp_fun_commute:
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   836
  "comp_fun_commute (f \<circ> g)"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   837
proof
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   838
qed (simp_all add: comp_fun_commute)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   839
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   840
lemma (in comp_fun_idem) comp_comp_fun_idem:
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   841
  "comp_fun_idem (f \<circ> g)"
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   842
  by (rule comp_fun_idem.intro, rule comp_comp_fun_commute, unfold_locales)
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   843
    (simp_all add: comp_fun_idem)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   844
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   845
lemma (in comp_fun_commute) comp_fun_commute_funpow:
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   846
  "comp_fun_commute (\<lambda>x. f x ^^ g x)"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   847
proof
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   848
  fix y x
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   849
  show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   850
  proof (cases "x = y")
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   851
    case True then show ?thesis by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   852
  next
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   853
    case False show ?thesis
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   854
    proof (induct "g x" arbitrary: g)
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   855
      case 0 then show ?case by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   856
    next
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   857
      case (Suc n g)
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   858
      have hyp1: "f y ^^ g y \<circ> f x = f x \<circ> f y ^^ g y"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   859
      proof (induct "g y" arbitrary: g)
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   860
        case 0 then show ?case by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   861
      next
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   862
        case (Suc n g)
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   863
        def h \<equiv> "\<lambda>z. g z - 1"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   864
        with Suc have "n = h y" by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   865
        with Suc have hyp: "f y ^^ h y \<circ> f x = f x \<circ> f y ^^ h y"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   866
          by auto
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   867
        from Suc h_def have "g y = Suc (h y)" by simp
49739
13aa6d8268ec consolidated names of theorems on composition;
haftmann
parents: 49724
diff changeset
   868
        then show ?case by (simp add: comp_assoc hyp)
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   869
          (simp add: o_assoc comp_fun_commute)
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   870
      qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   871
      def h \<equiv> "\<lambda>z. if z = x then g x - 1 else g z"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   872
      with Suc have "n = h x" by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   873
      with Suc have "f y ^^ h y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ h y"
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   874
        by auto
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   875
      with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y" by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   876
      from Suc h_def have "g x = Suc (h x)" by simp
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   877
      then show ?case by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2)
49739
13aa6d8268ec consolidated names of theorems on composition;
haftmann
parents: 49724
diff changeset
   878
        (simp add: comp_assoc hyp1)
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   879
    qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   880
  qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   881
qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   882
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   883
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   884
subsubsection {* Expressing set operations via @{const fold} *}
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
   885
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   886
lemma comp_fun_commute_const:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   887
  "comp_fun_commute (\<lambda>_. f)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   888
proof
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   889
qed rule
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   890
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   891
lemma comp_fun_idem_insert:
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   892
  "comp_fun_idem insert"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   893
proof
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   894
qed auto
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   895
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   896
lemma comp_fun_idem_remove:
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
   897
  "comp_fun_idem Set.remove"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   898
proof
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   899
qed auto
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
   900
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   901
lemma (in semilattice_inf) comp_fun_idem_inf:
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   902
  "comp_fun_idem inf"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   903
proof
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   904
qed (auto simp add: inf_left_commute)
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   905
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   906
lemma (in semilattice_sup) comp_fun_idem_sup:
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   907
  "comp_fun_idem sup"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   908
proof
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   909
qed (auto simp add: sup_left_commute)
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
   910
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   911
lemma union_fold_insert:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   912
  assumes "finite A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   913
  shows "A \<union> B = fold insert B A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   914
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
   915
  interpret comp_fun_idem insert by (fact comp_fun_idem_insert)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   916
  from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   917
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
   918
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   919
lemma minus_fold_remove:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   920
  assumes "finite A"
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
   921
  shows "B - A = fold Set.remove B A"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   922
proof -
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
   923
  interpret comp_fun_idem Set.remove by (fact comp_fun_idem_remove)
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
   924
  from `finite A` have "fold Set.remove B A = B - A" by (induct A arbitrary: B) auto
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
   925
  then show ?thesis ..
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   926
qed
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
   927
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   928
lemma comp_fun_commute_filter_fold:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   929
  "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   930
proof - 
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   931
  interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   932
  show ?thesis by default (auto simp: fun_eq_iff)
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   933
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   934
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   935
lemma Set_filter_fold:
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   936
  assumes "finite A"
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   937
  shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   938
using assms
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   939
by (induct A) 
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   940
  (auto simp add: Set.filter_def comp_fun_commute.fold_insert[OF comp_fun_commute_filter_fold])
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   941
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   942
lemma inter_Set_filter:     
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   943
  assumes "finite B"
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   944
  shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   945
using assms 
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
   946
by (induct B) (auto simp: Set.filter_def)
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   947
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   948
lemma image_fold_insert:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   949
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   950
  shows "image f A = fold (\<lambda>k A. Set.insert (f k) A) {} A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   951
using assms
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   952
proof -
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   953
  interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A" by default auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   954
  show ?thesis using assms by (induct A) auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   955
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   956
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   957
lemma Ball_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   958
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   959
  shows "Ball A P = fold (\<lambda>k s. s \<and> P k) True A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   960
using assms
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   961
proof -
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   962
  interpret comp_fun_commute "\<lambda>k s. s \<and> P k" by default auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   963
  show ?thesis using assms by (induct A) auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   964
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   965
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   966
lemma Bex_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   967
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   968
  shows "Bex A P = fold (\<lambda>k s. s \<or> P k) False A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   969
using assms
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   970
proof -
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   971
  interpret comp_fun_commute "\<lambda>k s. s \<or> P k" by default auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   972
  show ?thesis using assms by (induct A) auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   973
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   974
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   975
lemma comp_fun_commute_Pow_fold: 
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   976
  "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)" 
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   977
  by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   978
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   979
lemma Pow_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   980
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   981
  shows "Pow A = fold (\<lambda>x A. A \<union> Set.insert x ` A) {{}} A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   982
using assms
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   983
proof -
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   984
  interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A" by (rule comp_fun_commute_Pow_fold)
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   985
  show ?thesis using assms by (induct A) (auto simp: Pow_insert)
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   986
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   987
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   988
lemma fold_union_pair:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   989
  assumes "finite B"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   990
  shows "(\<Union>y\<in>B. {(x, y)}) \<union> A = fold (\<lambda>y. Set.insert (x, y)) A B"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   991
proof -
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   992
  interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)" by default auto
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   993
  show ?thesis using assms  by (induct B arbitrary: A) simp_all
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   994
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   995
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   996
lemma comp_fun_commute_product_fold: 
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   997
  assumes "finite B"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   998
  shows "comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)" 
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   999
by default (auto simp: fold_union_pair[symmetric] assms)
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1000
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1001
lemma product_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1002
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1003
  assumes "finite B"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1004
  shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1005
using assms unfolding Sigma_def 
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1006
by (induct A) 
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1007
  (simp_all add: comp_fun_commute.fold_insert[OF comp_fun_commute_product_fold] fold_union_pair)
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1008
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1009
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1010
context complete_lattice
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1011
begin
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1012
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1013
lemma inf_Inf_fold_inf:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1014
  assumes "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1015
  shows "inf (Inf A) B = fold inf B A"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1016
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1017
  interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1018
  from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1019
    (simp_all add: inf_commute fun_eq_iff)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1020
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1021
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1022
lemma sup_Sup_fold_sup:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1023
  assumes "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1024
  shows "sup (Sup A) B = fold sup B A"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1025
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1026
  interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1027
  from `finite A` fold_fun_left_comm show ?thesis by (induct A arbitrary: B)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1028
    (simp_all add: sup_commute fun_eq_iff)
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1029
qed
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1030
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1031
lemma Inf_fold_inf:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1032
  assumes "finite A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1033
  shows "Inf A = fold inf top A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1034
  using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1035
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1036
lemma Sup_fold_sup:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1037
  assumes "finite A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1038
  shows "Sup A = fold sup bot A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1039
  using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1040
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1041
lemma inf_INF_fold_inf:
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1042
  assumes "finite A"
42873
da1253ff1764 point-free characterization of operations on finite sets
haftmann
parents: 42871
diff changeset
  1043
  shows "inf B (INFI A f) = fold (inf \<circ> f) B A" (is "?inf = ?fold") 
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1044
proof (rule sym)
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1045
  interpret comp_fun_idem inf by (fact comp_fun_idem_inf)
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1046
  interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
42873
da1253ff1764 point-free characterization of operations on finite sets
haftmann
parents: 42871
diff changeset
  1047
  from `finite A` show "?fold = ?inf"
42869
43b0f61f56d0 use point-free characterization for locale fun_left_comm_idem
haftmann
parents: 42809
diff changeset
  1048
    by (induct A arbitrary: B)
44928
7ef6505bde7f renamed Complete_Lattices lemmas, removed legacy names
hoelzl
parents: 44919
diff changeset
  1049
      (simp_all add: INF_def inf_left_commute)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1050
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1051
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1052
lemma sup_SUP_fold_sup:
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1053
  assumes "finite A"
42873
da1253ff1764 point-free characterization of operations on finite sets
haftmann
parents: 42871
diff changeset
  1054
  shows "sup B (SUPR A f) = fold (sup \<circ> f) B A" (is "?sup = ?fold") 
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1055
proof (rule sym)
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1056
  interpret comp_fun_idem sup by (fact comp_fun_idem_sup)
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1057
  interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
42873
da1253ff1764 point-free characterization of operations on finite sets
haftmann
parents: 42871
diff changeset
  1058
  from `finite A` show "?fold = ?sup"
42869
43b0f61f56d0 use point-free characterization for locale fun_left_comm_idem
haftmann
parents: 42809
diff changeset
  1059
    by (induct A arbitrary: B)
44928
7ef6505bde7f renamed Complete_Lattices lemmas, removed legacy names
hoelzl
parents: 44919
diff changeset
  1060
      (simp_all add: SUP_def sup_left_commute)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1061
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1062
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1063
lemma INF_fold_inf:
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1064
  assumes "finite A"
42873
da1253ff1764 point-free characterization of operations on finite sets
haftmann
parents: 42871
diff changeset
  1065
  shows "INFI A f = fold (inf \<circ> f) top A"
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1066
  using assms inf_INF_fold_inf [of A top] by simp
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1067
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1068
lemma SUP_fold_sup:
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1069
  assumes "finite A"
42873
da1253ff1764 point-free characterization of operations on finite sets
haftmann
parents: 42871
diff changeset
  1070
  shows "SUPR A f = fold (sup \<circ> f) bot A"
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1071
  using assms sup_SUP_fold_sup [of A bot] by simp
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1072
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1073
end
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1074
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1075
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1076
subsection {* Locales as mini-packages for fold operations *}
34007
aea892559fc5 tuned lattices theory fragements; generlized some lemmas from sets to lattices
haftmann
parents: 33960
diff changeset
  1077
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1078
subsubsection {* The natural case *}
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1079
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1080
locale folding =
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1081
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1082
  fixes z :: "'b"
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1083
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1084
begin
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1085
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1086
definition F :: "'a set \<Rightarrow> 'b"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1087
where
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1088
  eq_fold: "F A = fold f z A"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1089
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1090
lemma empty [simp]:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1091
  "F {} = z"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1092
  by (simp add: eq_fold)
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1093
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1094
lemma infinite [simp]:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1095
  "\<not> finite A \<Longrightarrow> F A = z"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1096
  by (simp add: eq_fold)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1097
 
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1098
lemma insert [simp]:
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1099
  assumes "finite A" and "x \<notin> A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1100
  shows "F (insert x A) = f x (F A)"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1101
proof -
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
  1102
  interpret comp_fun_commute f
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
  1103
    by default (insert comp_fun_commute, simp add: fun_eq_iff)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1104
  from fold_insert assms
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1105
  have "fold f z (insert x A) = f x (fold f z A)" by simp
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39198
diff changeset
  1106
  with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1107
qed
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1108
 
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1109
lemma remove:
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1110
  assumes "finite A" and "x \<in> A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1111
  shows "F A = f x (F (A - {x}))"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1112
proof -
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1113
  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1114
    by (auto dest: mk_disjoint_insert)
53374
a14d2a854c02 tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents: 53015
diff changeset
  1115
  moreover from `finite A` A have "finite B" by simp
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1116
  ultimately show ?thesis by simp
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1117
qed
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1118
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1119
lemma insert_remove:
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1120
  assumes "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1121
  shows "F (insert x A) = f x (F (A - {x}))"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1122
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1123
34007
aea892559fc5 tuned lattices theory fragements; generlized some lemmas from sets to lattices
haftmann
parents: 33960
diff changeset
  1124
end
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1125
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1126
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1127
subsubsection {* With idempotency *}
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1128
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1129
locale folding_idem = folding +
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1130
  assumes comp_fun_idem: "f x \<circ> f x = f x"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1131
begin
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1132
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1133
declare insert [simp del]
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1134
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1135
lemma insert_idem [simp]:
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1136
  assumes "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1137
  shows "F (insert x A) = f x (F A)"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1138
proof -
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1139
  interpret comp_fun_idem f
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1140
    by default (insert comp_fun_commute comp_fun_idem, simp add: fun_eq_iff)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1141
  from fold_insert_idem assms
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1142
  have "fold f z (insert x A) = f x (fold f z A)" by simp
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1143
  with `finite A` show ?thesis by (simp add: eq_fold fun_eq_iff)
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1144
qed
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1145
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1146
end
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1147
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1148
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1149
subsection {* Finite cardinality *}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1150
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1151
text {*
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1152
  The traditional definition
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1153
  @{prop "card A \<equiv> LEAST n. EX f. A = {f i | i. i < n}"}
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1154
  is ugly to work with.
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1155
  But now that we have @{const fold} things are easy:
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1156
*}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1157
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1158
definition card :: "'a set \<Rightarrow> nat" where
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1159
  "card = folding.F (\<lambda>_. Suc) 0"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1160
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1161
interpretation card!: folding "\<lambda>_. Suc" 0
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1162
where
51546
2e26df807dc7 more uniform style for interpretation and sublocale declarations
haftmann
parents: 51489
diff changeset
  1163
  "folding.F (\<lambda>_. Suc) 0 = card"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1164
proof -
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1165
  show "folding (\<lambda>_. Suc)" by default rule
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1166
  then interpret card!: folding "\<lambda>_. Suc" 0 .
51546
2e26df807dc7 more uniform style for interpretation and sublocale declarations
haftmann
parents: 51489
diff changeset
  1167
  from card_def show "folding.F (\<lambda>_. Suc) 0 = card" by rule
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1168
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1169
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1170
lemma card_infinite:
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1171
  "\<not> finite A \<Longrightarrow> card A = 0"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1172
  by (fact card.infinite)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1173
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1174
lemma card_empty:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1175
  "card {} = 0"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1176
  by (fact card.empty)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1177
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1178
lemma card_insert_disjoint:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1179
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1180
  by (fact card.insert)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1181
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1182
lemma card_insert_if:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1183
  "finite A \<Longrightarrow> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1184
  by auto (simp add: card.insert_remove card.remove)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1185
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1186
lemma card_ge_0_finite:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1187
  "card A > 0 \<Longrightarrow> finite A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1188
  by (rule ccontr) simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1189
54148
c8cc5ab4a863 killed more "no_atp"s
blanchet
parents: 54147
diff changeset
  1190
lemma card_0_eq [simp]:
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1191
  "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1192
  by (auto dest: mk_disjoint_insert)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1193
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1194
lemma finite_UNIV_card_ge_0:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1195
  "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1196
  by (rule ccontr) simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1197
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1198
lemma card_eq_0_iff:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1199
  "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1200
  by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1201
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1202
lemma card_gt_0_iff:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1203
  "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1204
  by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1205
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1206
lemma card_Suc_Diff1:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1207
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> Suc (card (A - {x})) = card A"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1208
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1209
apply(simp del:insert_Diff_single)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1210
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1211
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1212
lemma card_Diff_singleton:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1213
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) = card A - 1"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1214
  by (simp add: card_Suc_Diff1 [symmetric])
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1215
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1216
lemma card_Diff_singleton_if:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1217
  "finite A \<Longrightarrow> card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1218
  by (simp add: card_Diff_singleton)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1219
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1220
lemma card_Diff_insert[simp]:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1221
  assumes "finite A" and "a \<in> A" and "a \<notin> B"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1222
  shows "card (A - insert a B) = card (A - B) - 1"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1223
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1224
  have "A - insert a B = (A - B) - {a}" using assms by blast
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1225
  then show ?thesis using assms by(simp add: card_Diff_singleton)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1226
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1227
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1228
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1229
  by (fact card.insert_remove)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1230
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1231
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1232
by (simp add: card_insert_if)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1233
41987
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1234
lemma card_Collect_less_nat[simp]: "card{i::nat. i < n} = n"
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1235
by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1236
41988
c2583bbb92f5 tuned lemma
nipkow
parents: 41987
diff changeset
  1237
lemma card_Collect_le_nat[simp]: "card{i::nat. i <= n} = Suc n"
41987
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1238
using card_Collect_less_nat[of "Suc n"] by(simp add: less_Suc_eq_le)
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1239
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1240
lemma card_mono:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1241
  assumes "finite B" and "A \<subseteq> B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1242
  shows "card A \<le> card B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1243
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1244
  from assms have "finite A" by (auto intro: finite_subset)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1245
  then show ?thesis using assms proof (induct A arbitrary: B)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1246
    case empty then show ?case by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1247
  next
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1248
    case (insert x A)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1249
    then have "x \<in> B" by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1250
    from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1251
    with insert.hyps have "card A \<le> card (B - {x})" by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1252
    with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1253
  qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1254
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1255
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1256
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
  1257
apply (induct rule: finite_induct)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
  1258
apply simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
  1259
apply clarify
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1260
apply (subgoal_tac "finite A & A - {x} <= F")
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1261
 prefer 2 apply (blast intro: finite_subset, atomize)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1262
apply (drule_tac x = "A - {x}" in spec)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1263
apply (simp add: card_Diff_singleton_if split add: split_if_asm)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1264
apply (case_tac "card A", auto)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1265
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1266
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1267
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1268
apply (simp add: psubset_eq linorder_not_le [symmetric])
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1269
apply (blast dest: card_seteq)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1270
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1271
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1272
lemma card_Un_Int:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1273
  assumes "finite A" and "finite B"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1274
  shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1275
using assms proof (induct A)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1276
  case empty then show ?case by simp
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1277
next
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1278
 case (insert x A) then show ?case
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1279
    by (auto simp add: insert_absorb Int_insert_left)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1280
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1281
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1282
lemma card_Un_disjoint:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1283
  assumes "finite A" and "finite B"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1284
  assumes "A \<inter> B = {}"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1285
  shows "card (A \<union> B) = card A + card B"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1286
using assms card_Un_Int [of A B] by simp
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1287
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1288
lemma card_Diff_subset:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1289
  assumes "finite B" and "B \<subseteq> A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1290
  shows "card (A - B) = card A - card B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1291
proof (cases "finite A")
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1292
  case False with assms show ?thesis by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1293
next
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1294
  case True with assms show ?thesis by (induct B arbitrary: A) simp_all
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1295
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1296
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1297
lemma card_Diff_subset_Int:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1298
  assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1299
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1300
  have "A - B = A - A \<inter> B" by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1301
  thus ?thesis
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1302
    by (simp add: card_Diff_subset AB) 
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1303
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1304
40716
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1305
lemma diff_card_le_card_Diff:
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1306
assumes "finite B" shows "card A - card B \<le> card(A - B)"
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1307
proof-
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1308
  have "card A - card B \<le> card A - card (A \<inter> B)"
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1309
    using card_mono[OF assms Int_lower2, of A] by arith
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1310
  also have "\<dots> = card(A-B)" using assms by(simp add: card_Diff_subset_Int)
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1311
  finally show ?thesis .
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1312
qed
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1313
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1314
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1315
apply (rule Suc_less_SucD)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1316
apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1317
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1318
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1319
lemma card_Diff2_less:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1320
  "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1321
apply (case_tac "x = y")
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1322
 apply (simp add: card_Diff1_less del:card_Diff_insert)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1323
apply (rule less_trans)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1324
 prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1325
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1326
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1327
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1328
apply (case_tac "x : A")
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1329
 apply (simp_all add: card_Diff1_less less_imp_le)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1330
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1331
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1332
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1333
by (erule psubsetI, blast)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1334
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1335
lemma card_le_inj:
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1336
  assumes fA: "finite A"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1337
    and fB: "finite B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1338
    and c: "card A \<le> card B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1339
  shows "\<exists>f. f ` A \<subseteq> B \<and> inj_on f A"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1340
  using fA fB c
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1341
proof (induct arbitrary: B rule: finite_induct)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1342
  case empty
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1343
  then show ?case by simp
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1344
next
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1345
  case (insert x s t)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1346
  then show ?case
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1347
  proof (induct rule: finite_induct[OF "insert.prems"(1)])
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1348
    case 1
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1349
    then show ?case by simp
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1350
  next
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1351
    case (2 y t)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1352
    from "2.prems"(1,2,5) "2.hyps"(1,2) have cst: "card s \<le> card t"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1353
      by simp
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1354
    from "2.prems"(3) [OF "2.hyps"(1) cst]
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1355
    obtain f where "f ` s \<subseteq> t" "inj_on f s"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1356
      by blast
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1357
    with "2.prems"(2) "2.hyps"(2) show ?case
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1358
      apply -
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1359
      apply (rule exI[where x = "\<lambda>z. if z = x then y else f z"])
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1360
      apply (auto simp add: inj_on_def)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1361
      done
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1362
  qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1363
qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1364
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1365
lemma card_subset_eq:
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1366
  assumes fB: "finite B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1367
    and AB: "A \<subseteq> B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1368
    and c: "card A = card B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1369
  shows "A = B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1370
proof -
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1371
  from fB AB have fA: "finite A"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1372
    by (auto intro: finite_subset)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1373
  from fA fB have fBA: "finite (B - A)"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1374
    by auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1375
  have e: "A \<inter> (B - A) = {}"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1376
    by blast
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1377
  have eq: "A \<union> (B - A) = B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1378
    using AB by blast
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1379
  from card_Un_disjoint[OF fA fBA e, unfolded eq c] have "card (B - A) = 0"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1380
    by arith
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1381
  then have "B - A = {}"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1382
    unfolding card_eq_0_iff using fA fB by simp
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1383
  with AB show "A = B"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1384
    by blast
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1385
qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1386
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1387
lemma insert_partition:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1388
  "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1389
  \<Longrightarrow> x \<inter> \<Union> F = {}"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1390
by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1391
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1392
lemma finite_psubset_induct[consumes 1, case_names psubset]:
36079
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1393
  assumes fin: "finite A" 
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1394
  and     major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P A" 
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1395
  shows "P A"
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1396
using fin
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1397
proof (induct A taking: card rule: measure_induct_rule)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1398
  case (less A)
36079
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1399
  have fin: "finite A" by fact
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1400
  have ih: "\<And>B. \<lbrakk>card B < card A; finite B\<rbrakk> \<Longrightarrow> P B" by fact
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1401
  { fix B 
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1402
    assume asm: "B \<subset> A"
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1403
    from asm have "card B < card A" using psubset_card_mono fin by blast
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1404
    moreover
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1405
    from asm have "B \<subseteq> A" by auto
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1406
    then have "finite B" using fin finite_subset by blast
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1407
    ultimately 
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1408
    have "P B" using ih by simp
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1409
  }
fa0e354e6a39 simplified induction case in finite_psubset_induct; tuned the proof that uses this induction principle
Christian Urban <urbanc@in.tum.de>
parents: 36045
diff changeset
  1410
  with fin show "P A" using major by blast
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1411
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1412
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1413
lemma finite_induct_select[consumes 1, case_names empty select]:
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1414
  assumes "finite S"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1415
  assumes "P {}"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1416
  assumes select: "\<And>T. T \<subset> S \<Longrightarrow> P T \<Longrightarrow> \<exists>s\<in>S - T. P (insert s T)"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1417
  shows "P S"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1418
proof -
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1419
  have "0 \<le> card S" by simp
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1420
  then have "\<exists>T \<subseteq> S. card T = card S \<and> P T"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1421
  proof (induct rule: dec_induct)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1422
    case base with `P {}` show ?case
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1423
      by (intro exI[of _ "{}"]) auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1424
  next
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1425
    case (step n)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1426
    then obtain T where T: "T \<subseteq> S" "card T = n" "P T"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1427
      by auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1428
    with `n < card S` have "T \<subset> S" "P T"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1429
      by auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1430
    with select[of T] obtain s where "s \<in> S" "s \<notin> T" "P (insert s T)"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1431
      by auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1432
    with step(2) T `finite S` show ?case
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1433
      by (intro exI[of _ "insert s T"]) (auto dest: finite_subset)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1434
  qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1435
  with `finite S` show "P S"
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1436
    by (auto dest: card_subset_eq)
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1437
qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1438
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1439
text{* main cardinality theorem *}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1440
lemma card_partition [rule_format]:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1441
  "finite C ==>
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1442
     finite (\<Union> C) -->
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1443
     (\<forall>c\<in>C. card c = k) -->
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1444
     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1445
     k * card(C) = card (\<Union> C)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1446
apply (erule finite_induct, simp)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1447
apply (simp add: card_Un_disjoint insert_partition 
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1448
       finite_subset [of _ "\<Union> (insert x F)"])
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1449
done
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1450
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1451
lemma card_eq_UNIV_imp_eq_UNIV:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1452
  assumes fin: "finite (UNIV :: 'a set)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1453
  and card: "card A = card (UNIV :: 'a set)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1454
  shows "A = (UNIV :: 'a set)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1455
proof
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1456
  show "A \<subseteq> UNIV" by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1457
  show "UNIV \<subseteq> A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1458
  proof
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1459
    fix x
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1460
    show "x \<in> A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1461
    proof (rule ccontr)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1462
      assume "x \<notin> A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1463
      then have "A \<subset> UNIV" by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1464
      with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1465
      with card show False by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1466
    qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1467
  qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1468
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1469
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1470
text{*The form of a finite set of given cardinality*}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1471
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1472
lemma card_eq_SucD:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1473
assumes "card A = Suc k"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1474
shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1475
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1476
  have fin: "finite A" using assms by (auto intro: ccontr)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1477
  moreover have "card A \<noteq> 0" using assms by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1478
  ultimately obtain b where b: "b \<in> A" by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1479
  show ?thesis
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1480
  proof (intro exI conjI)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1481
    show "A = insert b (A-{b})" using b by blast
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1482
    show "b \<notin> A - {b}" by blast
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1483
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44835
diff changeset
  1484
      using assms b fin by(fastforce dest:mk_disjoint_insert)+
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1485
  qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1486
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1487
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1488
lemma card_Suc_eq:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1489
  "(card A = Suc k) =
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1490
   (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1491
 apply(auto elim!: card_eq_SucD)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1492
 apply(subst card.insert)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1493
 apply(auto simp add: intro:ccontr)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1494
 done
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1495
44744
bdf8eb8f126b added new lemmas
nipkow
parents: 43991
diff changeset
  1496
lemma card_le_Suc_iff: "finite A \<Longrightarrow>
bdf8eb8f126b added new lemmas
nipkow
parents: 43991
diff changeset
  1497
  Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44835
diff changeset
  1498
by (fastforce simp: card_Suc_eq less_eq_nat.simps(2) insert_eq_iff
44744
bdf8eb8f126b added new lemmas
nipkow
parents: 43991
diff changeset
  1499
  dest: subset_singletonD split: nat.splits if_splits)
bdf8eb8f126b added new lemmas
nipkow
parents: 43991
diff changeset
  1500
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1501
lemma finite_fun_UNIVD2:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1502
  assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1503
  shows "finite (UNIV :: 'b set)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1504
proof -
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1505
  from fin have "\<And>arbitrary. finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1506
    by (rule finite_imageI)
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1507
  moreover have "\<And>arbitrary. UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1508
    by (rule UNIV_eq_I) auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1509
  ultimately show "finite (UNIV :: 'b set)" by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1510
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1511
48063
f02b4302d5dd remove duplicate lemma card_unit in favor of Finite_Set.card_UNIV_unit
huffman
parents: 47221
diff changeset
  1512
lemma card_UNIV_unit [simp]: "card (UNIV :: unit set) = 1"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1513
  unfolding UNIV_unit by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1514
47210
b1dd32b2a505 move lemma card_UNIV_bool from Nat_Numeral.thy to Finite_Set.thy
huffman
parents: 46898
diff changeset
  1515
lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
b1dd32b2a505 move lemma card_UNIV_bool from Nat_Numeral.thy to Finite_Set.thy
huffman
parents: 46898
diff changeset
  1516
  unfolding UNIV_bool by simp
b1dd32b2a505 move lemma card_UNIV_bool from Nat_Numeral.thy to Finite_Set.thy
huffman
parents: 46898
diff changeset
  1517
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1518
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1519
subsubsection {* Cardinality of image *}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1520
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1521
lemma card_image_le: "finite A ==> card (f ` A) \<le> card A"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1522
  by (induct rule: finite_induct) (simp_all add: le_SucI card_insert_if)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1523
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1524
lemma card_image:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1525
  assumes "inj_on f A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1526
  shows "card (f ` A) = card A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1527
proof (cases "finite A")
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1528
  case True then show ?thesis using assms by (induct A) simp_all
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1529
next
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1530
  case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1531
  with False show ?thesis by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1532
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1533
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1534
lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1535
by(auto simp: card_image bij_betw_def)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1536
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1537
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1538
by (simp add: card_seteq card_image)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1539
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1540
lemma eq_card_imp_inj_on:
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1541
  assumes "finite A" "card(f ` A) = card A" shows "inj_on f A"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1542
using assms
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1543
proof (induct rule:finite_induct)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1544
  case empty show ?case by simp
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1545
next
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1546
  case (insert x A)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1547
  then show ?case using card_image_le [of A f]
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1548
    by (simp add: card_insert_if split: if_splits)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1549
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1550
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1551
lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card(f ` A) = card A"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1552
  by (blast intro: card_image eq_card_imp_inj_on)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1553
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1554
lemma card_inj_on_le:
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1555
  assumes "inj_on f A" "f ` A \<subseteq> B" "finite B" shows "card A \<le> card B"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1556
proof -
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1557
  have "finite A" using assms
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1558
    by (blast intro: finite_imageD dest: finite_subset)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1559
  then show ?thesis using assms 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1560
   by (force intro: card_mono simp: card_image [symmetric])
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1561
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1562
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1563
lemma card_bij_eq:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1564
  "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1565
     finite A; finite B |] ==> card A = card B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1566
by (auto intro: le_antisym card_inj_on_le)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1567
40703
d1fc454d6735 Move some missing lemmas from Andrei Popescus 'Ordinals and Cardinals' AFP entry to the HOL-image.
hoelzl
parents: 40702
diff changeset
  1568
lemma bij_betw_finite:
d1fc454d6735 Move some missing lemmas from Andrei Popescus 'Ordinals and Cardinals' AFP entry to the HOL-image.
hoelzl
parents: 40702
diff changeset
  1569
  assumes "bij_betw f A B"
d1fc454d6735 Move some missing lemmas from Andrei Popescus 'Ordinals and Cardinals' AFP entry to the HOL-image.
hoelzl
parents: 40702
diff changeset
  1570
  shows "finite A \<longleftrightarrow> finite B"
d1fc454d6735 Move some missing lemmas from Andrei Popescus 'Ordinals and Cardinals' AFP entry to the HOL-image.
hoelzl
parents: 40702
diff changeset
  1571
using assms unfolding bij_betw_def
d1fc454d6735 Move some missing lemmas from Andrei Popescus 'Ordinals and Cardinals' AFP entry to the HOL-image.
hoelzl
parents: 40702
diff changeset
  1572
using finite_imageD[of f A] by auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1573
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
  1574
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1575
subsubsection {* Pigeonhole Principles *}
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1576
40311
994e784ca17a removed assumption
nipkow
parents: 39302
diff changeset
  1577
lemma pigeonhole: "card A > card(f ` A) \<Longrightarrow> ~ inj_on f A "
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1578
by (auto dest: card_image less_irrefl_nat)
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1579
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1580
lemma pigeonhole_infinite:
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1581
assumes  "~ finite A" and "finite(f`A)"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1582
shows "EX a0:A. ~finite{a:A. f a = f a0}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1583
proof -
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1584
  have "finite(f`A) \<Longrightarrow> ~ finite A \<Longrightarrow> EX a0:A. ~finite{a:A. f a = f a0}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1585
  proof(induct "f`A" arbitrary: A rule: finite_induct)
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1586
    case empty thus ?case by simp
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1587
  next
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1588
    case (insert b F)
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1589
    show ?case
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1590
    proof cases
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1591
      assume "finite{a:A. f a = b}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1592
      hence "~ finite(A - {a:A. f a = b})" using `\<not> finite A` by simp
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1593
      also have "A - {a:A. f a = b} = {a:A. f a \<noteq> b}" by blast
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1594
      finally have "~ finite({a:A. f a \<noteq> b})" .
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1595
      from insert(3)[OF _ this]
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1596
      show ?thesis using insert(2,4) by simp (blast intro: rev_finite_subset)
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1597
    next
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1598
      assume 1: "~finite{a:A. f a = b}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1599
      hence "{a \<in> A. f a = b} \<noteq> {}" by force
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1600
      thus ?thesis using 1 by blast
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1601
    qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1602
  qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1603
  from this[OF assms(2,1)] show ?thesis .
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1604
qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1605
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1606
lemma pigeonhole_infinite_rel:
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1607
assumes "~finite A" and "finite B" and "ALL a:A. EX b:B. R a b"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1608
shows "EX b:B. ~finite{a:A. R a b}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1609
proof -
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1610
   let ?F = "%a. {b:B. R a b}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1611
   from finite_Pow_iff[THEN iffD2, OF `finite B`]
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1612
   have "finite(?F ` A)" by(blast intro: rev_finite_subset)
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1613
   from pigeonhole_infinite[where f = ?F, OF assms(1) this]
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1614
   obtain a0 where "a0\<in>A" and 1: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1615
   obtain b0 where "b0 : B" and "R a0 b0" using `a0:A` assms(3) by blast
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1616
   { assume "finite{a:A. R a b0}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1617
     then have "finite {a\<in>A. ?F a = ?F a0}"
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1618
       using `b0 : B` `R a0 b0` by(blast intro: rev_finite_subset)
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1619
   }
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1620
   with 1 `b0 : B` show ?thesis by blast
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1621
qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1622
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  1623
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1624
subsubsection {* Cardinality of sums *}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1625
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1626
lemma card_Plus:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1627
  assumes "finite A" and "finite B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1628
  shows "card (A <+> B) = card A + card B"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1629
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1630
  have "Inl`A \<inter> Inr`B = {}" by fast
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1631
  with assms show ?thesis
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1632
    unfolding Plus_def
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1633
    by (simp add: card_Un_disjoint card_image)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1634
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1635
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1636
lemma card_Plus_conv_if:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1637
  "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1638
  by (auto simp add: card_Plus)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1639
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1640
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1641
subsubsection {* Cardinality of the Powerset *}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1642
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1643
lemma card_Pow: "finite A \<Longrightarrow> card (Pow A) = 2 ^ card A"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1644
proof (induct rule: finite_induct)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1645
  case empty 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1646
    show ?case by auto
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1647
next
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1648
  case (insert x A)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1649
  then have "inj_on (insert x) (Pow A)" 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1650
    unfolding inj_on_def by (blast elim!: equalityE)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1651
  then have "card (Pow A) + card (insert x ` Pow A) = 2 * 2 ^ card A" 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1652
    by (simp add: mult_2 card_image Pow_insert insert.hyps)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1653
  then show ?case using insert
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1654
    apply (simp add: Pow_insert)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1655
    apply (subst card_Un_disjoint, auto)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1656
    done
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1657
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1658
41987
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1659
text {* Relates to equivalence classes.  Based on a theorem of F. Kamm\"uller.  *}
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1660
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1661
lemma dvd_partition:
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1662
  assumes f: "finite (\<Union>C)" and "\<forall>c\<in>C. k dvd card c" "\<forall>c1\<in>C. \<forall>c2\<in>C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1663
    shows "k dvd card (\<Union>C)"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1664
proof -
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1665
  have "finite C" 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1666
    by (rule finite_UnionD [OF f])
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1667
  then show ?thesis using assms
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1668
  proof (induct rule: finite_induct)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1669
    case empty show ?case by simp
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1670
  next
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1671
    case (insert c C)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1672
    then show ?case 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1673
      apply simp
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1674
      apply (subst card_Un_disjoint)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1675
      apply (auto simp add: disjoint_eq_subset_Compl)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1676
      done
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1677
  qed
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1678
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1679
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1680
subsubsection {* Relating injectivity and surjectivity *}
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1681
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1682
lemma finite_surj_inj: assumes "finite A" "A \<subseteq> f ` A" shows "inj_on f A"
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1683
proof -
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1684
  have "f ` A = A" 
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1685
    by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1686
  then show ?thesis using assms
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1687
    by (simp add: eq_card_imp_inj_on)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  1688
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1689
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1690
lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1691
shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
40702
cf26dd7395e4 Replace surj by abbreviation; remove surj_on.
hoelzl
parents: 40311
diff changeset
  1692
by (blast intro: finite_surj_inj subset_UNIV)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1693
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1694
lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1695
shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44835
diff changeset
  1696
by(fastforce simp:surj_def dest!: endo_inj_surj)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1697
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1698
corollary infinite_UNIV_nat [iff]:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1699
  "\<not> finite (UNIV :: nat set)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1700
proof
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1701
  assume "finite (UNIV :: nat set)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1702
  with finite_UNIV_inj_surj [of Suc]
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1703
  show False by simp (blast dest: Suc_neq_Zero surjD)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1704
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1705
54147
97a8ff4e4ac9 killed most "no_atp", to make Sledgehammer more complete
blanchet
parents: 53820
diff changeset
  1706
lemma infinite_UNIV_char_0:
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1707
  "\<not> finite (UNIV :: 'a::semiring_char_0 set)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1708
proof
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1709
  assume "finite (UNIV :: 'a set)"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1710
  with subset_UNIV have "finite (range of_nat :: 'a set)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1711
    by (rule finite_subset)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1712
  moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1713
    by (simp add: inj_on_def)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1714
  ultimately have "finite (UNIV :: nat set)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1715
    by (rule finite_imageD)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1716
  then show False
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1717
    by simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1718
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1719
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  1720
hide_const (open) Finite_Set.fold
46033
6fc579c917b8 qualified Finite_Set.fold
haftmann
parents: 45962
diff changeset
  1721
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1722
end
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1723