src/HOL/Finite_Set.thy
author wenzelm
Fri, 12 Aug 2022 19:47:38 +0200
changeset 75827 4920ebbde486
parent 75669 43f5dfb7fa35
child 76422 2612b3406b61
permissions -rw-r--r--
tuned, following hints by IntelliJ IDEA;
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
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
     2
    Author:     Tobias Nipkow
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
     3
    Author:     Lawrence C Paulson
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
     4
    Author:     Markus Wenzel
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
     5
    Author:     Jeremy Avigad
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
     6
    Author:     Andrei Popescu
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     7
*)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     8
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
     9
section \<open>Finite sets\<close>
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    10
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15124
diff changeset
    11
theory Finite_Set
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
    12
  imports Product_Type Sum_Type Fields
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15124
diff changeset
    13
begin
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    14
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
    15
subsection \<open>Predicate for finite sets\<close>
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    16
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
    17
context notes [[inductive_internals]]
61681
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61605
diff changeset
    18
begin
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61605
diff changeset
    19
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    20
inductive finite :: "'a set \<Rightarrow> bool"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
    21
  where
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
    22
    emptyI [simp, intro!]: "finite {}"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
    23
  | insertI [simp, intro!]: "finite A \<Longrightarrow> finite (insert a A)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    24
61681
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61605
diff changeset
    25
end
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61605
diff changeset
    26
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
    27
simproc_setup finite_Collect ("finite (Collect P)") = \<open>K Set_Comprehension_Pointfree.simproc\<close>
48109
0a58f7eefba2 Integrated set comprehension pointfree simproc.
Rafal Kolanski <rafal.kolanski@nicta.com.au>
parents: 48063
diff changeset
    28
54611
31afce809794 set_comprehension_pointfree simproc causes to many surprises if enabled by default
traytel
parents: 54570
diff changeset
    29
declare [[simproc del: finite_Collect]]
31afce809794 set_comprehension_pointfree simproc causes to many surprises if enabled by default
traytel
parents: 54570
diff changeset
    30
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    31
lemma finite_induct [case_names empty insert, induct set: finite]:
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61778
diff changeset
    32
  \<comment> \<open>Discharging \<open>x \<notin> F\<close> entails extra work.\<close>
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    33
  assumes "finite F"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    34
  assumes "P {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    35
    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
    36
  shows "P F"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    37
  using \<open>finite F\<close>
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
    38
proof induct
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    39
  show "P {}" by fact
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    40
next
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    41
  fix x F
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    42
  assume F: "finite F" and P: "P F"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    43
  show "P (insert x F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    44
  proof cases
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    45
    assume "x \<in> F"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    46
    then have "insert x F = F" by (rule insert_absorb)
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    47
    with P show ?thesis by (simp only:)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    48
  next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    49
    assume "x \<notin> F"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    50
    from F this P show ?thesis by (rule insert)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    51
  qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    52
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    53
51622
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    54
lemma infinite_finite_induct [case_names infinite empty insert]:
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    55
  assumes infinite: "\<And>A. \<not> finite A \<Longrightarrow> P A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    56
    and empty: "P {}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    57
    and insert: "\<And>x F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> P F \<Longrightarrow> P (insert x F)"
51622
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    58
  shows "P A"
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    59
proof (cases "finite A")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    60
  case False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    61
  with infinite show ?thesis .
51622
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    62
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    63
  case True
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    64
  then show ?thesis by (induct A) (fact empty insert)+
51622
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    65
qed
183f30bc11de convenient induction rule
haftmann
parents: 51598
diff changeset
    66
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    67
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
    68
subsubsection \<open>Choice principles\<close>
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    69
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 63982
diff changeset
    70
lemma ex_new_if_finite: \<comment> \<open>does not depend on def of finite at all\<close>
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    71
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    72
  shows "\<exists>a::'a. a \<notin> A"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    73
proof -
28823
dcbef866c9e2 tuned unfold_locales invocation
haftmann
parents: 27981
diff changeset
    74
  from assms have "A \<noteq> UNIV" by blast
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    75
  then show ?thesis by blast
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    76
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    77
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
    78
text \<open>A finite choice principle. Does not need the SOME choice operator.\<close>
15484
2636ec211ec8 fold and fol1 changes
nipkow
parents: 15483
diff changeset
    79
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    80
lemma finite_set_choice: "finite A \<Longrightarrow> \<forall>x\<in>A. \<exists>y. P x y \<Longrightarrow> \<exists>f. \<forall>x\<in>A. P x (f x)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
    81
proof (induct rule: finite_induct)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    82
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    83
  then show ?case by simp
29923
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    84
next
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    85
  case (insert a A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    86
  then obtain f b where f: "\<forall>x\<in>A. P x (f x)" and ab: "P a b"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    87
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    88
  show ?case (is "\<exists>f. ?P f")
29923
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    89
  proof
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    90
    show "?P (\<lambda>x. if x = a then b else f x)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    91
      using f ab by auto
29923
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    92
  qed
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    93
qed
24f56736c56f added finite_set_choice
nipkow
parents: 29920
diff changeset
    94
23878
bd651ecd4b8a simplified HOL bootstrap
haftmann
parents: 23736
diff changeset
    95
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
    96
subsubsection \<open>Finite sets are the images of initial segments of natural numbers\<close>
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
    97
15510
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
    98
lemma finite_imp_nat_seg_image_inj_on:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
    99
  assumes "finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   100
  shows "\<exists>(n::nat) f. A = f ` {i. i < n} \<and> inj_on f {i. i < n}"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   101
  using assms
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   102
proof induct
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   103
  case empty
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   104
  show ?case
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   105
  proof
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   106
    show "\<exists>f. {} = f ` {i::nat. i < 0} \<and> inj_on f {i. i < 0}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   107
      by simp
15510
9de204d7b699 new foldSet proofs
paulson
parents: 15509
diff changeset
   108
  qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   109
next
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   110
  case (insert a A)
23389
aaca6a8e5414 tuned proofs: avoid implicit prems;
wenzelm
parents: 23277
diff changeset
   111
  have notinA: "a \<notin> A" by fact
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   112
  from insert.hyps obtain n f where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   113
    by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   114
  then have "insert a A = f(n:=a) ` {i. i < Suc n}" and "inj_on (f(n:=a)) {i. i < Suc n}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   115
    using notinA by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   116
  then show ?case by blast
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   117
qed
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   118
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   119
lemma nat_seg_image_imp_finite: "A = f ` {i::nat. i < n} \<Longrightarrow> finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   120
proof (induct n arbitrary: A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   121
  case 0
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   122
  then show ?case by simp
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   123
next
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   124
  case (Suc n)
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   125
  let ?B = "f ` {i. i < n}"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   126
  have finB: "finite ?B" by (rule Suc.hyps[OF refl])
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   127
  show ?case
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   128
  proof (cases "\<exists>k<n. f n = f k")
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   129
    case True
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   130
    then have "A = ?B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   131
      using Suc.prems by (auto simp:less_Suc_eq)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   132
    then show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   133
      using finB by simp
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   134
  next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   135
    case False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   136
    then have "A = insert (f n) ?B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   137
      using Suc.prems by (auto simp:less_Suc_eq)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   138
    then show ?thesis using finB by simp
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   139
  qed
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   140
qed
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   141
63982
wenzelm
parents: 63915
diff changeset
   142
lemma finite_conv_nat_seg_image: "finite A \<longleftrightarrow> (\<exists>n f. A = f ` {i::nat. i < n})"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   143
  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
   144
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   145
lemma finite_imp_inj_to_nat_seg:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   146
  assumes "finite A"
63982
wenzelm
parents: 63915
diff changeset
   147
  shows "\<exists>f n. f ` A = {i::nat. i < n} \<and> inj_on f A"
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   148
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   149
  from finite_imp_nat_seg_image_inj_on [OF \<open>finite A\<close>]
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   150
  obtain f and n :: nat where bij: "bij_betw f {i. i<n} A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   151
    by (auto simp: bij_betw_def)
33057
764547b68538 inv_onto -> inv_into
nipkow
parents: 32989
diff changeset
   152
  let ?f = "the_inv_into {i. i<n} f"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   153
  have "inj_on ?f A \<and> ?f ` A = {i. i<n}"
33057
764547b68538 inv_onto -> inv_into
nipkow
parents: 32989
diff changeset
   154
    by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   155
  then show ?thesis by blast
32988
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   156
qed
d1d4d7a08a66 Inv -> inv_onto, inv abbr. inv_onto UNIV.
nipkow
parents: 32705
diff changeset
   157
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   158
lemma finite_Collect_less_nat [iff]: "finite {n::nat. n < k}"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44835
diff changeset
   159
  by (fastforce simp: finite_conv_nat_seg_image)
29920
b95f5b8b93dd more finiteness
nipkow
parents: 29918
diff changeset
   160
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   161
lemma finite_Collect_le_nat [iff]: "finite {n::nat. n \<le> k}"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   162
  by (simp add: le_eq_less_or_eq Collect_disj_eq)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   163
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   164
68975
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   165
subsection \<open>Finiteness and common set operations\<close>
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   166
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   167
lemma rev_finite_subset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   168
proof (induct arbitrary: A rule: finite_induct)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   169
  case empty
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   170
  then show ?case by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   171
next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   172
  case (insert x F A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   173
  have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F \<Longrightarrow> finite (A - {x})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   174
    by fact+
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   175
  show "finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   176
  proof cases
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   177
    assume x: "x \<in> A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   178
    with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   179
    with r have "finite (A - {x})" .
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   180
    then have "finite (insert x (A - {x}))" ..
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   181
    also have "insert x (A - {x}) = A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   182
      using x by (rule insert_Diff)
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   183
    finally show ?thesis .
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   184
  next
60595
804dfdc82835 premises in 'show' are treated like 'assume';
wenzelm
parents: 60585
diff changeset
   185
    show ?thesis when "A \<subseteq> F"
804dfdc82835 premises in 'show' are treated like 'assume';
wenzelm
parents: 60585
diff changeset
   186
      using that by fact
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   187
    assume "x \<notin> A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   188
    with A show "A \<subseteq> F"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   189
      by (simp add: subset_insert_iff)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   190
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   191
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   192
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   193
lemma finite_subset: "A \<subseteq> B \<Longrightarrow> finite B \<Longrightarrow> finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   194
  by (rule rev_finite_subset)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   195
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   196
lemma finite_UnI:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   197
  assumes "finite F" and "finite G"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   198
  shows "finite (F \<union> G)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   199
  using assms by induct simp_all
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
   200
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   201
lemma finite_Un [iff]: "finite (F \<union> G) \<longleftrightarrow> finite F \<and> finite G"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   202
  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
   203
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   204
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
   205
proof -
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   206
  have "finite {a} \<and> finite A \<longleftrightarrow> finite A" by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   207
  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
   208
  then show ?thesis by simp
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   209
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   210
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   211
lemma finite_Int [simp, intro]: "finite F \<or> finite G \<Longrightarrow> finite (F \<inter> G)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   212
  by (blast intro: finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   213
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   214
lemma finite_Collect_conjI [simp, intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   215
  "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
   216
  by (simp add: Collect_conj_eq)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   217
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   218
lemma finite_Collect_disjI [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   219
  "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
   220
  by (simp add: Collect_disj_eq)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   221
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   222
lemma finite_Diff [simp, intro]: "finite A \<Longrightarrow> finite (A - B)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   223
  by (rule finite_subset, rule Diff_subset)
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   224
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   225
lemma finite_Diff2 [simp]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   226
  assumes "finite B"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   227
  shows "finite (A - B) \<longleftrightarrow> finite A"
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   228
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   229
  have "finite A \<longleftrightarrow> finite ((A - B) \<union> (A \<inter> B))"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   230
    by (simp add: Un_Diff_Int)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   231
  also have "\<dots> \<longleftrightarrow> finite (A - B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   232
    using \<open>finite B\<close> by simp
29901
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   233
  finally show ?thesis ..
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   234
qed
f4b3f8fbf599 finiteness lemmas
nipkow
parents: 29879
diff changeset
   235
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   236
lemma finite_Diff_insert [iff]: "finite (A - insert a B) \<longleftrightarrow> finite (A - B)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   237
proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   238
  have "finite (A - B) \<longleftrightarrow> finite (A - B - {a})" by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   239
  moreover have "A - insert a B = A - B - {a}" by auto
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   240
  ultimately show ?thesis by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   241
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   242
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   243
lemma finite_compl [simp]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   244
  "finite (A :: 'a set) \<Longrightarrow> finite (- A) \<longleftrightarrow> finite (UNIV :: 'a set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   245
  by (simp add: Compl_eq_Diff_UNIV)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   246
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   247
lemma finite_Collect_not [simp]:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   248
  "finite {x :: 'a. P x} \<Longrightarrow> finite {x. \<not> P x} \<longleftrightarrow> finite (UNIV :: 'a set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   249
  by (simp add: Collect_neg_eq)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   250
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   251
lemma finite_Union [simp, intro]:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   252
  "finite A \<Longrightarrow> (\<And>M. M \<in> A \<Longrightarrow> finite M) \<Longrightarrow> finite (\<Union>A)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   253
  by (induct rule: finite_induct) simp_all
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   254
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   255
lemma finite_UN_I [intro]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   256
  "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
   257
  by (induct rule: finite_induct) simp_all
29903
2c0046b26f80 more finiteness changes
nipkow
parents: 29901
diff changeset
   258
69275
9bbd5497befd clarified status of legacy input abbreviations
haftmann
parents: 69235
diff changeset
   259
lemma finite_UN [simp]: "finite A \<Longrightarrow> finite (\<Union>(B ` A)) \<longleftrightarrow> (\<forall>x\<in>A. finite (B x))"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   260
  by (blast intro: finite_subset)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   261
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   262
lemma finite_Inter [intro]: "\<exists>A\<in>M. finite A \<Longrightarrow> finite (\<Inter>M)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   263
  by (blast intro: Inter_lower finite_subset)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   264
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   265
lemma finite_INT [intro]: "\<exists>x\<in>I. finite (A x) \<Longrightarrow> finite (\<Inter>x\<in>I. A x)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   266
  by (blast intro: INT_lower finite_subset)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   267
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   268
lemma finite_imageI [simp, intro]: "finite F \<Longrightarrow> finite (h ` F)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   269
  by (induct rule: finite_induct) simp_all
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   270
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   271
lemma finite_image_set [simp]: "finite {x. P x} \<Longrightarrow> finite {f x |x. P x}"
31768
159cd6b5e5d4 lemma finite_image_set by Jeremy Avigad
haftmann
parents: 31465
diff changeset
   272
  by (simp add: image_Collect [symmetric])
159cd6b5e5d4 lemma finite_image_set by Jeremy Avigad
haftmann
parents: 31465
diff changeset
   273
59504
8c6747dba731 New lemmas and a bit of tidying up.
paulson <lp15@cam.ac.uk>
parents: 59336
diff changeset
   274
lemma finite_image_set2:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   275
  "finite {x. P x} \<Longrightarrow> finite {y. Q y} \<Longrightarrow> finite {f x y |x y. P x \<and> Q y}"
59504
8c6747dba731 New lemmas and a bit of tidying up.
paulson <lp15@cam.ac.uk>
parents: 59336
diff changeset
   276
  by (rule finite_subset [where B = "\<Union>x \<in> {x. P x}. \<Union>y \<in> {y. Q y}. {f x y}"]) auto
8c6747dba731 New lemmas and a bit of tidying up.
paulson <lp15@cam.ac.uk>
parents: 59336
diff changeset
   277
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   278
lemma finite_imageD:
42206
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   279
  assumes "finite (f ` A)" and "inj_on f A"
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   280
  shows "finite A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   281
  using assms
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   282
proof (induct "f ` A" arbitrary: A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   283
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   284
  then show ?case by simp
42206
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   285
next
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   286
  case (insert x B)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   287
  then have B_A: "insert x B = f ` A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   288
    by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   289
  then obtain y where "x = f y" and "y \<in> A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   290
    by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   291
  from B_A \<open>x \<notin> B\<close> have "B = f ` A - {x}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   292
    by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   293
  with B_A \<open>x \<notin> B\<close> \<open>x = f y\<close> \<open>inj_on f A\<close> \<open>y \<in> A\<close> have "B = f ` (A - {y})"
69286
nipkow
parents: 69275
diff changeset
   294
    by (simp add: inj_on_image_set_diff)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   295
  moreover from \<open>inj_on f A\<close> have "inj_on f (A - {y})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   296
    by (rule inj_on_diff)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   297
  ultimately have "finite (A - {y})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   298
    by (rule insert.hyps)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   299
  then show "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   300
    by simp
42206
0920f709610f tuned proof
haftmann
parents: 41988
diff changeset
   301
qed
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   302
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   303
lemma finite_image_iff: "inj_on f A \<Longrightarrow> finite (f ` A) \<longleftrightarrow> finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   304
  using finite_imageD by blast
62618
f7f2467ab854 Refactoring (moving theorems into better locations), plus a bit of new material
paulson <lp15@cam.ac.uk>
parents: 62481
diff changeset
   305
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   306
lemma finite_surj: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> finite B"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   307
  by (erule finite_subset) (rule finite_imageI)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   308
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   309
lemma finite_range_imageI: "finite (range g) \<Longrightarrow> finite (range (\<lambda>x. f (g x)))"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   310
  by (drule finite_imageI) (simp add: range_composition)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   311
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   312
lemma finite_subset_image:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   313
  assumes "finite B"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   314
  shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   315
  using assms
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   316
proof induct
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   317
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   318
  then show ?case by simp
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   319
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   320
  case insert
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   321
  then show ?case
71258
d67924987c34 a few new and tidier proofs (mostly about finite sets)
paulson <lp15@cam.ac.uk>
parents: 70723
diff changeset
   322
    by (clarsimp simp del: image_insert simp add: image_insert [symmetric]) blast
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   323
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   324
68975
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   325
lemma all_subset_image: "(\<forall>B. B \<subseteq> f ` A \<longrightarrow> P B) \<longleftrightarrow> (\<forall>B. B \<subseteq> A \<longrightarrow> P(f ` B))"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   326
  by (safe elim!: subset_imageE) (use image_mono in \<open>blast+\<close>) (* slow *)
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   327
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   328
lemma all_finite_subset_image:
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   329
  "(\<forall>B. finite B \<and> B \<subseteq> f ` A \<longrightarrow> P B) \<longleftrightarrow> (\<forall>B. finite B \<and> B \<subseteq> A \<longrightarrow> P (f ` B))"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   330
proof safe
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   331
  fix B :: "'a set"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   332
  assume B: "finite B" "B \<subseteq> f ` A" and P: "\<forall>B. finite B \<and> B \<subseteq> A \<longrightarrow> P (f ` B)"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   333
  show "P B"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   334
    using finite_subset_image [OF B] P by blast
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   335
qed blast
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   336
70178
4900351361b0 Lindelöf spaces and supporting material
paulson <lp15@cam.ac.uk>
parents: 70019
diff changeset
   337
lemma ex_finite_subset_image:
68975
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   338
  "(\<exists>B. finite B \<and> B \<subseteq> f ` A \<and> P B) \<longleftrightarrow> (\<exists>B. finite B \<and> B \<subseteq> A \<and> P (f ` B))"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   339
proof safe
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   340
  fix B :: "'a set"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   341
  assume B: "finite B" "B \<subseteq> f ` A" and "P B"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   342
  show "\<exists>B. finite B \<and> B \<subseteq> A \<and> P (f ` B)"
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   343
    using finite_subset_image [OF B] \<open>P B\<close> by blast
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   344
qed blast
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   345
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   346
lemma finite_vimage_IntI: "finite F \<Longrightarrow> inj_on h A \<Longrightarrow> finite (h -` F \<inter> A)"
68975
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   347
proof (induct rule: finite_induct)
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   348
  case (insert x F)
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   349
  then show ?case
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   350
    by (simp add: vimage_insert [of h x F] finite_subset [OF inj_on_vimage_singleton] Int_Un_distrib2)
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   351
qed simp
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   352
61762
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   353
lemma finite_finite_vimage_IntI:
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   354
  assumes "finite F"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   355
    and "\<And>y. y \<in> F \<Longrightarrow> finite ((h -` {y}) \<inter> A)"
61762
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   356
  shows "finite (h -` F \<inter> A)"
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   357
proof -
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   358
  have *: "h -` F \<inter> A = (\<Union> y\<in>F. (h -` {y}) \<inter> A)"
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   359
    by blast
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   360
  show ?thesis
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   361
    by (simp only: * assms finite_UN_I)
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   362
qed
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61681
diff changeset
   363
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   364
lemma finite_vimageI: "finite F \<Longrightarrow> inj h \<Longrightarrow> finite (h -` F)"
43991
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   365
  using finite_vimage_IntI[of F h UNIV] by auto
f4a7697011c5 finite vimage on arbitrary domains
hoelzl
parents: 43866
diff changeset
   366
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   367
lemma finite_vimageD': "finite (f -` A) \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   368
  by (auto simp add: subset_image_iff intro: finite_subset[rotated])
59519
2fb0c0fc62a3 add more general version of finite_vimageD
Andreas Lochbihler
parents: 59504
diff changeset
   369
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   370
lemma finite_vimageD: "finite (h -` F) \<Longrightarrow> surj h \<Longrightarrow> finite F"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   371
  by (auto dest: finite_vimageD')
34111
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   372
1b015caba46c add lemmas rev_finite_subset, finite_vimageD, finite_vimage_iff
huffman
parents: 34007
diff changeset
   373
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
   374
  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
   375
74438
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   376
lemma finite_inverse_image_gen:
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   377
  assumes "finite A" "inj_on f D"
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   378
  shows "finite {j\<in>D. f j \<in> A}"
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   379
  using finite_vimage_IntI [OF assms]
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   380
  by (simp add: Collect_conj_eq inf_commute vimage_def)
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   381
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   382
lemma finite_inverse_image:
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   383
  assumes "finite A" "inj f"
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   384
  shows "finite {j. f j \<in> A}"
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   385
  using finite_inverse_image_gen [OF assms] by simp
5827b91ef30e new material from the Roth development, mostly about finite sets, disjoint famillies and partitions
paulson <lp15@cam.ac.uk>
parents: 74223
diff changeset
   386
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   387
lemma finite_Collect_bex [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   388
  assumes "finite A"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   389
  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
   390
proof -
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   391
  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
   392
  with assms show ?thesis by simp
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   393
qed
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   394
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   395
lemma finite_Collect_bounded_ex [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   396
  assumes "finite {y. P y}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   397
  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
   398
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   399
  have "{x. \<exists>y. P y \<and> Q x y} = (\<Union>y\<in>{y. P y}. {x. Q x y})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   400
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   401
  with assms show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   402
    by simp
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   403
qed
29920
b95f5b8b93dd more finiteness
nipkow
parents: 29918
diff changeset
   404
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   405
lemma finite_Plus: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A <+> B)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   406
  by (simp add: Plus_def)
17022
b257300c3a9c added Brian Hufmann's finite instances
nipkow
parents: 16775
diff changeset
   407
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   408
lemma finite_PlusD:
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   409
  fixes A :: "'a set" and B :: "'b set"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   410
  assumes fin: "finite (A <+> B)"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   411
  shows "finite A" "finite B"
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   412
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   413
  have "Inl ` A \<subseteq> A <+> B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   414
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   415
  then have "finite (Inl ` A :: ('a + 'b) set)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   416
    using fin by (rule finite_subset)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   417
  then show "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   418
    by (rule finite_imageD) (auto intro: inj_onI)
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   419
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   420
  have "Inr ` B \<subseteq> A <+> B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   421
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   422
  then have "finite (Inr ` B :: ('a + 'b) set)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   423
    using fin by (rule finite_subset)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   424
  then show "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   425
    by (rule finite_imageD) (auto intro: inj_onI)
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   426
qed
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   427
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   428
lemma finite_Plus_iff [simp]: "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   429
  by (auto intro: finite_PlusD finite_Plus)
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31017
diff changeset
   430
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   431
lemma finite_Plus_UNIV_iff [simp]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   432
  "finite (UNIV :: ('a + 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   433
  by (subst UNIV_Plus_UNIV [symmetric]) (rule finite_Plus_iff)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   434
40786
0a54cfc9add3 gave more standard finite set rules simp and intro attribute
nipkow
parents: 40716
diff changeset
   435
lemma finite_SigmaI [simp, intro]:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   436
  "finite A \<Longrightarrow> (\<And>a. a\<in>A \<Longrightarrow> finite (B a)) \<Longrightarrow> finite (SIGMA a:A. B a)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   437
  unfolding Sigma_def by blast
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   438
51290
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   439
lemma finite_SigmaI2:
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   440
  assumes "finite {x\<in>A. B x \<noteq> {}}"
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   441
  and "\<And>a. a \<in> A \<Longrightarrow> finite (B a)"
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   442
  shows "finite (Sigma A B)"
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   443
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   444
  from assms have "finite (Sigma {x\<in>A. B x \<noteq> {}} B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   445
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   446
  also have "Sigma {x:A. B x \<noteq> {}} B = Sigma A B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   447
    by auto
51290
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   448
  finally show ?thesis .
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   449
qed
c48477e76de5 added lemma
Andreas Lochbihler
parents: 49806
diff changeset
   450
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   451
lemma finite_cartesian_product: "finite A \<Longrightarrow> finite B \<Longrightarrow> finite (A \<times> B)"
15402
97204f3b4705 REorganized Finite_Set
nipkow
parents: 15392
diff changeset
   452
  by (rule finite_SigmaI)
97204f3b4705 REorganized Finite_Set
nipkow
parents: 15392
diff changeset
   453
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   454
lemma finite_Prod_UNIV:
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   455
  "finite (UNIV :: 'a set) \<Longrightarrow> finite (UNIV :: 'b set) \<Longrightarrow> finite (UNIV :: ('a \<times> 'b) set)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   456
  by (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   457
15409
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   458
lemma finite_cartesian_productD1:
42207
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   459
  assumes "finite (A \<times> B)" and "B \<noteq> {}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   460
  shows "finite A"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   461
proof -
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   462
  from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   463
    by (auto simp add: finite_conv_nat_seg_image)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   464
  then have "fst ` (A \<times> B) = fst ` f ` {i::nat. i < n}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   465
    by simp
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   466
  with \<open>B \<noteq> {}\<close> have "A = (fst \<circ> f) ` {i::nat. i < n}"
56154
f0a927235162 more complete set of lemmas wrt. image and composition
haftmann
parents: 55096
diff changeset
   467
    by (simp add: image_comp)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   468
  then have "\<exists>n f. A = f ` {i::nat. i < n}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   469
    by blast
42207
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   470
  then show ?thesis
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   471
    by (auto simp add: finite_conv_nat_seg_image)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   472
qed
15409
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   473
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   474
lemma finite_cartesian_productD2:
42207
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   475
  assumes "finite (A \<times> B)" and "A \<noteq> {}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   476
  shows "finite B"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   477
proof -
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   478
  from assms obtain n f where "A \<times> B = f ` {i::nat. i < n}"
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   479
    by (auto simp add: finite_conv_nat_seg_image)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   480
  then have "snd ` (A \<times> B) = snd ` f ` {i::nat. i < n}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   481
    by simp
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   482
  with \<open>A \<noteq> {}\<close> have "B = (snd \<circ> f) ` {i::nat. i < n}"
56154
f0a927235162 more complete set of lemmas wrt. image and composition
haftmann
parents: 55096
diff changeset
   483
    by (simp add: image_comp)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   484
  then have "\<exists>n f. B = f ` {i::nat. i < n}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   485
    by blast
42207
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   486
  then show ?thesis
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   487
    by (auto simp add: finite_conv_nat_seg_image)
2bda5eddadf3 tuned proofs
haftmann
parents: 42206
diff changeset
   488
qed
15409
a063687d24eb new and stronger lemmas and improved simplification for finite sets
paulson
parents: 15402
diff changeset
   489
57025
e7fd64f82876 add various lemmas
hoelzl
parents: 56218
diff changeset
   490
lemma finite_cartesian_product_iff:
e7fd64f82876 add various lemmas
hoelzl
parents: 56218
diff changeset
   491
  "finite (A \<times> B) \<longleftrightarrow> (A = {} \<or> B = {} \<or> (finite A \<and> finite B))"
e7fd64f82876 add various lemmas
hoelzl
parents: 56218
diff changeset
   492
  by (auto dest: finite_cartesian_productD1 finite_cartesian_productD2 finite_cartesian_product)
e7fd64f82876 add various lemmas
hoelzl
parents: 56218
diff changeset
   493
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   494
lemma finite_prod:
48175
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   495
  "finite (UNIV :: ('a \<times> 'b) set) \<longleftrightarrow> finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set)"
57025
e7fd64f82876 add various lemmas
hoelzl
parents: 56218
diff changeset
   496
  using finite_cartesian_product_iff[of UNIV UNIV] by simp
48175
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   497
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   498
lemma finite_Pow_iff [iff]: "finite (Pow A) \<longleftrightarrow> finite A"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   499
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   500
  assume "finite (Pow A)"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   501
  then have "finite ((\<lambda>x. {x}) ` A)"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   502
    by (blast intro: finite_subset)  (* somewhat slow *)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   503
  then show "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   504
    by (rule finite_imageD [unfolded inj_on_def]) simp
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   505
next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   506
  assume "finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   507
  then show "finite (Pow A)"
35216
7641e8d831d2 get rid of many duplicate simp rule warnings
huffman
parents: 35171
diff changeset
   508
    by induct (simp_all add: Pow_insert)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   509
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   510
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   511
corollary finite_Collect_subsets [simp, intro]: "finite A \<Longrightarrow> finite {B. B \<subseteq> A}"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   512
  by (simp add: Pow_def [symmetric])
29918
214755b03df3 more finiteness
nipkow
parents: 29916
diff changeset
   513
48175
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   514
lemma finite_set: "finite (UNIV :: 'a set set) \<longleftrightarrow> finite (UNIV :: 'a set)"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   515
  by (simp only: finite_Pow_iff Pow_UNIV[symmetric])
48175
fea68365c975 add finiteness lemmas for 'a * 'b and 'a set
Andreas Lochbihler
parents: 48128
diff changeset
   516
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   517
lemma finite_UnionD: "finite (\<Union>A) \<Longrightarrow> finite A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   518
  by (blast intro: finite_subset [OF subset_Pow_Union])
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   519
67511
a6f5a78712af include lemmas generally useful for combinatorial proofs
bulwahn
parents: 67457
diff changeset
   520
lemma finite_bind:
a6f5a78712af include lemmas generally useful for combinatorial proofs
bulwahn
parents: 67457
diff changeset
   521
  assumes "finite S"
a6f5a78712af include lemmas generally useful for combinatorial proofs
bulwahn
parents: 67457
diff changeset
   522
  assumes "\<forall>x \<in> S. finite (f x)"
a6f5a78712af include lemmas generally useful for combinatorial proofs
bulwahn
parents: 67457
diff changeset
   523
  shows "finite (Set.bind S f)"
a6f5a78712af include lemmas generally useful for combinatorial proofs
bulwahn
parents: 67457
diff changeset
   524
using assms by (simp add: bind_UNION)
a6f5a78712af include lemmas generally useful for combinatorial proofs
bulwahn
parents: 67457
diff changeset
   525
68463
410818a69ee3 material on finite sets and maps
Lars Hupel <lars.hupel@mytum.de>
parents: 67511
diff changeset
   526
lemma finite_filter [simp]: "finite S \<Longrightarrow> finite (Set.filter P S)"
410818a69ee3 material on finite sets and maps
Lars Hupel <lars.hupel@mytum.de>
parents: 67511
diff changeset
   527
unfolding Set.filter_def by simp
410818a69ee3 material on finite sets and maps
Lars Hupel <lars.hupel@mytum.de>
parents: 67511
diff changeset
   528
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   529
lemma finite_set_of_finite_funs:
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   530
  assumes "finite A" "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   531
  shows "finite {f. \<forall>x. (x \<in> A \<longrightarrow> f x \<in> B) \<and> (x \<notin> A \<longrightarrow> f x = d)}" (is "finite ?S")
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   532
proof -
53820
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   533
  let ?F = "\<lambda>f. {(a,b). a \<in> A \<and> b = f a}"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   534
  have "?F ` ?S \<subseteq> Pow(A \<times> B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   535
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   536
  from finite_subset[OF this] assms have 1: "finite (?F ` ?S)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   537
    by simp
53820
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   538
  have 2: "inj_on ?F ?S"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   539
    by (fastforce simp add: inj_on_def set_eq_iff fun_eq_iff)  (* somewhat slow *)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   540
  show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   541
    by (rule finite_imageD [OF 1 2])
53820
9c7e97d67b45 added lemmas
nipkow
parents: 53374
diff changeset
   542
qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   543
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   544
lemma not_finite_existsD:
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   545
  assumes "\<not> finite {a. P a}"
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   546
  shows "\<exists>a. P a"
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   547
proof (rule classical)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   548
  assume "\<not> ?thesis"
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   549
  with assms show ?thesis by auto
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   550
qed
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   551
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   552
68975
5ce4d117cea7 A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents: 68521
diff changeset
   553
subsection \<open>Further induction rules on finite sets\<close>
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   554
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   555
lemma finite_ne_induct [case_names singleton insert, consumes 2]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   556
  assumes "finite F" and "F \<noteq> {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   557
  assumes "\<And>x. P {x}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   558
    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
   559
  shows "P F"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   560
  using assms
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   561
proof induct
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   562
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   563
  then show ?case by simp
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   564
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   565
  case (insert x F)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   566
  then show ?case by cases auto
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   567
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   568
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   569
lemma finite_subset_induct [consumes 2, case_names empty insert]:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   570
  assumes "finite F" and "F \<subseteq> A"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   571
    and empty: "P {}"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   572
    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
   573
  shows "P F"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   574
  using \<open>finite F\<close> \<open>F \<subseteq> A\<close>
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   575
proof induct
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   576
  show "P {}" by fact
31441
428e4caf2299 finite lemmas
nipkow
parents: 31438
diff changeset
   577
next
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   578
  fix x F
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   579
  assume "finite F" and "x \<notin> F" and P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   580
  show "P (insert x F)"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   581
  proof (rule insert)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   582
    from i show "x \<in> A" by blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   583
    from i have "F \<subseteq> A" by blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   584
    with P show "P F" .
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   585
    show "finite F" by fact
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   586
    show "x \<notin> F" by fact
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   587
  qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   588
qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   589
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   590
lemma finite_empty_induct:
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   591
  assumes "finite A"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   592
    and "P A"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   593
    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
   594
  shows "P {}"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   595
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   596
  have "P (A - B)" if "B \<subseteq> A" for B :: "'a set"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   597
  proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   598
    from \<open>finite A\<close> that have "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   599
      by (rule rev_finite_subset)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   600
    from this \<open>B \<subseteq> A\<close> show "P (A - B)"
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   601
    proof induct
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   602
      case empty
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   603
      from \<open>P A\<close> show ?case by simp
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   604
    next
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   605
      case (insert b B)
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   606
      have "P (A - B - {b})"
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   607
      proof (rule remove)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   608
        from \<open>finite A\<close> show "finite (A - B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   609
          by induct auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   610
        from insert show "b \<in> A - B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   611
          by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   612
        from insert show "P (A - B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   613
          by simp
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   614
      qed
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   615
      also have "A - B - {b} = A - insert b B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   616
        by (rule Diff_insert [symmetric])
41656
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   617
      finally show ?case .
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   618
    qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   619
  qed
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   620
  then have "P (A - A)" by blast
011fcb70e32f restructured theory;
haftmann
parents: 41550
diff changeset
   621
  then show ?thesis by simp
31441
428e4caf2299 finite lemmas
nipkow
parents: 31438
diff changeset
   622
qed
428e4caf2299 finite lemmas
nipkow
parents: 31438
diff changeset
   623
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   624
lemma finite_update_induct [consumes 1, case_names const update]:
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   625
  assumes finite: "finite {a. f a \<noteq> c}"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   626
    and const: "P (\<lambda>a. c)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   627
    and update: "\<And>a b f. finite {a. f a \<noteq> c} \<Longrightarrow> f a = c \<Longrightarrow> b \<noteq> c \<Longrightarrow> P f \<Longrightarrow> P (f(a := b))"
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   628
  shows "P f"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   629
  using finite
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   630
proof (induct "{a. f a \<noteq> c}" arbitrary: f)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   631
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   632
  with const show ?case by simp
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   633
next
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   634
  case (insert a A)
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   635
  then have "A = {a'. (f(a := c)) a' \<noteq> c}" and "f a \<noteq> c"
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   636
    by auto
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   637
  with \<open>finite A\<close> have "finite {a'. (f(a := c)) a' \<noteq> c}"
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   638
    by simp
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   639
  have "(f(a := c)) a = c"
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   640
    by simp
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   641
  from insert \<open>A = {a'. (f(a := c)) a' \<noteq> c}\<close> have "P (f(a := c))"
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   642
    by simp
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   643
  with \<open>finite {a'. (f(a := c)) a' \<noteq> c}\<close> \<open>(f(a := c)) a = c\<close> \<open>f a \<noteq> c\<close>
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   644
  have "P ((f(a := c))(a := f a))"
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   645
    by (rule update)
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   646
  then show ?case by simp
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   647
qed
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   648
63561
fba08009ff3e add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents: 63404
diff changeset
   649
lemma finite_subset_induct' [consumes 2, case_names empty insert]:
fba08009ff3e add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents: 63404
diff changeset
   650
  assumes "finite F" and "F \<subseteq> A"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   651
    and empty: "P {}"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   652
    and insert: "\<And>a F. \<lbrakk>finite F; a \<in> A; F \<subseteq> A; a \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert a F)"
63561
fba08009ff3e add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents: 63404
diff changeset
   653
  shows "P F"
63915
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   654
  using assms(1,2)
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   655
proof induct
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   656
  show "P {}" by fact
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   657
next
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   658
  fix x F
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   659
  assume "finite F" and "x \<notin> F" and
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   660
    P: "F \<subseteq> A \<Longrightarrow> P F" and i: "insert x F \<subseteq> A"
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   661
  show "P (insert x F)"
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   662
  proof (rule insert)
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   663
    from i show "x \<in> A" by blast
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   664
    from i have "F \<subseteq> A" by blast
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   665
    with P show "P F" .
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   666
    show "finite F" by fact
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   667
    show "x \<notin> F" by fact
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
   668
    show "F \<subseteq> A" by fact
63561
fba08009ff3e add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents: 63404
diff changeset
   669
  qed
fba08009ff3e add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents: 63404
diff changeset
   670
qed
fba08009ff3e add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents: 63404
diff changeset
   671
58195
1fee63e0377d added various facts
haftmann
parents: 57598
diff changeset
   672
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61778
diff changeset
   673
subsection \<open>Class \<open>finite\<close>\<close>
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
   674
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   675
class finite =
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   676
  assumes finite_UNIV: "finite (UNIV :: 'a set)"
27430
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   677
begin
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   678
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60762
diff changeset
   679
lemma finite [simp]: "finite (A :: 'a set)"
26441
7914697ff104 no "attach UNIV" any more
haftmann
parents: 26146
diff changeset
   680
  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
   681
61076
bdc1e2f0a86a eliminated \<Colon>;
wenzelm
parents: 60762
diff changeset
   682
lemma finite_code [code]: "finite (A :: 'a set) \<longleftrightarrow> True"
40922
4d0f96a54e76 adding code equation for finiteness of finite types
bulwahn
parents: 40786
diff changeset
   683
  by simp
4d0f96a54e76 adding code equation for finiteness of finite types
bulwahn
parents: 40786
diff changeset
   684
27430
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   685
end
1e25ac05cd87 prove lemma finite in context of finite class
huffman
parents: 27418
diff changeset
   686
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   687
instance prod :: (finite, finite) finite
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   688
  by standard (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
26146
61cb176d0385 tuned proofs
haftmann
parents: 26041
diff changeset
   689
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   690
lemma inj_graph: "inj (\<lambda>f. {(x, y). y = f x})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   691
  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
   692
26146
61cb176d0385 tuned proofs
haftmann
parents: 26041
diff changeset
   693
instance "fun" :: (finite, finite) finite
61cb176d0385 tuned proofs
haftmann
parents: 26041
diff changeset
   694
proof
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   695
  show "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
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
   696
  proof (rule finite_imageD)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   697
    let ?graph = "\<lambda>f::'a \<Rightarrow> 'b. {(x, y). y = f x}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   698
    have "range ?graph \<subseteq> Pow UNIV"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   699
      by simp
26792
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   700
    moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   701
      by (simp only: finite_Pow_iff finite)
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   702
    ultimately show "finite (range ?graph)"
f2d75fd23124 - Deleted code setup for finite and card
berghofe
parents: 26757
diff changeset
   703
      by (rule finite_subset)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   704
    show "inj ?graph"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   705
      by (rule inj_graph)
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
   706
  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
   707
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
   708
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   709
instance bool :: finite
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   710
  by standard (simp add: UNIV_bool)
44831
haftmann
parents: 43991
diff changeset
   711
45962
fc77947a7db4 finite type class instance for `set`
haftmann
parents: 45166
diff changeset
   712
instance set :: (finite) finite
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   713
  by standard (simp only: Pow_UNIV [symmetric] finite_Pow_iff finite)
45962
fc77947a7db4 finite type class instance for `set`
haftmann
parents: 45166
diff changeset
   714
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   715
instance unit :: finite
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   716
  by standard (simp add: UNIV_unit)
44831
haftmann
parents: 43991
diff changeset
   717
46898
1570b30ee040 tuned proofs -- eliminated pointless chaining of facts after 'interpret';
wenzelm
parents: 46146
diff changeset
   718
instance sum :: (finite, finite) finite
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   719
  by standard (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
27981
feb0c01cf0fb tuned import order
haftmann
parents: 27611
diff changeset
   720
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
   721
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   722
subsection \<open>A basic fold functional for finite sets\<close>
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   723
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   724
text \<open>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   725
  The intended behaviour is \<open>fold f z {x\<^sub>1, \<dots>, x\<^sub>n} = f x\<^sub>1 (\<dots> (f x\<^sub>n z)\<dots>)\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   726
  if \<open>f\<close> is ``left-commutative''.
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   727
  The commutativity requirement is relativised to the carrier set \<open>S\<close>:
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   728
\<close>
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   729
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   730
locale comp_fun_commute_on =
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   731
  fixes S :: "'a set"
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   732
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   733
  assumes comp_fun_commute_on: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> 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
   734
begin
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   735
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   736
lemma fun_left_comm: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f y (f x z) = f x (f y z)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   737
  using comp_fun_commute_on 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
   738
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   739
lemma commute_left_comp: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f y \<circ> (f x \<circ> g) = f x \<circ> (f y \<circ> g)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   740
  by (simp add: o_assoc comp_fun_commute_on)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   741
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   742
end
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   743
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   744
inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   745
  for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   746
  where
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   747
    emptyI [intro]: "fold_graph f z {} z"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
   748
  | insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   749
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   750
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
   751
68521
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   752
lemma fold_graph_closed_lemma:
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   753
  "fold_graph f z A x \<and> x \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   754
  if "fold_graph g z A x"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   755
    "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   756
    "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   757
    "z \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   758
  using that(1-3)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   759
proof (induction rule: fold_graph.induct)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   760
  case (insertI x A y)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   761
  have "fold_graph f z A y" "y \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   762
    unfolding atomize_conj
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   763
    by (rule insertI.IH) (auto intro: insertI.prems)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   764
  then have "g x y \<in> B" and f_eq: "f x y = g x y"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   765
    by (auto simp: insertI.prems)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   766
  moreover have "fold_graph f z (insert x A) (f x y)"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   767
    by (rule fold_graph.insertI; fact)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   768
  ultimately
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   769
  show ?case
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   770
    by (simp add: f_eq)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   771
qed (auto intro!: that)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   772
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   773
lemma fold_graph_closed_eq:
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   774
  "fold_graph f z A = fold_graph g z A"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   775
  if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   776
     "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   777
     "z \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   778
  using fold_graph_closed_lemma[of f z A _ B g] fold_graph_closed_lemma[of g z A _ B f] that
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   779
  by auto
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   780
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   781
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   782
  where "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
   783
68521
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   784
lemma fold_closed_eq: "fold f z A = fold g z A"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   785
  if "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> f a b = g a b"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   786
     "\<And>a b. a \<in> A \<Longrightarrow> b \<in> B \<Longrightarrow> g a b \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   787
     "z \<in> B"
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   788
  unfolding Finite_Set.fold_def
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   789
  by (subst fold_graph_closed_eq[where B=B and g=g]) (auto simp: that)
1bad08165162 added lemmas and transfer rules
immler
parents: 68463
diff changeset
   790
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   791
text \<open>
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   792
  A tempting alternative for the definition is
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
   793
  \<^term>\<open>if finite A then THE y. fold_graph f z A y else e\<close>.
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   794
  It allows the removal of finiteness assumptions from the theorems
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   795
  \<open>fold_comm\<close>, \<open>fold_reindex\<close> and \<open>fold_distrib\<close>.
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   796
  The proofs become ugly. It is not worth the effort. (???)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   797
\<close>
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   798
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   799
lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   800
  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
   801
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   802
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
   803
subsubsection \<open>From \<^const>\<open>fold_graph\<close> to \<^term>\<open>fold\<close>\<close>
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   804
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   805
context comp_fun_commute_on
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
   806
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
   807
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   808
lemma fold_graph_finite:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   809
  assumes "fold_graph f z A y"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   810
  shows "finite A"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   811
  using assms by induct simp_all
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   812
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   813
lemma fold_graph_insertE_aux:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   814
  assumes "A \<subseteq> S"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   815
  assumes "fold_graph f z A y" "a \<in> A"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   816
  shows "\<exists>y'. y = f a y' \<and> fold_graph f z (A - {a}) y'"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   817
  using assms(2-,1)
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   818
proof (induct set: fold_graph)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   819
  case emptyI
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   820
  then show ?case by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   821
next
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   822
  case (insertI x A y)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   823
  show ?case
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   824
  proof (cases "x = a")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   825
    case True
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   826
    with insertI show ?thesis by auto
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   827
  next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   828
    case False
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   829
    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
   830
      using insertI by auto
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   831
    from insertI have "x \<in> S" "a \<in> S" by auto
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   832
    then have "f x y = f a (f x y')"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   833
      unfolding y by (intro fun_left_comm; simp)
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   834
    moreover have "fold_graph f z (insert x A - {a}) (f x y')"
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
   835
      using y' and \<open>x \<noteq> a\<close> and \<open>x \<notin> A\<close>
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   836
      by (simp add: insert_Diff_if fold_graph.insertI)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   837
    ultimately show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   838
      by fast
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   839
  qed
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   840
qed
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   841
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   842
lemma fold_graph_insertE:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   843
  assumes "insert x A \<subseteq> S"
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   844
  assumes "fold_graph f z (insert x A) v" and "x \<notin> A"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   845
  obtains y where "v = f x y" and "fold_graph f z A y"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   846
  using assms by (auto dest: fold_graph_insertE_aux[OF \<open>insert x A \<subseteq> S\<close> _ insertI1])
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   847
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   848
lemma fold_graph_determ:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   849
  assumes "A \<subseteq> S"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   850
  assumes "fold_graph f z A x" "fold_graph f z A y"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   851
  shows "y = x"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   852
  using assms(2-,1)
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   853
proof (induct arbitrary: y set: fold_graph)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   854
  case emptyI
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   855
  then show ?case by fast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   856
next
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   857
  case (insertI x A y v)
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   858
  from \<open>insert x A \<subseteq> S\<close> and \<open>fold_graph f z (insert x A) v\<close> and \<open>x \<notin> A\<close>
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   859
  obtain y' where "v = f x y'" and "fold_graph f z A y'"
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   860
    by (rule fold_graph_insertE)
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   861
  from \<open>fold_graph f z A y'\<close> insertI have "y' = y"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   862
    by simp
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   863
  with \<open>v = f x y'\<close> show "v = f x y"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   864
    by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   865
qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   866
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   867
lemma fold_equality: "A \<subseteq> S \<Longrightarrow> 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
   868
  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
   869
42272
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   870
lemma fold_graph_fold:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   871
  assumes "A \<subseteq> S"
42272
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   872
  assumes "finite A"
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   873
  shows "fold_graph f z A (fold f z A)"
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   874
proof -
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   875
  from \<open>finite A\<close> have "\<exists>x. fold_graph f z A x"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   876
    by (rule finite_imp_fold_graph)
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   877
  moreover note fold_graph_determ[OF \<open>A \<subseteq> S\<close>]
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   878
  ultimately have "\<exists>!x. fold_graph f z A x"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   879
    by (rule ex_ex1I)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   880
  then have "fold_graph f z A (The (fold_graph f z A))"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   881
    by (rule theI')
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   882
  with assms show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   883
    by (simp add: fold_def)
42272
a46a13b4be5f dropped unused lemmas; proper Isar proof
haftmann
parents: 42207
diff changeset
   884
qed
36045
b846881928ea simplify fold_graph proofs
huffman
parents: 35831
diff changeset
   885
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61778
diff changeset
   886
text \<open>The base case for \<open>fold\<close>:\<close>
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   887
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   888
lemma (in -) fold_infinite [simp]: "\<not> finite A \<Longrightarrow> fold f z A = z"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   889
  by (auto simp: fold_def)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   890
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   891
lemma (in -) fold_empty [simp]: "fold f z {} = z"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   892
  by (auto simp: fold_def)
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   893
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
   894
text \<open>The various recursion equations for \<^const>\<open>fold\<close>:\<close>
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   895
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
   896
lemma fold_insert [simp]:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   897
  assumes "insert x A \<subseteq> S"
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   898
  assumes "finite A" and "x \<notin> A"
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   899
  shows "fold f z (insert x A) = f x (fold f z A)"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   900
proof (rule fold_equality[OF \<open>insert x A \<subseteq> S\<close>])
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   901
  fix z
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   902
  from \<open>insert x A \<subseteq> S\<close> \<open>finite A\<close> have "fold_graph f z A (fold f z A)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   903
    by (blast intro: fold_graph_fold)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   904
  with \<open>x \<notin> A\<close> have "fold_graph f z (insert x A) (f x (fold f z A))"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   905
    by (rule fold_graph.insertI)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   906
  then show "fold_graph f z (insert x A) (f x (fold f z A))"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   907
    by simp
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   908
qed
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   909
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   910
declare (in -) empty_fold_graphE [rule del] fold_graph.intros [rule del]
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61778
diff changeset
   911
  \<comment> \<open>No more proofs involve these.\<close>
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
   912
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   913
lemma fold_fun_left_comm:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   914
  assumes "insert x A \<subseteq> S" "finite A" 
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   915
  shows "f x (fold f z A) = fold f (f x z) A"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   916
  using assms(2,1)
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   917
proof (induct rule: finite_induct)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   918
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   919
  then show ?case by simp
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   920
next
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   921
  case (insert y F)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   922
  then have "fold f (f x z) (insert y F) = f y (fold f (f x z) F)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   923
    by simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   924
  also have "\<dots> = f x (f y (fold f z F))"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   925
    using insert by (simp add: fun_left_comm[where ?y=x])
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   926
  also have "\<dots> = f x (fold f z (insert y F))"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   927
  proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   928
    from insert have "insert y F \<subseteq> S" by simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   929
    from fold_insert[OF this] insert show ?thesis by simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   930
  qed
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   931
  finally show ?case ..
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   932
qed
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   933
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   934
lemma fold_insert2:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   935
  "insert x A \<subseteq> S \<Longrightarrow> finite A \<Longrightarrow> x \<notin> 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
   936
  by (simp add: fold_fun_left_comm)
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
   937
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
   938
lemma fold_rec:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   939
  assumes "A \<subseteq> S"
42875
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   940
  assumes "finite A" and "x \<in> A"
d1aad0957eb2 tuned proofs
haftmann
parents: 42873
diff changeset
   941
  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
   942
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   943
  have A: "A = insert x (A - {x})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   944
    using \<open>x \<in> A\<close> by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   945
  then have "fold f z A = fold f z (insert x (A - {x}))"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   946
    by simp
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   947
  also have "\<dots> = f x (fold f z (A - {x}))"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   948
    by (rule fold_insert) (use assms in \<open>auto\<close>)
15535
nipkow
parents: 15532
diff changeset
   949
  finally show ?thesis .
nipkow
parents: 15532
diff changeset
   950
qed
nipkow
parents: 15532
diff changeset
   951
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   952
lemma fold_insert_remove:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   953
  assumes "insert x A \<subseteq> S"
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   954
  assumes "finite A"
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   955
  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
   956
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   957
  from \<open>finite A\<close> have "finite (insert x A)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   958
    by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   959
  moreover have "x \<in> insert x A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   960
    by auto
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   961
  ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   962
    using \<open>insert x A \<subseteq> S\<close> by (blast intro: fold_rec)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   963
  then show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
   964
    by simp
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   965
qed
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
   966
57598
56ed992b6d65 add lemma
Andreas Lochbihler
parents: 57447
diff changeset
   967
lemma fold_set_union_disj:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   968
  assumes "A \<subseteq> S" "B \<subseteq> S"
57598
56ed992b6d65 add lemma
Andreas Lochbihler
parents: 57447
diff changeset
   969
  assumes "finite A" "finite B" "A \<inter> B = {}"
56ed992b6d65 add lemma
Andreas Lochbihler
parents: 57447
diff changeset
   970
  shows "Finite_Set.fold f z (A \<union> B) = Finite_Set.fold f (Finite_Set.fold f z A) B"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   971
  using \<open>finite B\<close> assms(1,2,3,5)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   972
proof induct
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   973
  case (insert x F)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   974
  have "fold f z (A \<union> insert x F) = f x (fold f (fold f z A) F)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   975
    using insert by auto
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   976
  also have "\<dots> = fold f (fold f z A) (insert x F)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   977
    using insert by (blast intro: fold_insert[symmetric])
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   978
  finally show ?case .
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   979
qed simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   980
57598
56ed992b6d65 add lemma
Andreas Lochbihler
parents: 57447
diff changeset
   981
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   982
end
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
   983
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
   984
text \<open>Other properties of \<^const>\<open>fold\<close>:\<close>
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
   985
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   986
lemma fold_graph_image:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   987
  assumes "inj_on g A"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   988
  shows "fold_graph f z (g ` A) = fold_graph (f \<circ> g) z A"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   989
proof
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   990
  fix w
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   991
  show "fold_graph f z (g ` A) w = fold_graph (f o g) z A w"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   992
  proof
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   993
    assume "fold_graph f z (g ` A) w"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   994
    then show "fold_graph (f \<circ> g) z A w"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   995
      using assms
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   996
    proof (induct "g ` A" w arbitrary: A)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   997
      case emptyI
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   998
      then show ?case by (auto intro: fold_graph.emptyI)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
   999
    next
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1000
      case (insertI x A r B)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1001
      from \<open>inj_on g B\<close> \<open>x \<notin> A\<close> \<open>insert x A = image g B\<close> obtain x' A'
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1002
        where "x' \<notin> A'" and [simp]: "B = insert x' A'" "x = g x'" "A = g ` A'"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1003
        by (rule inj_img_insertE)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1004
      from insertI.prems have "fold_graph (f \<circ> g) z A' r"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1005
        by (auto intro: insertI.hyps)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1006
      with \<open>x' \<notin> A'\<close> have "fold_graph (f \<circ> g) z (insert x' A') ((f \<circ> g) x' r)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1007
        by (rule fold_graph.insertI)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1008
      then show ?case
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1009
        by simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1010
    qed
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1011
  next
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1012
    assume "fold_graph (f \<circ> g) z A w"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1013
    then show "fold_graph f z (g ` A) w"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1014
      using assms
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1015
    proof induct
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1016
      case emptyI
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1017
      then show ?case
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1018
        by (auto intro: fold_graph.emptyI)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1019
    next
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1020
      case (insertI x A r)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1021
      from \<open>x \<notin> A\<close> insertI.prems have "g x \<notin> g ` A"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1022
        by auto
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1023
      moreover from insertI have "fold_graph f z (g ` A) r"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1024
        by simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1025
      ultimately have "fold_graph f z (insert (g x) (g ` A)) (f (g x) r)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1026
        by (rule fold_graph.insertI)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1027
      then show ?case
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1028
        by simp
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1029
    qed
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1030
  qed
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1031
qed
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1032
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1033
lemma fold_image:
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
  1034
  assumes "inj_on g A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1035
  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
  1036
proof (cases "finite A")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1037
  case False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1038
  with assms show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1039
    by (auto dest: finite_imageD simp add: fold_def)
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
  1040
next
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
  1041
  case True
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1042
  then show ?thesis
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1043
    by (auto simp add: fold_def fold_graph_image[OF assms])
51598
5dbe537087aa generalized lemma fold_image thanks to Peter Lammich
haftmann
parents: 51546
diff changeset
  1044
qed
15392
290bc97038c7 First step in reorganizing Finite_Set
nipkow
parents: 15376
diff changeset
  1045
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1046
lemma fold_cong:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1047
  assumes "comp_fun_commute_on S f" "comp_fun_commute_on S g"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1048
    and "A \<subseteq> S" "finite A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1049
    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
  1050
    and "s = t" and "A = B"
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1051
  shows "fold f s A = fold g t B"
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1052
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1053
  have "fold f s A = fold g s A"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1054
    using \<open>finite A\<close> \<open>A \<subseteq> S\<close> cong
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1055
  proof (induct A)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1056
    case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1057
    then show ?case by simp
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1058
  next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1059
    case insert
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1060
    interpret f: comp_fun_commute_on S f by (fact \<open>comp_fun_commute_on S f\<close>)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1061
    interpret g: comp_fun_commute_on S g by (fact \<open>comp_fun_commute_on S g\<close>)
49724
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1062
    from insert show ?case by simp
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1063
  qed
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1064
  with assms show ?thesis by simp
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1065
qed
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1066
a5842f026d4c congruence rule for Finite_Set.fold
haftmann
parents: 49723
diff changeset
  1067
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1068
text \<open>A simplified version for idempotent functions:\<close>
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
  1069
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1070
locale comp_fun_idem_on = comp_fun_commute_on +
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1071
  assumes comp_fun_idem_on: "x \<in> S \<Longrightarrow> 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
  1072
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
  1073
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1074
lemma fun_left_idem: "x \<in> S \<Longrightarrow> f x (f x z) = f x z"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1075
  using comp_fun_idem_on 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
  1076
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
  1077
lemma fold_insert_idem:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1078
  assumes "insert x A \<subseteq> S"
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
  1079
  assumes fin: "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1080
  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
  1081
proof cases
28853
69eb69659bf3 Added new fold operator and renamed the old oe to fold_image.
nipkow
parents: 28823
diff changeset
  1082
  assume "x \<in> A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1083
  then obtain B where "A = insert x B" and "x \<notin> B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1084
    by (rule set_insert)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1085
  then show ?thesis
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1086
    using assms by (simp add: comp_fun_idem_on fun_left_idem)
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
  1087
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1088
  assume "x \<notin> A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1089
  then show ?thesis
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1090
    using assms by auto
15480
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
  1091
qed
cb3612cc41a3 renamed a few vars, added a lemma
nipkow
parents: 15479
diff changeset
  1092
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1093
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
  1094
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1095
lemma fold_insert_idem2: "insert x A \<subseteq> S \<Longrightarrow> 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
  1096
  by (simp add: fold_fun_left_comm)
15484
2636ec211ec8 fold and fol1 changes
nipkow
parents: 15483
diff changeset
  1097
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
  1098
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
  1099
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1100
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1101
subsubsection \<open>Liftings to \<open>comp_fun_commute_on\<close> etc.\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1102
                   
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1103
lemma (in comp_fun_commute_on) comp_comp_fun_commute_on:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1104
  "range g \<subseteq> S \<Longrightarrow> comp_fun_commute_on R (f \<circ> g)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1105
  by standard (force intro: comp_fun_commute_on)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1106
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1107
lemma (in comp_fun_idem_on) comp_comp_fun_idem_on:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1108
  assumes "range g \<subseteq> S"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1109
  shows "comp_fun_idem_on R (f \<circ> g)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1110
proof
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1111
  interpret f_g: comp_fun_commute_on R "f o g"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1112
    by (fact comp_comp_fun_commute_on[OF \<open>range g \<subseteq> S\<close>])
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1113
  show "x \<in> R \<Longrightarrow> y \<in> R \<Longrightarrow> (f \<circ> g) y \<circ> (f \<circ> g) x = (f \<circ> g) x \<circ> (f \<circ> g) y" for x y
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1114
    by (fact f_g.comp_fun_commute_on)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1115
qed (use \<open>range g \<subseteq> S\<close> in \<open>force intro: comp_fun_idem_on\<close>)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1116
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1117
lemma (in comp_fun_commute_on) comp_fun_commute_on_funpow:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1118
  "comp_fun_commute_on S (\<lambda>x. f x ^^ g x)"
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1119
proof
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1120
  fix x y assume "x \<in> S" "y \<in> S"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1121
  show "f y ^^ g y \<circ> f x ^^ g x = f x ^^ g x \<circ> f y ^^ g y"
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1122
  proof (cases "x = y")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1123
    case True
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1124
    then show ?thesis by simp
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1125
  next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1126
    case False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1127
    show ?thesis
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1128
    proof (induct "g x" arbitrary: g)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1129
      case 0
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1130
      then show ?case by simp
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1131
    next
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1132
      case (Suc n g)
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1133
      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
  1134
      proof (induct "g y" arbitrary: g)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1135
        case 0
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1136
        then show ?case by simp
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1137
      next
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1138
        case (Suc n g)
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62618
diff changeset
  1139
        define h where "h z = g z - 1" for z
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1140
        with Suc have "n = h y"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1141
          by simp
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1142
        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
  1143
          by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1144
        from Suc h_def have "g y = Suc (h y)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1145
          by simp
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1146
        with \<open>x \<in> S\<close> \<open>y \<in> S\<close> show ?case
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1147
          by (simp add: comp_assoc hyp) (simp add: o_assoc comp_fun_commute_on)
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1148
      qed
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62618
diff changeset
  1149
      define h where "h z = (if z = x then g x - 1 else g z)" for z
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1150
      with Suc have "n = h x"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1151
        by simp
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1152
      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
  1153
        by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1154
      with False h_def have hyp2: "f y ^^ g y \<circ> f x ^^ h x = f x ^^ h x \<circ> f y ^^ g y"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1155
        by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1156
      from Suc h_def have "g x = Suc (h x)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1157
        by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1158
      then show ?case
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1159
        by (simp del: funpow.simps add: funpow_Suc_right o_assoc hyp2) (simp add: comp_assoc hyp1)
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1160
    qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1161
  qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1162
qed
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1163
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1164
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1165
subsubsection \<open>\<^term>\<open>UNIV\<close> as carrier set\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1166
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1167
locale comp_fun_commute =
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1168
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1169
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1170
begin
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1171
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1172
lemma (in -) comp_fun_commute_def': "comp_fun_commute f = comp_fun_commute_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1173
  unfolding comp_fun_commute_def comp_fun_commute_on_def by blast
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1174
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1175
text \<open>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1176
  We abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1177
  result from instantiating the carrier set to \<^term>\<open>UNIV\<close>.
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1178
\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1179
sublocale comp_fun_commute_on UNIV f
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1180
  rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1181
       and "\<And>x. x \<in> UNIV \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1182
       and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1183
       and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1184
proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1185
  show "comp_fun_commute_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1186
    by standard  (simp add: comp_fun_commute)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1187
qed simp_all
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1188
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1189
end
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1190
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1191
lemma (in comp_fun_commute) comp_comp_fun_commute: "comp_fun_commute (f o g)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1192
  unfolding comp_fun_commute_def' by (fact comp_comp_fun_commute_on)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1193
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1194
lemma (in comp_fun_commute) comp_fun_commute_funpow: "comp_fun_commute (\<lambda>x. f x ^^ g x)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1195
  unfolding comp_fun_commute_def' by (fact comp_fun_commute_on_funpow)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1196
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1197
locale comp_fun_idem = comp_fun_commute +
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1198
  assumes comp_fun_idem: "f x o f x = f x"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1199
begin
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1200
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1201
lemma (in -) comp_fun_idem_def': "comp_fun_idem f = comp_fun_idem_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1202
  unfolding comp_fun_idem_on_def comp_fun_idem_def comp_fun_commute_def'
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1203
  unfolding comp_fun_idem_axioms_def comp_fun_idem_on_axioms_def
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1204
  by blast
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1205
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1206
text \<open>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1207
  Again, we abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1208
  result from instantiating the carrier set to \<^term>\<open>UNIV\<close>.
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1209
\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1210
sublocale comp_fun_idem_on UNIV f
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1211
  rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1212
       and "\<And>x. x \<in> UNIV \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1213
       and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1214
       and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1215
proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1216
  show "comp_fun_idem_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1217
    by standard (simp_all add: comp_fun_idem comp_fun_commute)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1218
qed simp_all
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1219
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1220
end
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1221
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1222
lemma (in comp_fun_idem) comp_comp_fun_idem: "comp_fun_idem (f o g)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1223
  unfolding comp_fun_idem_def' by (fact comp_comp_fun_idem_on)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1224
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1225
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
  1226
subsubsection \<open>Expressing set operations via \<^const>\<open>fold\<close>\<close>
49723
bbc2942ba09f alternative simplification of ^^ to the righthand side;
haftmann
parents: 48891
diff changeset
  1227
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1228
lemma comp_fun_commute_const: "comp_fun_commute (\<lambda>_. f)"
75669
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1229
  by standard (rule refl)
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1230
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1231
lemma comp_fun_idem_insert: "comp_fun_idem insert"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1232
  by standard auto
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1233
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1234
lemma comp_fun_idem_remove: "comp_fun_idem Set.remove"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1235
  by standard auto
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1236
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1237
lemma (in semilattice_inf) comp_fun_idem_inf: "comp_fun_idem inf"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1238
  by standard (auto simp add: inf_left_commute)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1239
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1240
lemma (in semilattice_sup) comp_fun_idem_sup: "comp_fun_idem sup"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1241
  by standard (auto simp add: sup_left_commute)
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1242
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1243
lemma union_fold_insert:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1244
  assumes "finite A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1245
  shows "A \<union> B = fold insert B A"
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1246
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1247
  interpret comp_fun_idem insert
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1248
    by (fact comp_fun_idem_insert)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1249
  from \<open>finite A\<close> show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1250
    by (induct A arbitrary: B) simp_all
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1251
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1252
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1253
lemma minus_fold_remove:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1254
  assumes "finite A"
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1255
  shows "B - A = fold Set.remove B A"
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1256
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1257
  interpret comp_fun_idem Set.remove
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1258
    by (fact comp_fun_idem_remove)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1259
  from \<open>finite A\<close> have "fold Set.remove B A = B - A"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1260
    by (induct A arbitrary: B) auto  (* slow *)
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1261
  then show ?thesis ..
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1262
qed
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1263
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1264
lemma comp_fun_commute_filter_fold:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1265
  "comp_fun_commute (\<lambda>x A'. if P x then Set.insert x A' else A')"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1266
proof -
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1267
  interpret comp_fun_idem Set.insert by (fact comp_fun_idem_insert)
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
  1268
  show ?thesis by standard (auto simp: fun_eq_iff)
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1269
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1270
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  1271
lemma Set_filter_fold:
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1272
  assumes "finite A"
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  1273
  shows "Set.filter P A = fold (\<lambda>x A'. if P x then Set.insert x A' else A') {} A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1274
  using assms
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1275
proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1276
  interpret commute_insert: comp_fun_commute "(\<lambda>x A'. if P x then Set.insert x A' else A')"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1277
    by (fact comp_fun_commute_filter_fold)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1278
  from \<open>finite A\<close> show ?thesis
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1279
    by induct (auto simp add: Set.filter_def)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1280
qed
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  1281
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1282
lemma inter_Set_filter:
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  1283
  assumes "finite B"
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  1284
  shows "A \<inter> B = Set.filter (\<lambda>x. x \<in> A) B"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1285
  using assms
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1286
  by induct (auto simp: Set.filter_def)
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1287
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1288
lemma image_fold_insert:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1289
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1290
  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
  1291
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1292
  interpret comp_fun_commute "\<lambda>k A. Set.insert (f k) A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1293
    by standard auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1294
  show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1295
    using assms by (induct A) auto
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1296
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1297
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1298
lemma Ball_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1299
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1300
  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
  1301
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1302
  interpret comp_fun_commute "\<lambda>k s. s \<and> P k"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1303
    by standard auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1304
  show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1305
    using assms by (induct A) auto
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1306
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1307
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1308
lemma Bex_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1309
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1310
  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
  1311
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1312
  interpret comp_fun_commute "\<lambda>k s. s \<or> P k"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1313
    by standard auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1314
  show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1315
    using assms by (induct A) auto
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1316
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1317
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1318
lemma comp_fun_commute_Pow_fold: "comp_fun_commute (\<lambda>x A. A \<union> Set.insert x ` A)"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1319
  by (clarsimp simp: fun_eq_iff comp_fun_commute_def) blast
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1320
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1321
lemma Pow_fold:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1322
  assumes "finite A"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1323
  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
  1324
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1325
  interpret comp_fun_commute "\<lambda>x A. A \<union> Set.insert x ` A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1326
    by (rule comp_fun_commute_Pow_fold)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1327
  show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1328
    using assms by (induct A) (auto simp: Pow_insert)
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1329
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1330
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1331
lemma fold_union_pair:
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1332
  assumes "finite B"
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1333
  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
  1334
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1335
  interpret comp_fun_commute "\<lambda>y. Set.insert (x, y)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1336
    by standard auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1337
  show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1338
    using assms by (induct arbitrary: A) simp_all
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1339
qed
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1340
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1341
lemma comp_fun_commute_product_fold:
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1342
  "finite B \<Longrightarrow> comp_fun_commute (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1343
  by standard (auto simp: fold_union_pair [symmetric])
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1344
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1345
lemma product_fold:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1346
  assumes "finite A" "finite B"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1347
  shows "A \<times> B = fold (\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B) {} A"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1348
proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1349
  interpret commute_product: comp_fun_commute "(\<lambda>x z. fold (\<lambda>y. Set.insert (x, y)) z B)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1350
    by (fact comp_fun_commute_product_fold[OF \<open>finite B\<close>])
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1351
  from assms show ?thesis unfolding Sigma_def
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1352
    by (induct A) (simp_all add: fold_union_pair)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1353
qed
48619
558e4e77ce69 more set operations expressed by Finite_Set.fold
kuncar
parents: 48175
diff changeset
  1354
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1355
context complete_lattice
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1356
begin
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1357
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1358
lemma inf_Inf_fold_inf:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1359
  assumes "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1360
  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
  1361
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1362
  interpret comp_fun_idem inf
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1363
    by (fact comp_fun_idem_inf)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1364
  from \<open>finite A\<close> fold_fun_left_comm show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1365
    by (induct A arbitrary: B) (simp_all add: inf_commute fun_eq_iff)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1366
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1367
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1368
lemma sup_Sup_fold_sup:
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1369
  assumes "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1370
  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
  1371
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1372
  interpret comp_fun_idem sup
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1373
    by (fact comp_fun_idem_sup)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1374
  from \<open>finite A\<close> fold_fun_left_comm show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1375
    by (induct A arbitrary: B) (simp_all add: sup_commute fun_eq_iff)
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1376
qed
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1377
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1378
lemma Inf_fold_inf: "finite A \<Longrightarrow> Inf A = fold inf top A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1379
  using inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1380
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1381
lemma Sup_fold_sup: "finite A \<Longrightarrow> Sup A = fold sup bot A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1382
  using 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
  1383
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1384
lemma inf_INF_fold_inf:
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1385
  assumes "finite A"
69275
9bbd5497befd clarified status of legacy input abbreviations
haftmann
parents: 69235
diff changeset
  1386
  shows "inf B (\<Sqinter>(f ` A)) = fold (inf \<circ> f) B A" (is "?inf = ?fold")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1387
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1388
  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
  1389
  interpret comp_fun_idem "inf \<circ> f" by (fact comp_comp_fun_idem)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1390
  from \<open>finite A\<close> have "?fold = ?inf"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1391
    by (induct A arbitrary: B) (simp_all add: inf_left_commute)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1392
  then show ?thesis ..
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1393
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1394
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  1395
lemma sup_SUP_fold_sup:
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1396
  assumes "finite A"
69275
9bbd5497befd clarified status of legacy input abbreviations
haftmann
parents: 69235
diff changeset
  1397
  shows "sup B (\<Squnion>(f ` A)) = fold (sup \<circ> f) B A" (is "?sup = ?fold")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1398
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42869
diff changeset
  1399
  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
  1400
  interpret comp_fun_idem "sup \<circ> f" by (fact comp_comp_fun_idem)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1401
  from \<open>finite A\<close> have "?fold = ?sup"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1402
    by (induct A arbitrary: B) (simp_all add: sup_left_commute)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1403
  then show ?thesis ..
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1404
qed
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1405
69275
9bbd5497befd clarified status of legacy input abbreviations
haftmann
parents: 69235
diff changeset
  1406
lemma INF_fold_inf: "finite A \<Longrightarrow> \<Sqinter>(f ` A) = fold (inf \<circ> f) top A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1407
  using inf_INF_fold_inf [of A top] by simp
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1408
69275
9bbd5497befd clarified status of legacy input abbreviations
haftmann
parents: 69235
diff changeset
  1409
lemma SUP_fold_sup: "finite A \<Longrightarrow> \<Squnion>(f ` A) = fold (sup \<circ> f) bot A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1410
  using sup_SUP_fold_sup [of A bot] by simp
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1411
72097
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1412
lemma finite_Inf_in:
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1413
  assumes "finite A" "A\<noteq>{}" and inf: "\<And>x y. \<lbrakk>x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> inf x y \<in> A"
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1414
  shows "Inf A \<in> A"
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1415
proof -
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1416
  have "Inf B \<in> A" if "B \<le> A" "B\<noteq>{}" for B
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1417
    using finite_subset [OF \<open>B \<subseteq> A\<close> \<open>finite A\<close>] that
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1418
  by (induction B) (use inf in \<open>force+\<close>)
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1419
  then show ?thesis
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1420
    by (simp add: assms)
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1421
qed
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1422
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1423
lemma finite_Sup_in:
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1424
  assumes "finite A" "A\<noteq>{}" and sup: "\<And>x y. \<lbrakk>x \<in> A; y \<in> A\<rbrakk> \<Longrightarrow> sup x y \<in> A"
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1425
  shows "Sup A \<in> A"
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1426
proof -
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1427
  have "Sup B \<in> A" if "B \<le> A" "B\<noteq>{}" for B
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1428
    using finite_subset [OF \<open>B \<subseteq> A\<close> \<open>finite A\<close>] that
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1429
  by (induction B) (use sup in \<open>force+\<close>)
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1430
  then show ?thesis
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1431
    by (simp add: assms)
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1432
qed
496cfe488d72 a few more lemmas
paulson <lp15@cam.ac.uk>
parents: 72095
diff changeset
  1433
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1434
end
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1435
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 31916
diff changeset
  1436
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1437
subsection \<open>Locales as mini-packages for fold operations\<close>
34007
aea892559fc5 tuned lattices theory fragements; generlized some lemmas from sets to lattices
haftmann
parents: 33960
diff changeset
  1438
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1439
subsubsection \<open>The natural case\<close>
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1440
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1441
locale folding_on =
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1442
  fixes S :: "'a set"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1443
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: "'b"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1444
  assumes comp_fun_commute_on: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f y o f x = f x o f y"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1445
begin
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1446
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1447
interpretation fold?: comp_fun_commute_on S f
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1448
  by standard (simp add: comp_fun_commute_on)
54867
c21a2465cac1 prefer ephemeral interpretation over interpretation in proof contexts;
haftmann
parents: 54611
diff changeset
  1449
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1450
definition F :: "'a set \<Rightarrow> 'b"
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1451
  where eq_fold: "F A = Finite_Set.fold f z A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1452
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1453
lemma empty [simp]: "F {} = z"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1454
  by (simp add: eq_fold)
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1455
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
  1456
lemma infinite [simp]: "\<not> finite A \<Longrightarrow> F A = z"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1457
  by (simp add: eq_fold)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1458
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1459
lemma insert [simp]:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1460
  assumes "insert x A \<subseteq> S" and "finite A" and "x \<notin> A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1461
  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
  1462
proof -
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1463
  from fold_insert assms
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1464
  have "Finite_Set.fold f z (insert x A) 
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1465
      = f x (Finite_Set.fold f z A)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1466
    by simp
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1467
  with \<open>finite A\<close> 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
  1468
qed
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1469
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1470
lemma remove:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1471
  assumes "A \<subseteq> S" and "finite A" and "x \<in> A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1472
  shows "F A = f x (F (A - {x}))"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1473
proof -
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1474
  from \<open>x \<in> A\<close> obtain B where A: "A = insert x B" and "x \<notin> B"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1475
    by (auto dest: mk_disjoint_insert)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1476
  moreover from \<open>finite A\<close> A have "finite B" by simp
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1477
  ultimately show ?thesis
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1478
    using \<open>A \<subseteq> S\<close> by auto
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1479
qed
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1480
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1481
lemma insert_remove:
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1482
  assumes "insert x A \<subseteq> S" and "finite A"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1483
  shows "F (insert x A) = f x (F (A - {x}))"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1484
  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
  1485
34007
aea892559fc5 tuned lattices theory fragements; generlized some lemmas from sets to lattices
haftmann
parents: 33960
diff changeset
  1486
end
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1487
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1488
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1489
subsubsection \<open>With idempotency\<close>
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1490
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1491
locale folding_idem_on = folding_on +
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1492
  assumes comp_fun_idem_on: "x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f x \<circ> f x = f x"
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1493
begin
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1494
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1495
declare insert [simp del]
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1496
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1497
interpretation fold?: comp_fun_idem_on S f
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1498
  by standard (simp_all add: comp_fun_commute_on comp_fun_idem_on)
54867
c21a2465cac1 prefer ephemeral interpretation over interpretation in proof contexts;
haftmann
parents: 54611
diff changeset
  1499
35719
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1500
lemma insert_idem [simp]:
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1501
  assumes "insert x A \<subseteq> S" and "finite A"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1502
  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
  1503
proof -
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1504
  from fold_insert_idem assms
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1505
  have "fold f z (insert x A) = f x (fold f z A)" by simp
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1506
  with \<open>finite A\<close> 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
  1507
qed
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1508
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1509
end
99b6152aedf5 split off theory Big_Operators from theory Finite_Set
haftmann
parents: 35577
diff changeset
  1510
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1511
subsubsection \<open>\<^term>\<open>UNIV\<close> as the carrier set\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1512
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1513
locale folding =
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1514
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: "'b"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1515
  assumes comp_fun_commute: "f y \<circ> f x = f x \<circ> f y"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1516
begin
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1517
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1518
lemma (in -) folding_def': "folding f = folding_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1519
  unfolding folding_def folding_on_def by blast
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1520
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1521
text \<open>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1522
  Again, we abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1523
  result from instantiating the carrier set to \<^term>\<open>UNIV\<close>.
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1524
\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1525
sublocale folding_on UNIV f
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1526
  rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1527
       and "\<And>x. x \<in> UNIV \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1528
       and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1529
       and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1530
proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1531
  show "folding_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1532
    by standard (simp add: comp_fun_commute)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1533
qed simp_all
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1534
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1535
end
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1536
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1537
locale folding_idem = folding +
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1538
  assumes comp_fun_idem: "f x \<circ> f x = f x"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1539
begin
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1540
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1541
lemma (in -) folding_idem_def': "folding_idem f = folding_idem_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1542
  unfolding folding_idem_def folding_def' folding_idem_on_def
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1543
  unfolding folding_idem_axioms_def folding_idem_on_axioms_def
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1544
  by blast
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1545
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1546
text \<open>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1547
  Again, we abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1548
  result from instantiating the carrier set to \<^term>\<open>UNIV\<close>.
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1549
\<close>
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1550
sublocale folding_idem_on UNIV f
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1551
  rewrites "\<And>X. (X \<subseteq> UNIV) \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1552
       and "\<And>x. x \<in> UNIV \<equiv> True"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1553
       and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1554
       and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1555
proof -
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1556
  show "folding_idem_on UNIV f"
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1557
    by standard (simp add: comp_fun_idem)
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1558
qed simp_all
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1559
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1560
end
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1561
35817
d8b8527102f5 added locales folding_one_(idem); various streamlining and tuning
haftmann
parents: 35796
diff changeset
  1562
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1563
subsection \<open>Finite cardinality\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1564
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1565
text \<open>
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1566
  The traditional definition
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
  1567
  \<^prop>\<open>card A \<equiv> LEAST n. \<exists>f. A = {f i |i. i < n}\<close>
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1568
  is ugly to work with.
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69312
diff changeset
  1569
  But now that we have \<^const>\<open>fold\<close> things are easy:
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1570
\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1571
61890
f6ded81f5690 abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents: 61810
diff changeset
  1572
global_interpretation card: folding "\<lambda>_. Suc" 0
73832
9db620f007fa More general fold function for maps
nipkow
parents: 73620
diff changeset
  1573
  defines card = "folding_on.F (\<lambda>_. Suc) 0"
75669
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1574
  by standard (rule refl)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1575
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1576
lemma card_insert_disjoint: "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> card (insert x A) = Suc (card A)"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1577
  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
  1578
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1579
lemma card_insert_if: "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
  1580
  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
  1581
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1582
lemma card_ge_0_finite: "card A > 0 \<Longrightarrow> finite A"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1583
  by (rule ccontr) simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1584
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1585
lemma card_0_eq [simp]: "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1586
  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
  1587
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1588
lemma finite_UNIV_card_ge_0: "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1589
  by (rule ccontr) simp
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1590
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1591
lemma card_eq_0_iff: "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1592
  by auto
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1593
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1594
lemma card_range_greater_zero: "finite (range f) \<Longrightarrow> card (range f) > 0"
63365
5340fb6633d0 more theorems
haftmann
parents: 63099
diff changeset
  1595
  by (rule ccontr) (simp add: card_eq_0_iff)
5340fb6633d0 more theorems
haftmann
parents: 63099
diff changeset
  1596
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1597
lemma card_gt_0_iff: "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1598
  by (simp add: neq0_conv [symmetric] card_eq_0_iff)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1599
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1600
lemma card_Suc_Diff1:
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1601
  assumes "finite A" "x \<in> A" shows "Suc (card (A - {x})) = card A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1602
proof -
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1603
  have "Suc (card (A - {x})) = card (insert x (A - {x}))"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1604
    using assms by (simp add: card.insert_remove)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1605
  also have "... = card A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1606
    using assms by (simp add: card_insert_if)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1607
  finally show ?thesis .
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1608
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1609
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1610
lemma card_insert_le_m1:
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1611
  assumes "n > 0" "card y \<le> n - 1" shows  "card (insert x y) \<le> n"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1612
  using assms
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1613
  by (cases "finite y") (auto simp: card_insert_if)
60762
bf0c76ccee8d new material for multivariate analysis, etc.
paulson
parents: 60758
diff changeset
  1614
74223
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1615
lemma card_Diff_singleton:
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1616
  assumes "x \<in> A" shows "card (A - {x}) = card A - 1"
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1617
proof (cases "finite A")
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1618
  case True
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1619
  with assms show ?thesis
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1620
    by (simp add: card_Suc_Diff1 [symmetric])
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1621
qed auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1622
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1623
lemma card_Diff_singleton_if:
74223
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1624
  "card (A - {x}) = (if x \<in> A then card A - 1 else card A)"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1625
  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
  1626
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1627
lemma card_Diff_insert[simp]:
74223
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1628
  assumes "a \<in> A" and "a \<notin> B"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1629
  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
  1630
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1631
  have "A - insert a B = (A - B) - {a}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1632
    using assms by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1633
  then show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1634
    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
  1635
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1636
74223
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1637
lemma card_insert_le: "card A \<le> card (insert x A)"
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1638
proof (cases "finite A")
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1639
  case True
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1640
  then show ?thesis   by (simp add: card_insert_if)
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1641
qed auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1642
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1643
lemma card_Collect_less_nat[simp]: "card {i::nat. i < n} = n"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1644
  by (induct n) (simp_all add:less_Suc_eq Collect_disj_eq)
41987
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1645
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1646
lemma card_Collect_le_nat[simp]: "card {i::nat. i \<le> n} = Suc n"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1647
  using card_Collect_less_nat[of "Suc n"] by (simp add: less_Suc_eq_le)
41987
4ad8f1dc2e0b added lemmas
nipkow
parents: 41657
diff changeset
  1648
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1649
lemma card_mono:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1650
  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
  1651
  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
  1652
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1653
  from assms have "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1654
    by (auto intro: finite_subset)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1655
  then show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1656
    using assms
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1657
  proof (induct A arbitrary: B)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1658
    case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1659
    then show ?case by simp
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1660
  next
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1661
    case (insert x A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1662
    then have "x \<in> B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1663
      by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1664
    from insert have "A \<subseteq> B - {x}" and "finite (B - {x})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1665
      by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1666
    with insert.hyps have "card A \<le> card (B - {x})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1667
      by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1668
    with \<open>finite A\<close> \<open>x \<notin> A\<close> \<open>finite B\<close> \<open>x \<in> B\<close> show ?case
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1669
      by simp (simp only: card.remove)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1670
  qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1671
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1672
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1673
lemma card_seteq: 
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1674
  assumes "finite B" and A: "A \<subseteq> B" "card B \<le> card A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1675
  shows "A = B"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1676
  using assms
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1677
proof (induction arbitrary: A rule: finite_induct)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1678
  case (insert b B)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1679
  then have A: "finite A" "A - {b} \<subseteq> B" 
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1680
    by force+
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1681
  then have "card B \<le> card (A - {b})"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1682
    using insert by (auto simp add: card_Diff_singleton_if)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1683
  then have "A - {b} = B"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1684
    using A insert.IH by auto
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1685
  then show ?case 
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1686
    using insert.hyps insert.prems by auto
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1687
qed auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1688
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1689
lemma psubset_card_mono: "finite B \<Longrightarrow> A < B \<Longrightarrow> card A < card B"
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1690
  using card_seteq [of B A] by (auto simp add: psubset_eq)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1691
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1692
lemma card_Un_Int:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1693
  assumes "finite A" "finite B"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1694
  shows "card A + card B = card (A \<union> B) + card (A \<inter> B)"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1695
  using assms
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1696
proof (induct A)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1697
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1698
  then show ?case by simp
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1699
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1700
  case insert
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1701
  then show ?case
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1702
    by (auto simp add: insert_absorb Int_insert_left)
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 51487
diff changeset
  1703
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1704
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1705
lemma card_Un_disjoint: "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> card (A \<union> B) = card A + card B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1706
  using 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
  1707
72095
cfb6c22a5636 lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents: 71449
diff changeset
  1708
lemma card_Un_disjnt: "\<lbrakk>finite A; finite B; disjnt A B\<rbrakk> \<Longrightarrow> card (A \<union> B) = card A + card B"
cfb6c22a5636 lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents: 71449
diff changeset
  1709
  by (simp add: card_Un_disjoint disjnt_def)
cfb6c22a5636 lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents: 71449
diff changeset
  1710
59336
a95b6f608a73 added lemma
nipkow
parents: 58889
diff changeset
  1711
lemma card_Un_le: "card (A \<union> B) \<le> card A + card B"
70723
4e39d87c9737 imported new material mostly due to Sébastien Gouëzel
paulson <lp15@cam.ac.uk>
parents: 70178
diff changeset
  1712
proof (cases "finite A \<and> finite B")
4e39d87c9737 imported new material mostly due to Sébastien Gouëzel
paulson <lp15@cam.ac.uk>
parents: 70178
diff changeset
  1713
  case True
4e39d87c9737 imported new material mostly due to Sébastien Gouëzel
paulson <lp15@cam.ac.uk>
parents: 70178
diff changeset
  1714
  then show ?thesis
4e39d87c9737 imported new material mostly due to Sébastien Gouëzel
paulson <lp15@cam.ac.uk>
parents: 70178
diff changeset
  1715
    using le_iff_add card_Un_Int [of A B] by auto
4e39d87c9737 imported new material mostly due to Sébastien Gouëzel
paulson <lp15@cam.ac.uk>
parents: 70178
diff changeset
  1716
qed auto
59336
a95b6f608a73 added lemma
nipkow
parents: 58889
diff changeset
  1717
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1718
lemma card_Diff_subset:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1719
  assumes "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1720
    and "B \<subseteq> A"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1721
  shows "card (A - B) = card A - card B"
63915
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  1722
  using assms
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1723
proof (cases "finite A")
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1724
  case False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1725
  with assms show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1726
    by simp
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1727
next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1728
  case True
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1729
  with assms show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1730
    by (induct B arbitrary: A) simp_all
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1731
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1732
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1733
lemma card_Diff_subset_Int:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1734
  assumes "finite (A \<inter> B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1735
  shows "card (A - B) = card A - card (A \<inter> B)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1736
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1737
  have "A - B = A - A \<inter> B" by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1738
  with assms show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1739
    by (simp add: card_Diff_subset)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1740
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1741
40716
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1742
lemma diff_card_le_card_Diff:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1743
  assumes "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1744
  shows "card A - card B \<le> card (A - B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1745
proof -
40716
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1746
  have "card A - card B \<le> card A - card (A \<inter> B)"
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1747
    using card_mono[OF assms Int_lower2, of A] by arith
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1748
  also have "\<dots> = card (A - B)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1749
    using assms by (simp add: card_Diff_subset_Int)
40716
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1750
  finally show ?thesis .
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1751
qed
a92d744bca5f new lemma
nipkow
parents: 40703
diff changeset
  1752
69312
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1753
lemma card_le_sym_Diff:
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1754
  assumes "finite A" "finite B" "card A \<le> card B"
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1755
  shows "card(A - B) \<le> card(B - A)"
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1756
proof -
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1757
  have "card(A - B) = card A - card (A \<inter> B)" using assms(1,2) by(simp add: card_Diff_subset_Int)
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1758
  also have "\<dots> \<le> card B - card (A \<inter> B)" using assms(3) by linarith
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1759
  also have "\<dots> = card(B - A)" using assms(1,2) by(simp add: card_Diff_subset_Int Int_commute)
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1760
  finally show ?thesis .
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1761
qed
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1762
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1763
lemma card_less_sym_Diff:
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1764
  assumes "finite A" "finite B" "card A < card B"
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1765
  shows "card(A - B) < card(B - A)"
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1766
proof -
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1767
  have "card(A - B) = card A - card (A \<inter> B)" using assms(1,2) by(simp add: card_Diff_subset_Int)
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1768
  also have "\<dots> < card B - card (A \<inter> B)" using assms(1,3) by (simp add: card_mono diff_less_mono)
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1769
  also have "\<dots> = card(B - A)" using assms(1,2) by(simp add: card_Diff_subset_Int Int_commute)
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1770
  finally show ?thesis .
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1771
qed
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  1772
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1773
lemma card_Diff1_less_iff: "card (A - {x}) < card A \<longleftrightarrow> finite A \<and> x \<in> A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1774
proof (cases "finite A \<and> x \<in> A")
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1775
  case True
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1776
  then show ?thesis
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1777
    by (auto simp: card_gt_0_iff intro: diff_less)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1778
qed auto
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1779
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1780
lemma card_Diff1_less: "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> card (A - {x}) < card A"
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1781
  unfolding card_Diff1_less_iff by auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1782
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1783
lemma card_Diff2_less:
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1784
  assumes "finite A" "x \<in> A" "y \<in> A" shows "card (A - {x} - {y}) < card A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1785
proof (cases "x = y")
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1786
  case True
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1787
  with assms show ?thesis
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1788
    by (simp add: card_Diff1_less del: card_Diff_insert)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1789
next
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1790
  case False
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1791
  then have "card (A - {x} - {y}) < card (A - {x})" "card (A - {x}) < card A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1792
    using assms by (intro card_Diff1_less; simp)+
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1793
  then show ?thesis
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1794
    by (blast intro: less_trans)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  1795
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1796
74223
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1797
lemma card_Diff1_le: "card (A - {x}) \<le> card A"
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1798
proof (cases "finite A")
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1799
  case True
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1800
  then show ?thesis  
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1801
    by (cases "x \<in> A") (simp_all add: card_Diff1_less less_imp_le)
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1802
qed auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1803
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1804
lemma card_psubset: "finite B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> card A < card B \<Longrightarrow> A < B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1805
  by (erule psubsetI) blast
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1806
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1807
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
  1808
  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
  1809
    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
  1810
    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
  1811
  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
  1812
  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
  1813
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
  1814
  case empty
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1815
  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
  1816
next
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1817
  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
  1818
  then show ?case
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1819
  proof (induct rule: finite_induct [OF insert.prems(1)])
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1820
    case 1
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1821
    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
  1822
  next
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1823
    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
  1824
    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
  1825
      by simp
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1826
    from "2.prems"(3) [OF "2.hyps"(1) cst]
75669
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1827
    obtain f where *: "f ` s \<subseteq> t" "inj_on f s"
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1828
      by blast
75669
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1829
    let ?g = "(\<lambda>a. if a = x then y else f a)"
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1830
    have "?g ` insert x s \<subseteq> insert y t \<and> inj_on ?g (insert x s)"
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1831
      using * "2.prems"(2) "2.hyps"(2) unfolding inj_on_def by auto
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  1832
    then show ?case by (rule exI[where ?x="?g"])
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1833
  qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1834
qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1835
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1836
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
  1837
  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
  1838
    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
  1839
    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
  1840
  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
  1841
proof -
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1842
  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
  1843
    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
  1844
  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
  1845
    by auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1846
  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
  1847
    by blast
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1848
  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
  1849
    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
  1850
  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
  1851
    by arith
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1852
  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
  1853
    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
  1854
  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
  1855
    by blast
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1856
qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1857
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1858
lemma insert_partition:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1859
  "x \<notin> F \<Longrightarrow> \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<Longrightarrow> x \<inter> \<Union>F = {}"
74223
527088d4a89b strengthened a few lemmas about finite sets and added a code equation for complex_of_real
paulson <lp15@cam.ac.uk>
parents: 73832
diff changeset
  1860
  by auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1861
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1862
lemma finite_psubset_induct [consumes 1, case_names psubset]:
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1863
  assumes finite: "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1864
    and major: "\<And>A. finite A \<Longrightarrow> (\<And>B. B \<subset> A \<Longrightarrow> P B) \<Longrightarrow> P 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
  1865
  shows "P A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1866
  using finite
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
  1867
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
  1868
  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
  1869
  have fin: "finite A" by fact
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1870
  have ih: "card B < card A \<Longrightarrow> finite B \<Longrightarrow> P B" for B by fact
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1871
  have "P B" if "B \<subset> A" for B
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1872
  proof -
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1873
    from that have "card B < card A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1874
      using psubset_card_mono fin by blast
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
  1875
    moreover
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1876
    from that have "B \<subseteq> A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1877
      by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1878
    then have "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1879
      using fin finite_subset by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1880
    ultimately show ?thesis using ih by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1881
  qed
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
  1882
  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
  1883
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1884
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1885
lemma finite_induct_select [consumes 1, case_names empty select]:
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1886
  assumes "finite S"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1887
    and "P {}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1888
    and select: "\<And>T. T \<subset> S \<Longrightarrow> P T \<Longrightarrow> \<exists>s\<in>S - T. P (insert s T)"
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1889
  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
  1890
proof -
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1891
  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
  1892
  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
  1893
  proof (induct rule: dec_induct)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1894
    case base with \<open>P {}\<close>
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1895
    show ?case
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1896
      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
  1897
  next
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1898
    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
  1899
    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
  1900
      by auto
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1901
    with \<open>n < card S\<close> have "T \<subset> S" "P T"
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1902
      by auto
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1903
    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
  1904
      by auto
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1905
    with step(2) T \<open>finite S\<close> show ?case
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1906
      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
  1907
  qed
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  1908
  with \<open>finite S\<close> show "P S"
54413
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1909
    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
  1910
qed
88a036a95967 add finite_select_induct; move generic lemmas from MV_Analysis/Linear_Algebra to the HOL image
hoelzl
parents: 54148
diff changeset
  1911
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1912
lemma remove_induct [case_names empty infinite remove]:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1913
  assumes empty: "P ({} :: 'a set)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1914
    and infinite: "\<not> finite B \<Longrightarrow> P B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1915
    and remove: "\<And>A. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P (A - {x})) \<Longrightarrow> P A"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1916
  shows "P B"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1917
proof (cases "finite B")
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1918
  case False
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1919
  then show ?thesis by (rule infinite)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1920
next
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1921
  case True
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1922
  define A where "A = B"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1923
  with True have "finite A" "A \<subseteq> B"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1924
    by simp_all
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1925
  then show "P A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1926
  proof (induct "card A" arbitrary: A)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1927
    case 0
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1928
    then have "A = {}" by auto
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1929
    with empty show ?case by simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1930
  next
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1931
    case (Suc n A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1932
    from \<open>A \<subseteq> B\<close> and \<open>finite B\<close> have "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1933
      by (rule finite_subset)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1934
    moreover from Suc.hyps have "A \<noteq> {}" by auto
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1935
    moreover note \<open>A \<subseteq> B\<close>
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1936
    moreover have "P (A - {x})" if x: "x \<in> A" for x
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1937
      using x Suc.prems \<open>Suc n = card A\<close> by (intro Suc) auto
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1938
    ultimately show ?case by (rule remove)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1939
  qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1940
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1941
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1942
lemma finite_remove_induct [consumes 1, case_names empty remove]:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1943
  fixes P :: "'a set \<Rightarrow> bool"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1944
  assumes "finite B"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1945
    and "P {}"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1946
    and "\<And>A. finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P (A - {x})) \<Longrightarrow> P A"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1947
  defines "B' \<equiv> B"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1948
  shows "P B'"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1949
  by (induct B' rule: remove_induct) (simp_all add: assms)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1950
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  1951
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1952
text \<open>Main cardinality theorem.\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1953
lemma card_partition [rule_format]:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1954
  "finite C \<Longrightarrow> finite (\<Union>C) \<Longrightarrow> (\<forall>c\<in>C. card c = k) \<Longrightarrow>
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1955
    (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}) \<Longrightarrow>
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1956
    k * card C = card (\<Union>C)"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1957
proof (induct rule: finite_induct)
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1958
  case empty
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1959
  then show ?case by simp
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1960
next
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1961
  case (insert x F)
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1962
  then show ?case
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1963
    by (simp add: card_Un_disjoint insert_partition finite_subset [of _ "\<Union>(insert _ _)"])
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  1964
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1965
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1966
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
  1967
  assumes fin: "finite (UNIV :: 'a set)"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1968
    and card: "card A = card (UNIV :: 'a set)"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1969
  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
  1970
proof
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1971
  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
  1972
  show "UNIV \<subseteq> A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1973
  proof
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1974
    show "x \<in> A" for x
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1975
    proof (rule ccontr)
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1976
      assume "x \<notin> A"
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1977
      then have "A \<subset> UNIV" by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1978
      with fin have "card A < card (UNIV :: 'a set)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1979
        by (fact psubset_card_mono)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1980
      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
  1981
    qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1982
  qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1983
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1984
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1985
text \<open>The form of a finite set of given cardinality\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1986
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1987
lemma card_eq_SucD:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1988
  assumes "card A = Suc k"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1989
  shows "\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {})"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1990
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1991
  have fin: "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1992
    using assms by (auto intro: ccontr)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1993
  moreover have "card A \<noteq> 0"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1994
    using assms by auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1995
  ultimately obtain b where b: "b \<in> A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1996
    by auto
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1997
  show ?thesis
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  1998
  proof (intro exI conjI)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  1999
    show "A = insert b (A - {b})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2000
      using b by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2001
    show "b \<notin> A - {b}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2002
      by blast
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2003
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2004
      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
  2005
  qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2006
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2007
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2008
lemma card_Suc_eq:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2009
  "card A = Suc k \<longleftrightarrow>
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2010
    (\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> (k = 0 \<longrightarrow> B = {}))"
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2011
  by (auto simp: card_insert_if card_gt_0_iff elim!: card_eq_SucD)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2012
73620
58aed6f71f90 A nice cardinality lemma
paulson <lp15@cam.ac.uk>
parents: 73555
diff changeset
  2013
lemma card_Suc_eq_finite:
58aed6f71f90 A nice cardinality lemma
paulson <lp15@cam.ac.uk>
parents: 73555
diff changeset
  2014
  "card A = Suc k \<longleftrightarrow> (\<exists>b B. A = insert b B \<and> b \<notin> B \<and> card B = k \<and> finite B)"
58aed6f71f90 A nice cardinality lemma
paulson <lp15@cam.ac.uk>
parents: 73555
diff changeset
  2015
  unfolding card_Suc_eq using card_gt_0_iff by fastforce
58aed6f71f90 A nice cardinality lemma
paulson <lp15@cam.ac.uk>
parents: 73555
diff changeset
  2016
61518
ff12606337e9 new lemmas about topology, etc., for Cauchy integral formula
paulson
parents: 61169
diff changeset
  2017
lemma card_1_singletonE:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2018
  assumes "card A = 1"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2019
  obtains x where "A = {x}"
61518
ff12606337e9 new lemmas about topology, etc., for Cauchy integral formula
paulson
parents: 61169
diff changeset
  2020
  using assms by (auto simp: card_Suc_eq)
ff12606337e9 new lemmas about topology, etc., for Cauchy integral formula
paulson
parents: 61169
diff changeset
  2021
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  2022
lemma is_singleton_altdef: "is_singleton A \<longleftrightarrow> card A = 1"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  2023
  unfolding is_singleton_def
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  2024
  by (auto elim!: card_1_singletonE is_singletonE simp del: One_nat_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 63040
diff changeset
  2025
71258
d67924987c34 a few new and tidier proofs (mostly about finite sets)
paulson <lp15@cam.ac.uk>
parents: 70723
diff changeset
  2026
lemma card_1_singleton_iff: "card A = Suc 0 \<longleftrightarrow> (\<exists>x. A = {x})"
d67924987c34 a few new and tidier proofs (mostly about finite sets)
paulson <lp15@cam.ac.uk>
parents: 70723
diff changeset
  2027
  by (simp add: card_Suc_eq)
d67924987c34 a few new and tidier proofs (mostly about finite sets)
paulson <lp15@cam.ac.uk>
parents: 70723
diff changeset
  2028
69312
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2029
lemma card_le_Suc0_iff_eq:
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2030
  assumes "finite A"
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2031
  shows "card A \<le> Suc 0 \<longleftrightarrow> (\<forall>a1 \<in> A. \<forall>a2 \<in> A. a1 = a2)" (is "?C = ?A")
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2032
proof
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2033
  assume ?C thus ?A using assms by (auto simp: le_Suc_eq dest: card_eq_SucD)
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2034
next
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2035
  assume ?A
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2036
  show ?C
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2037
  proof cases
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2038
    assume "A = {}" thus ?C using \<open>?A\<close> by simp
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2039
  next
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2040
    assume "A \<noteq> {}"
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2041
    then obtain a where "A = {a}" using \<open>?A\<close> by blast
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2042
    thus ?C by simp
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2043
  qed
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2044
qed
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2045
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2046
lemma card_le_Suc_iff:
69312
e0f68a507683 added and tuned lemmas
nipkow
parents: 69286
diff changeset
  2047
  "Suc n \<le> card A = (\<exists>a B. A = insert a B \<and> a \<notin> B \<and> n \<le> card B \<and> finite B)"
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2048
proof (cases "finite A")
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2049
  case True
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2050
  then show ?thesis
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2051
    by (fastforce simp: card_Suc_eq less_eq_nat.simps split: nat.splits)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2052
qed auto
44744
bdf8eb8f126b added new lemmas
nipkow
parents: 43991
diff changeset
  2053
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2054
lemma finite_fun_UNIVD2:
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2055
  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
  2056
  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
  2057
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2058
  from fin have "finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))" for arbitrary
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  2059
    by (rule finite_imageI)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2060
  moreover have "UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)" for arbitrary
46146
6baea4fca6bd incorporated various theorems from theory More_Set into corpus
haftmann
parents: 46033
diff changeset
  2061
    by (rule UNIV_eq_I) auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2062
  ultimately show "finite (UNIV :: 'b set)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2063
    by simp
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2064
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2065
48063
f02b4302d5dd remove duplicate lemma card_unit in favor of Finite_Set.card_UNIV_unit
huffman
parents: 47221
diff changeset
  2066
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
  2067
  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
  2068
57447
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2069
lemma infinite_arbitrarily_large:
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2070
  assumes "\<not> finite A"
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2071
  shows "\<exists>B. finite B \<and> card B = n \<and> B \<subseteq> A"
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2072
proof (induction n)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2073
  case 0
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2074
  show ?case by (intro exI[of _ "{}"]) auto
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2075
next
57447
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2076
  case (Suc n)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2077
  then obtain B where B: "finite B \<and> card B = n \<and> B \<subseteq> A" ..
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  2078
  with \<open>\<not> finite A\<close> have "A \<noteq> B" by auto
57447
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2079
  with B have "B \<subset> A" by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2080
  then have "\<exists>x. x \<in> A - B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2081
    by (elim psubset_imp_ex_mem)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2082
  then obtain x where x: "x \<in> A - B" ..
57447
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2083
  with B have "finite (insert x B) \<and> card (insert x B) = Suc n \<and> insert x B \<subseteq> A"
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2084
    by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2085
  then show "\<exists>B. finite B \<and> card B = Suc n \<and> B \<subseteq> A" ..
57447
87429bdecad5 import more stuff from the CLT proof; base the lborel measure on interval_measure; remove lebesgue measure
hoelzl
parents: 57025
diff changeset
  2086
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2087
67457
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2088
text \<open>Sometimes, to prove that a set is finite, it is convenient to work with finite subsets
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2089
and to show that their cardinalities are uniformly bounded. This possibility is formalized in
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2090
the next criterion.\<close>
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2091
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2092
lemma finite_if_finite_subsets_card_bdd:
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2093
  assumes "\<And>G. G \<subseteq> F \<Longrightarrow> finite G \<Longrightarrow> card G \<le> C"
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2094
  shows "finite F \<and> card F \<le> C"
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2095
proof (cases "finite F")
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2096
  case False
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2097
  obtain n::nat where n: "n > max C 0" by auto
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2098
  obtain G where G: "G \<subseteq> F" "card G = n" using infinite_arbitrarily_large[OF False] by auto
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2099
  hence "finite G" using \<open>n > max C 0\<close> using card.infinite gr_implies_not0 by blast
67457
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2100
  hence False using assms G n not_less by auto
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2101
  thus ?thesis ..
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2102
next
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2103
  case True thus ?thesis using assms[of F] by auto
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2104
qed
4b921bb461f6 moved from AFP/Gromov
nipkow
parents: 67443
diff changeset
  2105
75668
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2106
lemma obtain_subset_with_card_n:
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2107
  assumes "n \<le> card S"
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2108
  obtains T where "T \<subseteq> S" "card T = n" "finite T"
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2109
proof -
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2110
  obtain n' where "card S = n + n'"
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2111
    using le_Suc_ex[OF assms] by blast
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2112
  with that show thesis
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2113
  proof (induct n' arbitrary: S)
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2114
    case 0 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2115
    thus ?case by (cases "finite S") auto
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2116
  next
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2117
    case Suc 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2118
    thus ?case by (auto simp add: card_Suc_eq)
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2119
  qed
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2120
qed
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2121
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2122
lemma exists_subset_between: 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2123
  assumes 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2124
    "card A \<le> n" 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2125
    "n \<le> card C"
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2126
    "A \<subseteq> C"
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2127
    "finite C"
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2128
  shows "\<exists>B. A \<subseteq> B \<and> B \<subseteq> C \<and> card B = n" 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2129
  using assms 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2130
proof (induct n arbitrary: A C)
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2131
  case 0
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2132
  thus ?case using finite_subset[of A C] by (intro exI[of _ "{}"], auto)
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2133
next
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2134
  case (Suc n A C)
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2135
  show ?case
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2136
  proof (cases "A = {}")
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2137
    case True
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2138
    from obtain_subset_with_card_n[OF Suc(3)]
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2139
    obtain B where "B \<subseteq> C" "card B = Suc n" by blast
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2140
    thus ?thesis unfolding True by blast
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2141
  next
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2142
    case False
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2143
    then obtain a where a: "a \<in> A" by auto
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2144
    let ?A = "A - {a}" 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2145
    let ?C = "C - {a}" 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2146
    have 1: "card ?A \<le> n" using Suc(2-) a 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2147
      using finite_subset by fastforce 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2148
    have 2: "card ?C \<ge> n" using Suc(2-) a by auto
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2149
    from Suc(1)[OF 1 2 _ finite_subset[OF _ Suc(5)]] Suc(2-)
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2150
    obtain B where "?A \<subseteq> B" "B \<subseteq> ?C" "card B = n" by blast
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2151
    thus ?thesis using a Suc(2-) 
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2152
      by (intro exI[of _ "insert a B"], auto intro!: card_insert_disjoint finite_subset[of B C])
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2153
  qed
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2154
qed
b87b14e885af moved lemma fromm AFP
nipkow
parents: 74985
diff changeset
  2155
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2156
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  2157
subsubsection \<open>Cardinality of image\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2158
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2159
lemma card_image_le: "finite A \<Longrightarrow> card (f ` A) \<le> card A"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2160
  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
  2161
63915
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  2162
lemma card_image: "inj_on f A \<Longrightarrow> card (f ` A) = card A"
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  2163
proof (induct A rule: infinite_finite_induct)
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  2164
  case (infinite A)
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  2165
  then have "\<not> finite (f ` A)" by (auto dest: finite_imageD)
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  2166
  with infinite show ?case by simp
bab633745c7f tuned proofs;
wenzelm
parents: 63648
diff changeset
  2167
qed simp_all
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2168
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2169
lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2170
  by (auto simp: card_image bij_betw_def)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2171
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2172
lemma endo_inj_surj: "finite A \<Longrightarrow> f ` A \<subseteq> A \<Longrightarrow> inj_on f A \<Longrightarrow> f ` A = A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2173
  by (simp add: card_seteq card_image)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2174
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2175
lemma eq_card_imp_inj_on:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2176
  assumes "finite A" "card(f ` A) = card A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2177
  shows "inj_on f A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2178
  using assms
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2179
proof (induct rule:finite_induct)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2180
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2181
  show ?case by simp
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2182
next
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2183
  case (insert x A)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2184
  then show ?case
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2185
    using card_image_le [of A f] by (simp add: card_insert_if split: if_splits)
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2186
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2187
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2188
lemma inj_on_iff_eq_card: "finite A \<Longrightarrow> inj_on f A \<longleftrightarrow> card (f ` A) = card A"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2189
  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
  2190
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2191
lemma card_inj_on_le:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2192
  assumes "inj_on f A" "f ` A \<subseteq> B" "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2193
  shows "card A \<le> card B"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2194
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2195
  have "finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2196
    using assms by (blast intro: finite_imageD dest: finite_subset)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2197
  then show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2198
    using assms by (force intro: card_mono simp: card_image [symmetric])
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2199
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2200
69235
0e156963b636 simplified proof, moved lemma, added lemma
nipkow
parents: 68975
diff changeset
  2201
lemma inj_on_iff_card_le:
0e156963b636 simplified proof, moved lemma, added lemma
nipkow
parents: 68975
diff changeset
  2202
  "\<lbrakk> finite A; finite B \<rbrakk> \<Longrightarrow> (\<exists>f. inj_on f A \<and> f ` A \<le> B) = (card A \<le> card B)"
0e156963b636 simplified proof, moved lemma, added lemma
nipkow
parents: 68975
diff changeset
  2203
using card_inj_on_le[of _ A B] card_le_inj[of A B] by blast
0e156963b636 simplified proof, moved lemma, added lemma
nipkow
parents: 68975
diff changeset
  2204
59504
8c6747dba731 New lemmas and a bit of tidying up.
paulson <lp15@cam.ac.uk>
parents: 59336
diff changeset
  2205
lemma surj_card_le: "finite A \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> card B \<le> card A"
8c6747dba731 New lemmas and a bit of tidying up.
paulson <lp15@cam.ac.uk>
parents: 59336
diff changeset
  2206
  by (blast intro: card_image_le card_mono le_trans)
8c6747dba731 New lemmas and a bit of tidying up.
paulson <lp15@cam.ac.uk>
parents: 59336
diff changeset
  2207
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2208
lemma card_bij_eq:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2209
  "inj_on f A \<Longrightarrow> f ` A \<subseteq> B \<Longrightarrow> inj_on g B \<Longrightarrow> g ` B \<subseteq> A \<Longrightarrow> finite A \<Longrightarrow> finite B
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2210
    \<Longrightarrow> card A = card B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2211
  by (auto intro: le_antisym card_inj_on_le)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2212
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2213
lemma bij_betw_finite: "bij_betw f A B \<Longrightarrow> finite A \<longleftrightarrow> finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2214
  unfolding bij_betw_def 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
  2215
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2216
lemma inj_on_finite: "inj_on f A \<Longrightarrow> f ` A \<le> B \<Longrightarrow> finite B \<Longrightarrow> finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2217
  using finite_imageD finite_subset by blast
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2218
74985
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2219
lemma card_vimage_inj_on_le:
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2220
  assumes "inj_on f D" "finite A"
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2221
  shows "card (f-`A \<inter> D) \<le> card A"
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2222
proof (rule card_inj_on_le)
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2223
  show "inj_on f (f -` A \<inter> D)"
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2224
    by (blast intro: assms inj_on_subset)
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2225
qed (use assms in auto)
ac3901e4e0a9 A new lemma about inverse image
paulson <lp15@cam.ac.uk>
parents: 74438
diff changeset
  2226
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2227
lemma card_vimage_inj: "inj f \<Longrightarrow> A \<subseteq> range f \<Longrightarrow> card (f -` A) = card A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2228
  by (auto 4 3 simp: subset_image_iff inj_vimage_image_eq
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2229
      intro: card_image[symmetric, OF subset_inj_on])
55020
96b05fd2aee4 dissolved 'Fun_More_FP' (a BNF dependency)
blanchet
parents: 54870
diff changeset
  2230
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  2231
subsubsection \<open>Pigeonhole Principles\<close>
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2232
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2233
lemma pigeonhole: "card A > card (f ` A) \<Longrightarrow> \<not> inj_on f A "
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2234
  by (auto dest: card_image less_irrefl_nat)
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2235
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2236
lemma pigeonhole_infinite:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2237
  assumes "\<not> finite A" and "finite (f`A)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2238
  shows "\<exists>a0\<in>A. \<not> finite {a\<in>A. f a = f a0}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2239
  using assms(2,1)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2240
proof (induct "f`A" arbitrary: A rule: finite_induct)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2241
  case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2242
  then show ?case by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2243
next
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2244
  case (insert b F)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2245
  show ?case
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2246
  proof (cases "finite {a\<in>A. f a = b}")
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2247
    case True
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2248
    with \<open>\<not> finite A\<close> have "\<not> finite (A - {a\<in>A. f a = b})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2249
      by simp
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2250
    also have "A - {a\<in>A. f a = b} = {a\<in>A. f a \<noteq> b}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2251
      by blast
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2252
    finally have "\<not> finite {a\<in>A. f a \<noteq> b}" .
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2253
    from insert(3)[OF _ this] insert(2,4) show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2254
      by simp (blast intro: rev_finite_subset)
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2255
  next
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2256
    case False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2257
    then have "{a \<in> A. f a = b} \<noteq> {}" by force
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2258
    with False show ?thesis by blast
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2259
  qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2260
qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2261
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2262
lemma pigeonhole_infinite_rel:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2263
  assumes "\<not> finite A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2264
    and "finite B"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2265
    and "\<forall>a\<in>A. \<exists>b\<in>B. R a b"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2266
  shows "\<exists>b\<in>B. \<not> finite {a:A. R a b}"
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2267
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2268
  let ?F = "\<lambda>a. {b\<in>B. R a b}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2269
  from finite_Pow_iff[THEN iffD2, OF \<open>finite B\<close>] have "finite (?F ` A)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2270
    by (blast intro: rev_finite_subset)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2271
  from pigeonhole_infinite [where f = ?F, OF assms(1) this]
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2272
  obtain a0 where "a0 \<in> A" and infinite: "\<not> finite {a\<in>A. ?F a = ?F a0}" ..
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2273
  obtain b0 where "b0 \<in> B" and "R a0 b0"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2274
    using \<open>a0 \<in> A\<close> assms(3) by blast
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2275
  have "finite {a\<in>A. ?F a = ?F a0}" if "finite {a\<in>A. R a b0}"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2276
    using \<open>b0 \<in> B\<close> \<open>R a0 b0\<close> that by (blast intro: rev_finite_subset)
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2277
  with infinite \<open>b0 \<in> B\<close> show ?thesis
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2278
    by blast
37466
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2279
qed
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2280
87bf104920f2 added pigeonhole lemmas
nipkow
parents: 36637
diff changeset
  2281
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  2282
subsubsection \<open>Cardinality of sums\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2283
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2284
lemma card_Plus:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2285
  assumes "finite A" "finite B"
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2286
  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
  2287
proof -
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2288
  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
  2289
  with assms show ?thesis
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2290
    by (simp add: Plus_def card_Un_disjoint card_image)
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2291
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2292
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2293
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
  2294
  "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
  2295
  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
  2296
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2297
text \<open>Relates to equivalence classes.  Based on a theorem of F. Kammüller.\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2298
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2299
lemma dvd_partition:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2300
  assumes f: "finite (\<Union>C)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2301
    and "\<forall>c\<in>C. k dvd card c" "\<forall>c1\<in>C. \<forall>c2\<in>C. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {}"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2302
  shows "k dvd card (\<Union>C)"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2303
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2304
  have "finite C"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2305
    by (rule finite_UnionD [OF f])
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2306
  then show ?thesis
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2307
    using assms
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2308
  proof (induct rule: finite_induct)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2309
    case empty
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2310
    show ?case by simp
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2311
  next
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2312
    case (insert c C)
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2313
    then have "c \<inter> \<Union>C = {}"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2314
      by auto
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2315
    with insert show ?case
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2316
      by (simp add: card_Un_disjoint)
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2317
  qed
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2318
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2319
72384
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2320
subsubsection \<open>Finite orders\<close>
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2321
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2322
context order
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2323
begin
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2324
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2325
lemma finite_has_maximal:
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2326
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<exists> m \<in> A. \<forall> b \<in> A. m \<le> b \<longrightarrow> m = b"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2327
proof (induction rule: finite_psubset_induct)
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2328
  case (psubset A)
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2329
  from \<open>A \<noteq> {}\<close> obtain a where "a \<in> A" by auto
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2330
  let ?B = "{b \<in> A. a < b}"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2331
  show ?case
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2332
  proof cases
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2333
    assume "?B = {}"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2334
    hence "\<forall> b \<in> A. a \<le> b \<longrightarrow> a = b" using le_neq_trans by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2335
    thus ?thesis using \<open>a \<in> A\<close> by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2336
  next
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2337
    assume "?B \<noteq> {}"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2338
    have "a \<notin> ?B" by auto
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2339
    hence "?B \<subset> A" using \<open>a \<in> A\<close> by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2340
    from psubset.IH[OF this \<open>?B \<noteq> {}\<close>] show ?thesis using order.strict_trans2 by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2341
  qed
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2342
qed
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2343
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2344
lemma finite_has_maximal2:
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2345
  "\<lbrakk> finite A; a \<in> A \<rbrakk> \<Longrightarrow> \<exists> m \<in> A. a \<le> m \<and> (\<forall> b \<in> A. m \<le> b \<longrightarrow> m = b)"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2346
using finite_has_maximal[of "{b \<in> A. a \<le> b}"] by fastforce
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2347
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2348
lemma finite_has_minimal:
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2349
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<exists> m \<in> A. \<forall> b \<in> A. b \<le> m \<longrightarrow> m = b"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2350
proof (induction rule: finite_psubset_induct)
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2351
  case (psubset A)
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2352
  from \<open>A \<noteq> {}\<close> obtain a where "a \<in> A" by auto
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2353
  let ?B = "{b \<in> A. b < a}"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2354
  show ?case
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2355
  proof cases
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2356
    assume "?B = {}"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2357
    hence "\<forall> b \<in> A. b \<le> a \<longrightarrow> a = b" using le_neq_trans by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2358
    thus ?thesis using \<open>a \<in> A\<close> by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2359
  next
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2360
    assume "?B \<noteq> {}"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2361
    have "a \<notin> ?B" by auto
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2362
    hence "?B \<subset> A" using \<open>a \<in> A\<close> by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2363
    from psubset.IH[OF this \<open>?B \<noteq> {}\<close>] show ?thesis using order.strict_trans1 by blast
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2364
  qed
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2365
qed
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2366
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2367
lemma finite_has_minimal2:
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2368
  "\<lbrakk> finite A; a \<in> A \<rbrakk> \<Longrightarrow> \<exists> m \<in> A. m \<le> a \<and> (\<forall> b \<in> A. b \<le> m \<longrightarrow> m = b)"
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2369
using finite_has_minimal[of "{b \<in> A. b \<le> a}"] by fastforce
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2370
b037517c815b added lemmas; internalized defn in class
nipkow
parents: 72302
diff changeset
  2371
end
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2372
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60595
diff changeset
  2373
subsubsection \<open>Relating injectivity and surjectivity\<close>
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2374
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2375
lemma finite_surj_inj:
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2376
  assumes "finite A" "A \<subseteq> f ` A"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2377
  shows "inj_on f A"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2378
proof -
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2379
  have "f ` A = A"
54570
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2380
    by (rule card_seteq [THEN sym]) (auto simp add: assms card_image_le)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2381
  then show ?thesis using assms
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2382
    by (simp add: eq_card_imp_inj_on)
002b8729f228 polished some ancient proofs
paulson
parents: 54413
diff changeset
  2383
qed
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2384
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2385
lemma finite_UNIV_surj_inj: "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2386
  for f :: "'a \<Rightarrow> 'a"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2387
  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
  2388
63612
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2389
lemma finite_UNIV_inj_surj: "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
7195acc2fe93 misc tuning and modernization;
wenzelm
parents: 63561
diff changeset
  2390
  for f :: "'a \<Rightarrow> 'a"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2391
  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
  2392
70019
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2393
lemma surjective_iff_injective_gen:
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2394
  assumes fS: "finite S"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2395
    and fT: "finite T"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2396
    and c: "card S = card T"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2397
    and ST: "f ` S \<subseteq> T"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2398
  shows "(\<forall>y \<in> T. \<exists>x \<in> S. f x = y) \<longleftrightarrow> inj_on f S"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2399
  (is "?lhs \<longleftrightarrow> ?rhs")
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2400
proof
70019
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2401
  assume h: "?lhs"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2402
  {
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2403
    fix x y
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2404
    assume x: "x \<in> S"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2405
    assume y: "y \<in> S"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2406
    assume f: "f x = f y"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2407
    from x fS have S0: "card S \<noteq> 0"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2408
      by auto
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2409
    have "x = y"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2410
    proof (rule ccontr)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2411
      assume xy: "\<not> ?thesis"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2412
      have th: "card S \<le> card (f ` (S - {y}))"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2413
        unfolding c
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2414
      proof (rule card_mono)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2415
        show "finite (f ` (S - {y}))"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2416
          by (simp add: fS)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2417
        have "\<lbrakk>x \<noteq> y; x \<in> S; z \<in> S; f x = f y\<rbrakk>
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2418
         \<Longrightarrow> \<exists>x \<in> S. x \<noteq> y \<and> f z = f x" for z
75669
43f5dfb7fa35 tuned (some HOL lints, by Yecine Megdiche);
Fabian Huch <huch@in.tum.de>
parents: 75668
diff changeset
  2419
          by (cases "z = y \<longrightarrow> z = x") auto
70019
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2420
        then show "T \<subseteq> f ` (S - {y})"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2421
          using h xy x y f by fastforce
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2422
      qed
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2423
      also have " \<dots> \<le> card (S - {y})"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2424
        by (simp add: card_image_le fS)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2425
      also have "\<dots> \<le> card S - 1" using y fS by simp
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2426
      finally show False using S0 by arith
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2427
    qed
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2428
  }
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2429
  then show ?rhs
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2430
    unfolding inj_on_def by blast
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2431
next
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2432
  assume h: ?rhs
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2433
  have "f ` S = T"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2434
    by (simp add: ST c card_image card_subset_eq fT h)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2435
  then show ?lhs by blast
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2436
qed
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2437
49758
718f10c8bbfc use Set.filter instead of Finite_Set.filter, which is removed then
kuncar
parents: 49757
diff changeset
  2438
hide_const (open) Finite_Set.fold
46033
6fc579c917b8 qualified Finite_Set.fold
haftmann
parents: 45962
diff changeset
  2439
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2440
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2441
subsection \<open>Infinite Sets\<close>
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2442
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2443
text \<open>
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2444
  Some elementary facts about infinite sets, mostly by Stephan Merz.
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2445
  Beware! Because "infinite" merely abbreviates a negation, these
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2446
  lemmas may not work well with \<open>blast\<close>.
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2447
\<close>
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2448
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2449
abbreviation infinite :: "'a set \<Rightarrow> bool"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2450
  where "infinite S \<equiv> \<not> finite S"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2451
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2452
text \<open>
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2453
  Infinite sets are non-empty, and if we remove some elements from an
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2454
  infinite set, the result is still infinite.
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2455
\<close>
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2456
70019
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2457
lemma infinite_UNIV_nat [iff]: "infinite (UNIV :: nat set)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2458
proof
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2459
  assume "finite (UNIV :: nat set)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2460
  with finite_UNIV_inj_surj [of Suc] show False
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2461
    by simp (blast dest: Suc_neq_Zero surjD)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2462
qed
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2463
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2464
lemma infinite_UNIV_char_0: "infinite (UNIV :: 'a::semiring_char_0 set)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2465
proof
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2466
  assume "finite (UNIV :: 'a set)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2467
  with subset_UNIV have "finite (range of_nat :: 'a set)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2468
    by (rule finite_subset)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2469
  moreover have "inj (of_nat :: nat \<Rightarrow> 'a)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2470
    by (simp add: inj_on_def)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2471
  ultimately have "finite (UNIV :: nat set)"
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2472
    by (rule finite_imageD)
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2473
  then show False
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2474
    by simp
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2475
qed
095dce9892e8 A few results in Algebra, and bits for Analysis
paulson <lp15@cam.ac.uk>
parents: 69735
diff changeset
  2476
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2477
lemma infinite_imp_nonempty: "infinite S \<Longrightarrow> S \<noteq> {}"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2478
  by auto
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2479
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2480
lemma infinite_remove: "infinite S \<Longrightarrow> infinite (S - {a})"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2481
  by simp
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2482
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2483
lemma Diff_infinite_finite:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2484
  assumes "finite T" "infinite S"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2485
  shows "infinite (S - T)"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2486
  using \<open>finite T\<close>
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2487
proof induct
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2488
  from \<open>infinite S\<close> show "infinite (S - {})"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2489
    by auto
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2490
next
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2491
  fix T x
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2492
  assume ih: "infinite (S - T)"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2493
  have "S - (insert x T) = (S - T) - {x}"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2494
    by (rule Diff_insert)
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2495
  with ih show "infinite (S - (insert x T))"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2496
    by (simp add: infinite_remove)
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2497
qed
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2498
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2499
lemma Un_infinite: "infinite S \<Longrightarrow> infinite (S \<union> T)"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2500
  by simp
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2501
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2502
lemma infinite_Un: "infinite (S \<union> T) \<longleftrightarrow> infinite S \<or> infinite T"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2503
  by simp
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2504
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2505
lemma infinite_super:
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2506
  assumes "S \<subseteq> T"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2507
    and "infinite S"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2508
  shows "infinite T"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2509
proof
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2510
  assume "finite T"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2511
  with \<open>S \<subseteq> T\<close> have "finite S" by (simp add: finite_subset)
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2512
  with \<open>infinite S\<close> show False by simp
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2513
qed
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2514
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2515
proposition infinite_coinduct [consumes 1, case_names infinite]:
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2516
  assumes "X A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2517
    and step: "\<And>A. X A \<Longrightarrow> \<exists>x\<in>A. X (A - {x}) \<or> infinite (A - {x})"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2518
  shows "infinite A"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2519
proof
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2520
  assume "finite A"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2521
  then show False
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2522
    using \<open>X A\<close>
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2523
  proof (induction rule: finite_psubset_induct)
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2524
    case (psubset A)
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2525
    then obtain x where "x \<in> A" "X (A - {x}) \<or> infinite (A - {x})"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2526
      using local.step psubset.prems by blast
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2527
    then have "X (A - {x})"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2528
      using psubset.hyps by blast
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2529
    show False
72302
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2530
    proof (rule psubset.IH [where B = "A - {x}"])
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2531
      show "A - {x} \<subset> A"
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2532
        using \<open>x \<in> A\<close> by blast
d7d90ed4c74e fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents: 72097
diff changeset
  2533
    qed fact
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2534
  qed
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2535
qed
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2536
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2537
text \<open>
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2538
  For any function with infinite domain and finite range there is some
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2539
  element that is the image of infinitely many domain elements.  In
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2540
  particular, any infinite sequence of elements from a finite set
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2541
  contains some element that occurs infinitely often.
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2542
\<close>
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2543
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2544
lemma inf_img_fin_dom':
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2545
  assumes img: "finite (f ` A)"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2546
    and dom: "infinite A"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2547
  shows "\<exists>y \<in> f ` A. infinite (f -` {y} \<inter> A)"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2548
proof (rule ccontr)
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2549
  have "A \<subseteq> (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by auto
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2550
  moreover assume "\<not> ?thesis"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2551
  with img have "finite (\<Union>y\<in>f ` A. f -` {y} \<inter> A)" by blast
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2552
  ultimately have "finite A" by (rule finite_subset)
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2553
  with dom show False by contradiction
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2554
qed
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2555
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2556
lemma inf_img_fin_domE':
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2557
  assumes "finite (f ` A)" and "infinite A"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2558
  obtains y where "y \<in> f`A" and "infinite (f -` {y} \<inter> A)"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2559
  using assms by (blast dest: inf_img_fin_dom')
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2560
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2561
lemma inf_img_fin_dom:
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2562
  assumes img: "finite (f`A)" and dom: "infinite A"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2563
  shows "\<exists>y \<in> f`A. infinite (f -` {y})"
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2564
  using inf_img_fin_dom'[OF assms] by auto
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2565
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2566
lemma inf_img_fin_domE:
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2567
  assumes "finite (f`A)" and "infinite A"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2568
  obtains y where "y \<in> f`A" and "infinite (f -` {y})"
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2569
  using assms by (blast dest: inf_img_fin_dom)
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2570
63404
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2571
proposition finite_image_absD: "finite (abs ` S) \<Longrightarrow> finite S"
a95e7432d86c misc tuning and modernization;
wenzelm
parents: 63365
diff changeset
  2572
  for S :: "'a::linordered_ring set"
61810
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2573
  by (rule ccontr) (auto simp: abs_eq_iff vimage_def dest: inf_img_fin_dom)
3c5040d5694a sorted out eventually_mono
paulson <lp15@cam.ac.uk>
parents: 61799
diff changeset
  2574
73555
92783562ab78 collected combinatorial material
haftmann
parents: 72384
diff changeset
  2575
69735
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2576
subsection \<open>The finite powerset operator\<close>
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2577
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2578
definition Fpow :: "'a set \<Rightarrow> 'a set set"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2579
where "Fpow A \<equiv> {X. X \<subseteq> A \<and> finite X}"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2580
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2581
lemma Fpow_mono: "A \<subseteq> B \<Longrightarrow> Fpow A \<subseteq> Fpow B"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2582
unfolding Fpow_def by auto
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2583
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2584
lemma empty_in_Fpow: "{} \<in> Fpow A"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2585
unfolding Fpow_def by auto
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2586
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2587
lemma Fpow_not_empty: "Fpow A \<noteq> {}"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2588
using empty_in_Fpow by blast
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2589
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2590
lemma Fpow_subset_Pow: "Fpow A \<subseteq> Pow A"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2591
unfolding Fpow_def by auto
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2592
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2593
lemma Fpow_Pow_finite: "Fpow A = Pow A Int {A. finite A}"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2594
unfolding Fpow_def Pow_def by blast
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2595
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2596
lemma inj_on_image_Fpow:
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2597
  assumes "inj_on f A"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2598
  shows "inj_on (image f) (Fpow A)"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2599
  using assms Fpow_subset_Pow[of A] subset_inj_on[of "image f" "Pow A"]
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2600
    inj_on_image_Pow by blast
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2601
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2602
lemma image_Fpow_mono:
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2603
  assumes "f ` A \<subseteq> B"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2604
  shows "(image f) ` (Fpow A) \<subseteq> Fpow B"
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2605
  using assms by(unfold Fpow_def, auto)
8230dca028eb the theory of Equipollence, and moving Fpow from Cardinals into Main
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
  2606
35722
69419a09a7ff moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
haftmann
parents: 35719
diff changeset
  2607
end