src/HOL/Finite_Set.thy
author paulson
Sat, 27 Dec 2003 21:02:14 +0100
changeset 14331 8dbbb7cf3637
parent 14302 6c24235e8d5d
child 14430 5cb24165a2e1
permissions -rw-r--r--
re-organized numeric lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Finite_Set.thy
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     3
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     5
*)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     6
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     7
header {* Finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     8
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
     9
theory Finite_Set = Divides + Power + Inductive + SetInterval:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    10
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    11
subsection {* Collection of finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    12
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    13
consts Finites :: "'a set set"
13737
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    14
syntax
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    15
  finite :: "'a set => bool"
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    16
translations
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    17
  "finite A" == "A : Finites"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    18
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    19
inductive Finites
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    20
  intros
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    21
    emptyI [simp, intro!]: "{} : Finites"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    22
    insertI [simp, intro!]: "A : Finites ==> insert a A : Finites"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    23
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    24
axclass finite \<subseteq> type
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    25
  finite: "finite UNIV"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    26
13737
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    27
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    28
 "\<lbrakk> ~finite(UNIV::'a set); finite A \<rbrakk> \<Longrightarrow> \<exists>a::'a. a ~: A"
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    29
by(subgoal_tac "A ~= UNIV", blast, blast)
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    30
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    31
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    32
lemma finite_induct [case_names empty insert, induct set: Finites]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    33
  "finite F ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    34
    P {} ==> (!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    35
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    36
proof -
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    37
  assume "P {}" and
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    38
    insert: "!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    39
  assume "finite F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    40
  thus "P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    41
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    42
    show "P {}" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    43
    fix F x assume F: "finite F" and P: "P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    44
    show "P (insert x F)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    45
    proof cases
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    46
      assume "x \<in> F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    47
      hence "insert x F = F" by (rule insert_absorb)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    48
      with P show ?thesis by (simp only:)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    49
    next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    50
      assume "x \<notin> F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    51
      from F this P show ?thesis by (rule insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    52
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    53
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    54
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    55
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    56
lemma finite_subset_induct [consumes 2, case_names empty insert]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    57
  "finite F ==> F \<subseteq> A ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    58
    P {} ==> (!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)) ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    59
    P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    60
proof -
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    61
  assume "P {}" and insert:
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    62
    "!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    63
  assume "finite F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    64
  thus "F \<subseteq> A ==> P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    65
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    66
    show "P {}" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    67
    fix F x assume "finite F" and "x \<notin> F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    68
      and P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    69
    show "P (insert x F)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    70
    proof (rule insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    71
      from i show "x \<in> A" by blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    72
      from i have "F \<subseteq> A" by blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    73
      with P show "P F" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    74
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    75
  qed
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
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    78
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    79
  -- {* The union of two finite sets is finite. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    80
  by (induct set: Finites) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    81
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    82
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    83
  -- {* Every subset of a finite set is finite. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    84
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    85
  assume "finite B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    86
  thus "!!A. A \<subseteq> B ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    87
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    88
    case empty
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    89
    thus ?case by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    90
  next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    91
    case (insert F x A)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    92
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    93
    show "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    94
    proof cases
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    95
      assume x: "x \<in> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    96
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    97
      with r have "finite (A - {x})" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    98
      hence "finite (insert x (A - {x}))" ..
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    99
      also have "insert x (A - {x}) = A" by (rule insert_Diff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   100
      finally show ?thesis .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   101
    next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   102
      show "A \<subseteq> F ==> ?thesis" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   103
      assume "x \<notin> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   104
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   105
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   106
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   107
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   108
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   109
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   110
  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   111
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   112
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   113
  -- {* The converse obviously fails. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   114
  by (blast intro: finite_subset)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   115
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   116
lemma finite_insert [simp]: "finite (insert a A) = finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   117
  apply (subst insert_is_Un)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   118
  apply (simp only: finite_Un, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   119
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   120
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   121
lemma finite_empty_induct:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   122
  "finite A ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   123
  P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   124
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   125
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   126
    and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   127
  have "P (A - A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   128
  proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   129
    fix c b :: "'a set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   130
    presume c: "finite c" and b: "finite b"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   131
      and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   132
    from c show "c \<subseteq> b ==> P (b - c)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   133
    proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   134
      case empty
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   135
      from P1 show ?case by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   136
    next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   137
      case (insert F x)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   138
      have "P (b - F - {x})"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   139
      proof (rule P2)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   140
        from _ b show "finite (b - F)" by (rule finite_subset) blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   141
        from insert show "x \<in> b - F" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   142
        from insert show "P (b - F)" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   143
      qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   144
      also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   145
      finally show ?case .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   146
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   147
  next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   148
    show "A \<subseteq> A" ..
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   149
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   150
  thus "P {}" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   151
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   152
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   153
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   154
  by (rule Diff_subset [THEN finite_subset])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   155
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   156
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   157
  apply (subst Diff_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   158
  apply (case_tac "a : A - B")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   159
   apply (rule finite_insert [symmetric, THEN trans])
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   160
   apply (subst insert_Diff, simp_all)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   161
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   162
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   163
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   164
subsubsection {* Image and Inverse Image over Finite Sets *}
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   165
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   166
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   167
  -- {* The image of a finite set is finite. *}
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   168
  by (induct set: Finites) simp_all
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   169
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   170
lemma finite_range_imageI:
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   171
    "finite (range g) ==> finite (range (%x. f (g x)))"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   172
  apply (drule finite_imageI, simp)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   173
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   174
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   175
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   176
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   177
  have aux: "!!A. finite (A - {}) = finite A" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   178
  fix B :: "'a set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   179
  assume "finite B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   180
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   181
    apply induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   182
     apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   183
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   184
     apply clarify
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   185
     apply (simp (no_asm_use) add: inj_on_def)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   186
     apply (blast dest!: aux [THEN iffD1], atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   187
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   188
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   189
    apply (rule_tac x = xa in bexI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   190
     apply (simp_all add: inj_on_image_set_diff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   191
    done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   192
qed (rule refl)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   193
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   194
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   195
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   196
  -- {* The inverse image of a singleton under an injective function
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   197
         is included in a singleton. *}
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   198
  apply (auto simp add: inj_on_def) 
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   199
  apply (blast intro: the_equality [symmetric]) 
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   200
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   201
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   202
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   203
  -- {* The inverse image of a finite set under an injective function
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   204
         is finite. *}
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   205
  apply (induct set: Finites, simp_all) 
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   206
  apply (subst vimage_insert) 
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   207
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton]) 
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   208
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   209
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   210
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   211
subsubsection {* The finite UNION of finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   212
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   213
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   214
  by (induct set: Finites) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   215
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   216
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   217
  Strengthen RHS to
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   218
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x ~= {}})"}?
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   219
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   220
  We'd need to prove
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   221
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x ~= {}}"}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   222
  by induction. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   223
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   224
lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   225
  by (blast intro: finite_UN_I finite_subset)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   226
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   227
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   228
subsubsection {* Sigma of finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   229
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   230
lemma finite_SigmaI [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   231
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   232
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   233
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   234
lemma finite_Prod_UNIV:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   235
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   236
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   237
   apply (erule ssubst)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   238
   apply (erule finite_SigmaI, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   239
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   240
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   241
instance unit :: finite
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   242
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   243
  have "finite {()}" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   244
  also have "{()} = UNIV" by auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   245
  finally show "finite (UNIV :: unit set)" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   246
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   247
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   248
instance * :: (finite, finite) finite
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   249
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   250
  show "finite (UNIV :: ('a \<times> 'b) set)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   251
  proof (rule finite_Prod_UNIV)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   252
    show "finite (UNIV :: 'a set)" by (rule finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   253
    show "finite (UNIV :: 'b set)" by (rule finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   254
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   255
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   256
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   257
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   258
subsubsection {* The powerset of a finite set *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   259
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   260
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   261
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   262
  assume "finite (Pow A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   263
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   264
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   265
next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   266
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   267
  thus "finite (Pow A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   268
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   269
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   270
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   271
lemma finite_converse [iff]: "finite (r^-1) = finite r"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   272
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   273
   apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   274
   apply (rule iffI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   275
    apply (erule finite_imageD [unfolded inj_on_def])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   276
    apply (simp split add: split_split)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   277
   apply (erule finite_imageI)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   278
  apply (simp add: converse_def image_def, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   279
  apply (rule bexI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   280
   prefer 2 apply assumption
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   281
  apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   282
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   283
12937
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   284
lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..k(}"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   285
  by (induct k) (simp_all add: lessThan_Suc)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   286
12937
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   287
lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   288
  by (induct k) (simp_all add: atMost_Suc)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   289
13735
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   290
lemma finite_greaterThanLessThan [iff]:
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   291
  fixes l :: nat shows "finite {)l..u(}"
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   292
by (simp add: greaterThanLessThan_def)
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   293
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   294
lemma finite_atLeastLessThan [iff]:
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   295
  fixes l :: nat shows "finite {l..u(}"
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   296
by (simp add: atLeastLessThan_def)
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   297
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   298
lemma finite_greaterThanAtMost [iff]:
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   299
  fixes l :: nat shows "finite {)l..u}"
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   300
by (simp add: greaterThanAtMost_def)
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   301
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   302
lemma finite_atLeastAtMost [iff]:
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   303
  fixes l :: nat shows "finite {l..u}"
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   304
by (simp add: atLeastAtMost_def)
7de9342aca7a HOL-Algebra partially ported to Isar.
ballarin
parents: 13704
diff changeset
   305
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   306
lemma bounded_nat_set_is_finite:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   307
    "(ALL i:N. i < (n::nat)) ==> finite N"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   308
  -- {* A bounded set of natural numbers is finite. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   309
  apply (rule finite_subset)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   310
   apply (rule_tac [2] finite_lessThan, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   311
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   312
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   313
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   314
subsubsection {* Finiteness of transitive closure *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   315
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   316
text {* (Thanks to Sidi Ehmety) *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   317
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   318
lemma finite_Field: "finite r ==> finite (Field r)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   319
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   320
  apply (induct set: Finites)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   321
   apply (auto simp add: Field_def Domain_insert Range_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   322
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   323
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   324
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   325
  apply clarify
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   326
  apply (erule trancl_induct)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   327
   apply (auto simp add: Field_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   328
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   329
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   330
lemma finite_trancl: "finite (r^+) = finite r"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   331
  apply auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   332
   prefer 2
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   333
   apply (rule trancl_subset_Field2 [THEN finite_subset])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   334
   apply (rule finite_SigmaI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   335
    prefer 3
13704
854501b1e957 Transitive closure is now defined inductively as well.
berghofe
parents: 13595
diff changeset
   336
    apply (blast intro: r_into_trancl' finite_subset)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   337
   apply (auto simp add: finite_Field)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   338
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   339
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   340
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   341
subsection {* Finite cardinality *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   342
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   343
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   344
  This definition, although traditional, is ugly to work with: @{text
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   345
  "card A == LEAST n. EX f. A = {f i | i. i < n}"}.  Therefore we have
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   346
  switched to an inductive one:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   347
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   348
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   349
consts cardR :: "('a set \<times> nat) set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   350
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   351
inductive cardR
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   352
  intros
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   353
    EmptyI: "({}, 0) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   354
    InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   355
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   356
constdefs
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   357
  card :: "'a set => nat"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   358
  "card A == THE n. (A, n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   359
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   360
inductive_cases cardR_emptyE: "({}, n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   361
inductive_cases cardR_insertE: "(insert a A,n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   362
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   363
lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   364
  by (induct set: cardR) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   365
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   366
lemma cardR_determ_aux1:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   367
    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   368
  apply (induct set: cardR, auto)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   369
  apply (simp add: insert_Diff_if, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   370
  apply (drule cardR_SucD)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   371
  apply (blast intro!: cardR.intros)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   372
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   373
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   374
lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   375
  by (drule cardR_determ_aux1) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   376
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   377
lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   378
  apply (induct set: cardR)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   379
   apply (safe elim!: cardR_emptyE cardR_insertE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   380
  apply (rename_tac B b m)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   381
  apply (case_tac "a = b")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   382
   apply (subgoal_tac "A = B")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   383
    prefer 2 apply (blast elim: equalityE, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   384
  apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   385
   prefer 2
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   386
   apply (rule_tac x = "A Int B" in exI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   387
   apply (blast elim: equalityE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   388
  apply (frule_tac A = B in cardR_SucD)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   389
  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   390
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   391
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   392
lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   393
  by (induct set: cardR) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   394
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   395
lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   396
  by (induct set: Finites) (auto intro!: cardR.intros)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   397
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   398
lemma card_equality: "(A,n) : cardR ==> card A = n"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   399
  by (unfold card_def) (blast intro: cardR_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   400
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   401
lemma card_empty [simp]: "card {} = 0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   402
  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   403
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   404
lemma card_insert_disjoint [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   405
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   406
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   407
  assume x: "x \<notin> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   408
  hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   409
    apply (auto intro!: cardR.intros)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   410
    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   411
     apply (force dest: cardR_imp_finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   412
    apply (blast intro!: cardR.intros intro: cardR_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   413
    done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   414
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   415
  thus ?thesis
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   416
    apply (simp add: card_def aux)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   417
    apply (rule the_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   418
     apply (auto intro: finite_imp_cardR
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   419
       cong: conj_cong simp: card_def [symmetric] card_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   420
    done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   421
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   422
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   423
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   424
  apply auto
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   425
  apply (drule_tac a = x in mk_disjoint_insert, clarify)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   426
  apply (rotate_tac -1, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   427
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   428
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   429
lemma card_insert_if:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   430
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   431
  by (simp add: insert_absorb)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   432
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   433
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
14302
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   434
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   435
apply(simp del:insert_Diff_single)
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   436
done
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   437
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   438
lemma card_Diff_singleton:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   439
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   440
  by (simp add: card_Suc_Diff1 [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   441
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   442
lemma card_Diff_singleton_if:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   443
    "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   444
  by (simp add: card_Diff_singleton)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   445
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   446
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   447
  by (simp add: card_insert_if card_Suc_Diff1)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   448
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   449
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   450
  by (simp add: card_insert_if)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   451
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   452
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   453
  apply (induct set: Finites, simp, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   454
  apply (subgoal_tac "finite A & A - {x} <= F")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   455
   prefer 2 apply (blast intro: finite_subset, atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   456
  apply (drule_tac x = "A - {x}" in spec)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   457
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   458
  apply (case_tac "card A", auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   459
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   460
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   461
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   462
  apply (simp add: psubset_def linorder_not_le [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   463
  apply (blast dest: card_seteq)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   464
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   465
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   466
lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   467
  apply (case_tac "A = B", simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   468
  apply (simp add: linorder_not_less [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   469
  apply (blast dest: card_seteq intro: order_less_imp_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   470
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   471
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   472
lemma card_Un_Int: "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   473
    ==> card A + card B = card (A Un B) + card (A Int B)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   474
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   475
  apply (simp add: insert_absorb Int_insert_left)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   476
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   477
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   478
lemma card_Un_disjoint: "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   479
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   480
  by (simp add: card_Un_Int)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   481
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   482
lemma card_Diff_subset:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   483
    "finite A ==> B <= A ==> card A - card B = card (A - B)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   484
  apply (subgoal_tac "(A - B) Un B = A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   485
   prefer 2 apply blast
14331
8dbbb7cf3637 re-organized numeric lemmas
paulson
parents: 14302
diff changeset
   486
  apply (rule nat_add_right_cancel [THEN iffD1])
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   487
  apply (rule card_Un_disjoint [THEN subst])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   488
     apply (erule_tac [4] ssubst)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   489
     prefer 3 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   490
    apply (simp_all add: add_commute not_less_iff_le
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   491
      add_diff_inverse card_mono finite_subset)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   492
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   493
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   494
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   495
  apply (rule Suc_less_SucD)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   496
  apply (simp add: card_Suc_Diff1)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   497
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   498
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   499
lemma card_Diff2_less:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   500
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   501
  apply (case_tac "x = y")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   502
   apply (simp add: card_Diff1_less)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   503
  apply (rule less_trans)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   504
   prefer 2 apply (auto intro!: card_Diff1_less)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   505
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   506
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   507
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   508
  apply (case_tac "x : A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   509
   apply (simp_all add: card_Diff1_less less_imp_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   510
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   511
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   512
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   513
by (erule psubsetI, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   514
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   515
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   516
subsubsection {* Cardinality of image *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   517
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   518
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   519
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   520
  apply (simp add: le_SucI finite_imageI card_insert_if)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   521
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   522
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   523
lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   524
  apply (induct set: Finites, simp_all, atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   525
  apply safe
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   526
   apply (unfold inj_on_def, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   527
  apply (subst card_insert_disjoint)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   528
    apply (erule finite_imageI, blast, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   529
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   530
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   531
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   532
  by (simp add: card_seteq card_image)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   533
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   534
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   535
subsubsection {* Cardinality of the Powerset *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   536
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   537
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   538
  apply (induct set: Finites)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   539
   apply (simp_all add: Pow_insert)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   540
  apply (subst card_Un_disjoint, blast)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   541
    apply (blast intro: finite_imageI, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   542
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   543
   apply (simp add: card_image Pow_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   544
  apply (unfold inj_on_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   545
  apply (blast elim!: equalityE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   546
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   547
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   548
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   549
  \medskip Relates to equivalence classes.  Based on a theorem of
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   550
  F. Kammüller's.  The @{prop "finite C"} premise is redundant.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   551
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   552
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   553
lemma dvd_partition:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   554
  "finite C ==> finite (Union C) ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   555
    ALL c : C. k dvd card c ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   556
    (ALL c1: C. ALL c2: C. c1 ~= c2 --> c1 Int c2 = {}) ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   557
  k dvd card (Union C)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   558
  apply (induct set: Finites, simp_all, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   559
  apply (subst card_Un_disjoint)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   560
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   561
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   562
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   563
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   564
subsection {* A fold functional for finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   565
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   566
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   567
  For @{text n} non-negative we have @{text "fold f e {x1, ..., xn} =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   568
  f x1 (... (f xn e))"} where @{text f} is at least left-commutative.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   569
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   570
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   571
consts
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   572
  foldSet :: "('b => 'a => 'a) => 'a => ('b set \<times> 'a) set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   573
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   574
inductive "foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   575
  intros
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   576
    emptyI [intro]: "({}, e) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   577
    insertI [intro]: "x \<notin> A ==> (A, y) : foldSet f e ==> (insert x A, f x y) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   578
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   579
inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   580
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   581
constdefs
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   582
  fold :: "('b => 'a => 'a) => 'a => 'b set => 'a"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   583
  "fold f e A == THE x. (A, x) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   584
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   585
lemma Diff1_foldSet: "(A - {x}, y) : foldSet f e ==> x: A ==> (A, f x y) : foldSet f e"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   586
by (erule insert_Diff [THEN subst], rule foldSet.intros, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   587
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   588
lemma foldSet_imp_finite [simp]: "(A, x) : foldSet f e ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   589
  by (induct set: foldSet) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   590
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   591
lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   592
  by (induct set: Finites) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   593
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   594
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   595
subsubsection {* Left-commutative operations *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   596
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   597
locale LC =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   598
  fixes f :: "'b => 'a => 'a"    (infixl "\<cdot>" 70)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   599
  assumes left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   600
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   601
lemma (in LC) foldSet_determ_aux:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   602
  "ALL A x. card A < n --> (A, x) : foldSet f e -->
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   603
    (ALL y. (A, y) : foldSet f e --> y = x)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   604
  apply (induct n)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   605
   apply (auto simp add: less_Suc_eq)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   606
  apply (erule foldSet.cases, blast)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   607
  apply (erule foldSet.cases, blast, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   608
  txt {* force simplification of @{text "card A < card (insert ...)"}. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   609
  apply (erule rev_mp)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   610
  apply (simp add: less_Suc_eq_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   611
  apply (rule impI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   612
  apply (rename_tac Aa xa ya Ab xb yb, case_tac "xa = xb")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   613
   apply (subgoal_tac "Aa = Ab")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   614
    prefer 2 apply (blast elim!: equalityE, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   615
  txt {* case @{prop "xa \<notin> xb"}. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   616
  apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb : Aa & xa : Ab")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   617
   prefer 2 apply (blast elim!: equalityE, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   618
  apply (subgoal_tac "Aa = insert xb Ab - {xa}")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   619
   prefer 2 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   620
  apply (subgoal_tac "card Aa <= card Ab")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   621
   prefer 2
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   622
   apply (rule Suc_le_mono [THEN subst])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   623
   apply (simp add: card_Suc_Diff1)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   624
  apply (rule_tac A1 = "Aa - {xb}" and f1 = f in finite_imp_foldSet [THEN exE])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   625
  apply (blast intro: foldSet_imp_finite finite_Diff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   626
  apply (frule (1) Diff1_foldSet)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   627
  apply (subgoal_tac "ya = f xb x")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   628
   prefer 2 apply (blast del: equalityCE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   629
  apply (subgoal_tac "(Ab - {xa}, x) : foldSet f e")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   630
   prefer 2 apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   631
  apply (subgoal_tac "yb = f xa x")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   632
   prefer 2 apply (blast del: equalityCE dest: Diff1_foldSet)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   633
  apply (simp (no_asm_simp) add: left_commute)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   634
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   635
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   636
lemma (in LC) foldSet_determ: "(A, x) : foldSet f e ==> (A, y) : foldSet f e ==> y = x"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   637
  by (blast intro: foldSet_determ_aux [rule_format])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   638
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   639
lemma (in LC) fold_equality: "(A, y) : foldSet f e ==> fold f e A = y"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   640
  by (unfold fold_def) (blast intro: foldSet_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   641
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   642
lemma fold_empty [simp]: "fold f e {} = e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   643
  by (unfold fold_def) blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   644
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   645
lemma (in LC) fold_insert_aux: "x \<notin> A ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   646
    ((insert x A, v) : foldSet f e) =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   647
    (EX y. (A, y) : foldSet f e & v = f x y)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   648
  apply auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   649
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   650
   apply (fastsimp dest: foldSet_imp_finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   651
  apply (blast intro: foldSet_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   652
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   653
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   654
lemma (in LC) fold_insert:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   655
    "finite A ==> x \<notin> A ==> fold f e (insert x A) = f x (fold f e A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   656
  apply (unfold fold_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   657
  apply (simp add: fold_insert_aux)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   658
  apply (rule the_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   659
  apply (auto intro: finite_imp_foldSet
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   660
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   661
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   662
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   663
lemma (in LC) fold_commute: "finite A ==> (!!e. f x (fold f e A) = fold f (f x e) A)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   664
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   665
  apply (simp add: left_commute fold_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   666
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   667
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   668
lemma (in LC) fold_nest_Un_Int:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   669
  "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   670
    ==> fold f (fold f e B) A = fold f (fold f e (A Int B)) (A Un B)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   671
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   672
  apply (simp add: fold_insert fold_commute Int_insert_left insert_absorb)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   673
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   674
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   675
lemma (in LC) fold_nest_Un_disjoint:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   676
  "finite A ==> finite B ==> A Int B = {}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   677
    ==> fold f e (A Un B) = fold f (fold f e B) A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   678
  by (simp add: fold_nest_Un_Int)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   679
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   680
declare foldSet_imp_finite [simp del]
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   681
    empty_foldSetE [rule del]  foldSet.intros [rule del]
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   682
  -- {* Delete rules to do with @{text foldSet} relation. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   683
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   684
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   685
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   686
subsubsection {* Commutative monoids *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   687
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   688
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   689
  We enter a more restrictive context, with @{text "f :: 'a => 'a => 'a"}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   690
  instead of @{text "'b => 'a => 'a"}.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   691
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   692
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   693
locale ACe =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   694
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   695
    and e :: 'a
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   696
  assumes ident [simp]: "x \<cdot> e = x"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   697
    and commute: "x \<cdot> y = y \<cdot> x"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   698
    and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   699
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   700
lemma (in ACe) left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   701
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   702
  have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   703
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   704
  also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   705
  finally show ?thesis .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   706
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   707
12718
ade42a6c22ad lemmas (in ACe) AC;
wenzelm
parents: 12693
diff changeset
   708
lemmas (in ACe) AC = assoc commute left_commute
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   709
12693
827818b891c7 qualified exports from locales;
wenzelm
parents: 12396
diff changeset
   710
lemma (in ACe) left_ident [simp]: "e \<cdot> x = x"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   711
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   712
  have "x \<cdot> e = x" by (rule ident)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   713
  thus ?thesis by (subst commute)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   714
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   715
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   716
lemma (in ACe) fold_Un_Int:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   717
  "finite A ==> finite B ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   718
    fold f e A \<cdot> fold f e B = fold f e (A Un B) \<cdot> fold f e (A Int B)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   719
  apply (induct set: Finites, simp)
13400
dbb608cd11c4 accomodate cumulative locale predicates;
wenzelm
parents: 13390
diff changeset
   720
  apply (simp add: AC insert_absorb Int_insert_left
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
   721
    LC.fold_insert [OF LC.intro])
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   722
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   723
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   724
lemma (in ACe) fold_Un_disjoint:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   725
  "finite A ==> finite B ==> A Int B = {} ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   726
    fold f e (A Un B) = fold f e A \<cdot> fold f e B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   727
  by (simp add: fold_Un_Int)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   728
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   729
lemma (in ACe) fold_Un_disjoint2:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   730
  "finite A ==> finite B ==> A Int B = {} ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   731
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   732
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   733
  assume b: "finite B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   734
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   735
  thus "A Int B = {} ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   736
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   737
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   738
    case empty
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   739
    thus ?case by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   740
  next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   741
    case (insert F x)
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   742
    have "fold (f o g) e (insert x F \<union> B) = fold (f o g) e (insert x (F \<union> B))"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   743
      by simp
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   744
    also have "... = (f o g) x (fold (f o g) e (F \<union> B))"
13400
dbb608cd11c4 accomodate cumulative locale predicates;
wenzelm
parents: 13390
diff changeset
   745
      by (rule LC.fold_insert [OF LC.intro])
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
   746
        (insert b insert, auto simp add: left_commute)
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   747
    also from insert have "fold (f o g) e (F \<union> B) =
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   748
      fold (f o g) e F \<cdot> fold (f o g) e B" by blast
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   749
    also have "(f o g) x ... = (f o g) x (fold (f o g) e F) \<cdot> fold (f o g) e B"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   750
      by (simp add: AC)
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   751
    also have "(f o g) x (fold (f o g) e F) = fold (f o g) e (insert x F)"
13400
dbb608cd11c4 accomodate cumulative locale predicates;
wenzelm
parents: 13390
diff changeset
   752
      by (rule LC.fold_insert [OF LC.intro, symmetric]) (insert b insert,
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
   753
	auto simp add: left_commute)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   754
    finally show ?case .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   755
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   756
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   757
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   758
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   759
subsection {* Generalized summation over a set *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   760
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   761
constdefs
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   762
  setsum :: "('a => 'b) => 'a set => 'b::plus_ac0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   763
  "setsum f A == if finite A then fold (op + o f) 0 A else 0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   764
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   765
syntax
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   766
  "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0"    ("\<Sum>_:_. _" [0, 51, 10] 10)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   767
syntax (xsymbols)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   768
  "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0"    ("\<Sum>_\<in>_. _" [0, 51, 10] 10)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   769
translations
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   770
  "\<Sum>i:A. b" == "setsum (%i. b) A"  -- {* Beware of argument permutation! *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   771
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   772
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   773
lemma setsum_empty [simp]: "setsum f {} = 0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   774
  by (simp add: setsum_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   775
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   776
lemma setsum_insert [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   777
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
13365
a2c4faad4d35 adapted to locale defs;
wenzelm
parents: 12937
diff changeset
   778
  by (simp add: setsum_def
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
   779
    LC.fold_insert [OF LC.intro] plus_ac0_left_commute)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   780
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   781
lemma setsum_0: "setsum (\<lambda>i. 0) A = 0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   782
  apply (case_tac "finite A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   783
   prefer 2 apply (simp add: setsum_def)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   784
  apply (erule finite_induct, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   785
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   786
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   787
lemma setsum_eq_0_iff [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   788
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   789
  by (induct set: Finites) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   790
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   791
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   792
  apply (case_tac "finite A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   793
   prefer 2 apply (simp add: setsum_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   794
  apply (erule rev_mp)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   795
  apply (erule finite_induct, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   796
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   797
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   798
lemma card_eq_setsum: "finite A ==> card A = setsum (\<lambda>x. 1) A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   799
  -- {* Could allow many @{text "card"} proofs to be simplified. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   800
  by (induct set: Finites) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   801
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   802
lemma setsum_Un_Int: "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   803
    ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   804
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   805
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   806
  apply (simp add: plus_ac0 Int_insert_left insert_absorb)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   807
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   808
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   809
lemma setsum_Un_disjoint: "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   810
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   811
  apply (subst setsum_Un_Int [symmetric], auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   812
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   813
12937
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   814
lemma setsum_UN_disjoint:
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   815
  fixes f :: "'a => 'b::plus_ac0"
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   816
  shows
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   817
    "finite I ==> (ALL i:I. finite (A i)) ==>
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   818
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
0c4fd7529467 clarified syntax of ``long'' statements: fixes/assumes/shows;
wenzelm
parents: 12718
diff changeset
   819
      setsum f (UNION I A) = setsum (\<lambda>i. setsum f (A i)) I"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   820
  apply (induct set: Finites, simp, atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   821
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   822
   prefer 2 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   823
  apply (subgoal_tac "A x Int UNION F A = {}")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   824
   prefer 2 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   825
  apply (simp add: setsum_Un_disjoint)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   826
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   827
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   828
lemma setsum_addf: "setsum (\<lambda>x. f x + g x) A = (setsum f A + setsum g A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   829
  apply (case_tac "finite A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   830
   prefer 2 apply (simp add: setsum_def)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   831
  apply (erule finite_induct, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   832
  apply (simp add: plus_ac0)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   833
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   834
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   835
lemma setsum_Un: "finite A ==> finite B ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   836
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   837
  -- {* For the natural numbers, we have subtraction. *}
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   838
  apply (subst setsum_Un_Int [symmetric], auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   839
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   840
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   841
lemma setsum_diff1: "(setsum f (A - {a}) :: nat) =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   842
    (if a:A then setsum f A - f a else setsum f A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   843
  apply (case_tac "finite A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   844
   prefer 2 apply (simp add: setsum_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   845
  apply (erule finite_induct)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   846
   apply (auto simp add: insert_Diff_if)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   847
  apply (drule_tac a = a in mk_disjoint_insert, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   848
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   849
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   850
lemma setsum_cong:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   851
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   852
  apply (case_tac "finite B")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   853
   prefer 2 apply (simp add: setsum_def, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   854
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setsum f C = setsum g C")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   855
   apply simp
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   856
  apply (erule finite_induct, simp)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   857
  apply (simp add: subset_insert_iff, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   858
  apply (subgoal_tac "finite C")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   859
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   860
  apply (subgoal_tac "C = insert x (C - {x})")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   861
   prefer 2 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   862
  apply (erule ssubst)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   863
  apply (drule spec)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   864
  apply (erule (1) notE impE)
14302
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   865
  apply (simp add: Ball_def del:insert_Diff_single)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   866
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   867
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   868
subsubsection{* Min and Max of finite linearly ordered sets *}
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   869
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   870
text{* Seemed easier to define directly than via fold. *}
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   871
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   872
lemma ex_Max: fixes S :: "('a::linorder)set"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   873
  assumes fin: "finite S" shows "S \<noteq> {} \<Longrightarrow> \<exists>m\<in>S. \<forall>s \<in> S. s \<le> m"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   874
using fin
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   875
proof (induct)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   876
  case empty thus ?case by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   877
next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   878
  case (insert S x)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   879
  show ?case
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   880
  proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   881
    assume "S = {}" thus ?thesis by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   882
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   883
    assume nonempty: "S \<noteq> {}"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   884
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. s \<le> m" using insert by blast
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   885
    show ?thesis
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   886
    proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   887
      assume "x \<le> m" thus ?thesis using m by blast
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   888
    next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   889
      assume "\<not> x \<le> m" thus ?thesis using m
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   890
	by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   891
    qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   892
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   893
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   894
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   895
lemma ex_Min: fixes S :: "('a::linorder)set"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   896
  assumes fin: "finite S" shows "S \<noteq> {} \<Longrightarrow> \<exists>m\<in>S. \<forall>s \<in> S. m \<le> s"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   897
using fin
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   898
proof (induct)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   899
  case empty thus ?case by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   900
next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   901
  case (insert S x)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   902
  show ?case
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   903
  proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   904
    assume "S = {}" thus ?thesis by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   905
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   906
    assume nonempty: "S \<noteq> {}"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   907
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. m \<le> s" using insert by blast
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   908
    show ?thesis
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   909
    proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   910
      assume "m \<le> x" thus ?thesis using m by blast
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   911
    next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   912
      assume "\<not> m \<le> x" thus ?thesis using m
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   913
	by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   914
    qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   915
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   916
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   917
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   918
constdefs
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   919
 Min :: "('a::linorder)set \<Rightarrow> 'a"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   920
"Min S  \<equiv>  THE m. m \<in> S \<and> (\<forall>s \<in> S. m \<le> s)"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   921
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   922
 Max :: "('a::linorder)set \<Rightarrow> 'a"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   923
"Max S  \<equiv>  THE m. m \<in> S \<and> (\<forall>s \<in> S. s \<le> m)"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   924
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   925
lemma Min[simp]: assumes a: "finite S" "S \<noteq> {}"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   926
                 shows "Min S \<in> S \<and> (\<forall>s \<in> S. Min S \<le> s)" (is "?P(Min S)")
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   927
proof (unfold Min_def, rule theI')
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   928
  show "\<exists>!m. ?P m"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   929
  proof (rule ex_ex1I)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   930
    show "\<exists>m. ?P m" using ex_Min[OF a] by blast
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   931
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   932
    fix m1 m2 assume "?P m1" "?P m2"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   933
    thus "m1 = m2" by (blast dest:order_antisym)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   934
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   935
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   936
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   937
lemma Max[simp]: assumes a: "finite S" "S \<noteq> {}"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   938
                 shows "Max S \<in> S \<and> (\<forall>s \<in> S. s \<le> Max S)" (is "?P(Max S)")
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   939
proof (unfold Max_def, rule theI')
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   940
  show "\<exists>!m. ?P m"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   941
  proof (rule ex_ex1I)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   942
    show "\<exists>m. ?P m" using ex_Max[OF a] by blast
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   943
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   944
    fix m1 m2 assume "?P m1" "?P m2"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   945
    thus "m1 = m2" by (blast dest:order_antisym)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   946
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   947
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
   948
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   949
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   950
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   951
  \medskip Basic theorem about @{text "choose"}.  By Florian
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   952
  Kammüller, tidied by LCP.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   953
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   954
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   955
lemma card_s_0_eq_empty:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   956
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   957
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   958
  apply (simp cong add: rev_conj_cong)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   959
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   960
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   961
lemma choose_deconstruct: "finite M ==> x \<notin> M
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   962
  ==> {s. s <= insert x M & card(s) = Suc k}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   963
       = {s. s <= M & card(s) = Suc k} Un
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   964
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   965
  apply safe
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   966
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   967
  apply (drule_tac x = "xa - {x}" in spec)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   968
  apply (subgoal_tac "x ~: xa", auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   969
  apply (erule rev_mp, subst card_Diff_singleton)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   970
  apply (auto intro: finite_subset)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   971
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   972
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   973
lemma card_inj_on_le:
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   974
    "[|inj_on f A; f ` A \<subseteq> B; finite A; finite B |] ==> card A <= card B"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   975
  by (auto intro: card_mono simp add: card_image [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   976
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   977
lemma card_bij_eq: 
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   978
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A; 
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   979
       finite A; finite B |] ==> card A = card B"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   980
  by (auto intro: le_anti_sym card_inj_on_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   981
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   982
text{*There are as many subsets of @{term A} having cardinality @{term k}
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   983
 as there are sets obtained from the former by inserting a fixed element
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   984
 @{term x} into each.*}
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   985
lemma constr_bij:
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   986
   "[|finite A; x \<notin> A|] ==>
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   987
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   988
    card {B. B <= A & card(B) = k}"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   989
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   990
       apply (auto elim!: equalityE simp add: inj_on_def)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   991
    apply (subst Diff_insert0, auto)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   992
   txt {* finiteness of the two sets *}
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   993
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   994
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
   995
   apply fast+
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   996
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   997
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   998
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   999
  Main theorem: combinatorial statement about number of subsets of a set.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1000
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1001
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1002
lemma n_sub_lemma:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1003
  "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1004
  apply (induct k)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
  1005
   apply (simp add: card_s_0_eq_empty, atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1006
  apply (rotate_tac -1, erule finite_induct)
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1007
   apply (simp_all (no_asm_simp) cong add: conj_cong
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1008
     add: card_s_0_eq_empty choose_deconstruct)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1009
  apply (subst card_Un_disjoint)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1010
     prefer 4 apply (force simp add: constr_bij)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1011
    prefer 3 apply force
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1012
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1013
     finite_subset [of _ "Pow (insert x F)", standard])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1014
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1015
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1016
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1017
theorem n_subsets:
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1018
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1019
  by (simp add: n_sub_lemma)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1020
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1021
end