src/HOL/Finite_Set.thy
author obua
Tue, 23 Nov 2004 15:25:39 +0100
changeset 15311 2ca1c66a6758
parent 15309 173669c88fd2
child 15314 55eec5c6d401
permissions -rw-r--r--
Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
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
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
     4
                Additions by Jeremy Avigad in Feb 2004
12396
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
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15124
diff changeset
     9
theory Finite_Set
15140
322485b816ac import -> imports
nipkow
parents: 15131
diff changeset
    10
imports Divides Power Inductive
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15124
diff changeset
    11
begin
12396
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
subsection {* Collection of finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    14
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    15
consts Finites :: "'a set set"
13737
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    16
syntax
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    17
  finite :: "'a set => bool"
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    18
translations
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    19
  "finite A" == "A : Finites"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    20
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    21
inductive Finites
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    22
  intros
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    23
    emptyI [simp, intro!]: "{} : Finites"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    24
    insertI [simp, intro!]: "A : Finites ==> insert a A : Finites"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    25
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    26
axclass finite \<subseteq> type
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    27
  finite: "finite UNIV"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    28
13737
e564c3d2d174 added a few lemmas
nipkow
parents: 13735
diff changeset
    29
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    30
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    31
  shows "\<exists>a::'a. a \<notin> A"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    32
proof -
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    33
  from prems have "A \<noteq> UNIV" by blast
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    34
  thus ?thesis by blast
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
    35
qed
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    36
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    37
lemma finite_induct [case_names empty insert, induct set: Finites]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    38
  "finite F ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    39
    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
    40
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    41
proof -
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    42
  assume "P {}" and
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    43
    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
    44
  assume "finite F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    45
  thus "P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    46
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    47
    show "P {}" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    48
    fix F x assume F: "finite F" and P: "P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    49
    show "P (insert x F)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    50
    proof cases
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    51
      assume "x \<in> F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    52
      hence "insert x F = F" by (rule insert_absorb)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    53
      with P show ?thesis by (simp only:)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    54
    next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    55
      assume "x \<notin> F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    56
      from F this P show ?thesis by (rule insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    57
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    58
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    59
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    60
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    61
lemma finite_subset_induct [consumes 2, case_names empty insert]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    62
  "finite F ==> F \<subseteq> A ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    63
    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
    64
    P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    65
proof -
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    66
  assume "P {}" and insert:
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
    67
    "!!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
    68
  assume "finite F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    69
  thus "F \<subseteq> A ==> P F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    70
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    71
    show "P {}" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    72
    fix F x assume "finite F" and "x \<notin> F"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    73
      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
    74
    show "P (insert x F)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    75
    proof (rule insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    76
      from i show "x \<in> A" by blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    77
      from i have "F \<subseteq> A" by blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    78
      with P show "P F" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    79
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    80
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    81
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    82
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    83
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    84
  -- {* The union of two finite sets is finite. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    85
  by (induct set: Finites) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    86
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    87
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    88
  -- {* Every subset of a finite set is finite. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    89
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    90
  assume "finite B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    91
  thus "!!A. A \<subseteq> B ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    92
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    93
    case empty
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    94
    thus ?case by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    95
  next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    96
    case (insert F x A)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    97
    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
    98
    show "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
    99
    proof cases
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   100
      assume x: "x \<in> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   101
      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
   102
      with r have "finite (A - {x})" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   103
      hence "finite (insert x (A - {x}))" ..
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   104
      also have "insert x (A - {x}) = A" by (rule insert_Diff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   105
      finally show ?thesis .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   106
    next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   107
      show "A \<subseteq> F ==> ?thesis" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   108
      assume "x \<notin> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   109
      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
   110
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   111
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   112
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   113
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   114
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
   115
  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
   116
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   117
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
   118
  -- {* The converse obviously fails. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   119
  by (blast intro: finite_subset)
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_insert [simp]: "finite (insert a A) = finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   122
  apply (subst insert_is_Un)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   123
  apply (simp only: finite_Un, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   124
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   125
15281
bd4611956c7b More lemmas
nipkow
parents: 15234
diff changeset
   126
lemma finite_Union[simp, intro]:
bd4611956c7b More lemmas
nipkow
parents: 15234
diff changeset
   127
 "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
bd4611956c7b More lemmas
nipkow
parents: 15234
diff changeset
   128
by (induct rule:finite_induct) simp_all
bd4611956c7b More lemmas
nipkow
parents: 15234
diff changeset
   129
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   130
lemma finite_empty_induct:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   131
  "finite A ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   132
  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
   133
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   134
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   135
    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
   136
  have "P (A - A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   137
  proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   138
    fix c b :: "'a set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   139
    presume c: "finite c" and b: "finite b"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   140
      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
   141
    from c show "c \<subseteq> b ==> P (b - c)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   142
    proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   143
      case empty
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   144
      from P1 show ?case by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   145
    next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   146
      case (insert F x)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   147
      have "P (b - F - {x})"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   148
      proof (rule P2)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   149
        from _ b show "finite (b - F)" by (rule finite_subset) blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   150
        from insert show "x \<in> b - F" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   151
        from insert show "P (b - F)" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   152
      qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   153
      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
   154
      finally show ?case .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   155
    qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   156
  next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   157
    show "A \<subseteq> A" ..
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   158
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   159
  thus "P {}" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   160
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   161
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   162
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   163
  by (rule Diff_subset [THEN finite_subset])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   164
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   165
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
   166
  apply (subst Diff_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   167
  apply (case_tac "a : A - B")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   168
   apply (rule finite_insert [symmetric, THEN trans])
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   169
   apply (subst insert_Diff, simp_all)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   170
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   171
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   172
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   173
subsubsection {* Image and Inverse Image over Finite Sets *}
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   174
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   175
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   176
  -- {* The image of a finite set is finite. *}
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   177
  by (induct set: Finites) simp_all
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   178
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   179
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   180
  apply (frule finite_imageI)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   181
  apply (erule finite_subset, assumption)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   182
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   183
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   184
lemma finite_range_imageI:
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   185
    "finite (range g) ==> finite (range (%x. f (g x)))"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   186
  apply (drule finite_imageI, simp)
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   187
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   188
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   189
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
   190
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   191
  have aux: "!!A. finite (A - {}) = finite A" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   192
  fix B :: "'a set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   193
  assume "finite B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   194
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   195
    apply induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   196
     apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   197
    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
   198
     apply clarify
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   199
     apply (simp (no_asm_use) add: inj_on_def)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   200
     apply (blast dest!: aux [THEN iffD1], atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   201
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   202
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   203
    apply (rule_tac x = xa in bexI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   204
     apply (simp_all add: inj_on_image_set_diff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   205
    done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   206
qed (rule refl)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   207
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   208
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   209
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   210
  -- {* The inverse image of a singleton under an injective function
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   211
         is included in a singleton. *}
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   212
  apply (auto simp add: inj_on_def)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   213
  apply (blast intro: the_equality [symmetric])
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   214
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   215
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   216
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   217
  -- {* The inverse image of a finite set under an injective function
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   218
         is finite. *}
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   219
  apply (induct set: Finites, simp_all)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   220
  apply (subst vimage_insert)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   221
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton])
13825
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   222
  done
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   223
ef4c41e7956a new inverse image lemmas
paulson
parents: 13737
diff changeset
   224
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   225
subsubsection {* The finite UNION of finite sets *}
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
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
   228
  by (induct set: Finites) simp_all
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
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   231
  Strengthen RHS to
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   232
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
12396
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
  We'd need to prove
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   235
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   236
  by induction. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   237
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   238
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
   239
  by (blast intro: finite_UN_I finite_subset)
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
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   242
subsubsection {* Sigma of finite sets *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   243
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   244
lemma finite_SigmaI [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   245
    "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
   246
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
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
lemma finite_Prod_UNIV:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   249
    "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
   250
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   251
   apply (erule ssubst)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   252
   apply (erule finite_SigmaI, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   253
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   254
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   255
instance unit :: finite
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   256
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   257
  have "finite {()}" by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   258
  also have "{()} = UNIV" by auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   259
  finally show "finite (UNIV :: unit set)" .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   260
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   261
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   262
instance * :: (finite, finite) finite
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   263
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   264
  show "finite (UNIV :: ('a \<times> 'b) set)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   265
  proof (rule finite_Prod_UNIV)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   266
    show "finite (UNIV :: 'a set)" by (rule finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   267
    show "finite (UNIV :: 'b set)" by (rule finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   268
  qed
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
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   272
subsubsection {* The powerset of a finite set *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   273
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   274
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   275
proof
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   276
  assume "finite (Pow A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   277
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   278
  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
   279
next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   280
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   281
  thus "finite (Pow A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   282
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   283
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   284
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   285
lemma finite_converse [iff]: "finite (r^-1) = finite r"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   286
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   287
   apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   288
   apply (rule iffI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   289
    apply (erule finite_imageD [unfolded inj_on_def])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   290
    apply (simp split add: split_split)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   291
   apply (erule finite_imageI)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   292
  apply (simp add: converse_def image_def, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   293
  apply (rule bexI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   294
   prefer 2 apply assumption
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   295
  apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   296
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   297
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   298
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   299
subsubsection {* Finiteness of transitive closure *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   300
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   301
text {* (Thanks to Sidi Ehmety) *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   302
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   303
lemma finite_Field: "finite r ==> finite (Field r)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   304
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   305
  apply (induct set: Finites)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   306
   apply (auto simp add: Field_def Domain_insert Range_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   307
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   308
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   309
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   310
  apply clarify
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   311
  apply (erule trancl_induct)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   312
   apply (auto simp add: Field_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   313
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   314
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   315
lemma finite_trancl: "finite (r^+) = finite r"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   316
  apply auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   317
   prefer 2
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   318
   apply (rule trancl_subset_Field2 [THEN finite_subset])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   319
   apply (rule finite_SigmaI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   320
    prefer 3
13704
854501b1e957 Transitive closure is now defined inductively as well.
berghofe
parents: 13595
diff changeset
   321
    apply (blast intro: r_into_trancl' finite_subset)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   322
   apply (auto simp add: finite_Field)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   323
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   324
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   325
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   326
    finite (A <*> B)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   327
  by (rule finite_SigmaI)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   328
12396
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
subsection {* Finite cardinality *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   331
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   332
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   333
  This definition, although traditional, is ugly to work with: @{text
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   334
  "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
   335
  switched to an inductive one:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   336
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   337
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   338
consts cardR :: "('a set \<times> nat) set"
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
inductive cardR
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   341
  intros
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   342
    EmptyI: "({}, 0) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   343
    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
   344
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   345
constdefs
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   346
  card :: "'a set => nat"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   347
  "card A == THE n. (A, n) : cardR"
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
inductive_cases cardR_emptyE: "({}, n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   350
inductive_cases cardR_insertE: "(insert a A,n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   351
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   352
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
   353
  by (induct set: cardR) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   354
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   355
lemma cardR_determ_aux1:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   356
    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   357
  apply (induct set: cardR, auto)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   358
  apply (simp add: insert_Diff_if, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   359
  apply (drule cardR_SucD)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   360
  apply (blast intro!: cardR.intros)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   361
  done
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_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
   364
  by (drule cardR_determ_aux1) auto
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: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   367
  apply (induct set: cardR)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   368
   apply (safe elim!: cardR_emptyE cardR_insertE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   369
  apply (rename_tac B b m)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   370
  apply (case_tac "a = b")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   371
   apply (subgoal_tac "A = B")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   372
    prefer 2 apply (blast elim: equalityE, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   373
  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
   374
   prefer 2
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   375
   apply (rule_tac x = "A Int B" in exI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   376
   apply (blast elim: equalityE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   377
  apply (frule_tac A = B in cardR_SucD)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   378
  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   379
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   380
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   381
lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   382
  by (induct set: cardR) simp_all
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   383
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   384
lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   385
  by (induct set: Finites) (auto intro!: cardR.intros)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   386
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   387
lemma card_equality: "(A,n) : cardR ==> card A = n"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   388
  by (unfold card_def) (blast intro: cardR_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   389
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   390
lemma card_empty [simp]: "card {} = 0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   391
  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   392
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   393
lemma card_insert_disjoint [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   394
  "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
   395
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   396
  assume x: "x \<notin> A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   397
  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
   398
    apply (auto intro!: cardR.intros)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   399
    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   400
     apply (force dest: cardR_imp_finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   401
    apply (blast intro!: cardR.intros intro: cardR_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   402
    done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   403
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   404
  thus ?thesis
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   405
    apply (simp add: card_def aux)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   406
    apply (rule the_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   407
     apply (auto intro: finite_imp_cardR
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   408
       cong: conj_cong simp: card_def [symmetric] card_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   409
    done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   410
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   411
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   412
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   413
  apply auto
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   414
  apply (drule_tac a = x in mk_disjoint_insert, clarify)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   415
  apply (rotate_tac -1, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   416
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   417
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   418
lemma card_insert_if:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   419
    "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
   420
  by (simp add: insert_absorb)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   421
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   422
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
14302
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   423
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   424
apply(simp del:insert_Diff_single)
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   425
done
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   426
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   427
lemma card_Diff_singleton:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   428
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   429
  by (simp add: card_Suc_Diff1 [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   430
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   431
lemma card_Diff_singleton_if:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   432
    "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
   433
  by (simp add: card_Diff_singleton)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   434
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   435
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
   436
  by (simp add: card_insert_if card_Suc_Diff1)
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_insert_le: "finite A ==> card A <= card (insert x A)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   439
  by (simp add: card_insert_if)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   440
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   441
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   442
  apply (induct set: Finites, simp, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   443
  apply (subgoal_tac "finite A & A - {x} <= F")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   444
   prefer 2 apply (blast intro: finite_subset, atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   445
  apply (drule_tac x = "A - {x}" in spec)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   446
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   447
  apply (case_tac "card A", auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   448
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   449
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   450
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
   451
  apply (simp add: psubset_def linorder_not_le [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   452
  apply (blast dest: card_seteq)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   453
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   454
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   455
lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   456
  apply (case_tac "A = B", simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   457
  apply (simp add: linorder_not_less [symmetric])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   458
  apply (blast dest: card_seteq intro: order_less_imp_le)
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 card_Un_Int: "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   462
    ==> card A + card B = card (A Un B) + card (A Int B)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   463
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   464
  apply (simp add: insert_absorb Int_insert_left)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   465
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   466
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   467
lemma card_Un_disjoint: "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   468
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   469
  by (simp add: card_Un_Int)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   470
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   471
lemma card_Diff_subset:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   472
    "finite A ==> B <= A ==> card A - card B = card (A - B)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   473
  apply (subgoal_tac "(A - B) Un B = A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   474
   prefer 2 apply blast
14331
8dbbb7cf3637 re-organized numeric lemmas
paulson
parents: 14302
diff changeset
   475
  apply (rule nat_add_right_cancel [THEN iffD1])
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   476
  apply (rule card_Un_disjoint [THEN subst])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   477
     apply (erule_tac [4] ssubst)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   478
     prefer 3 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   479
    apply (simp_all add: add_commute not_less_iff_le
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   480
      add_diff_inverse card_mono finite_subset)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   481
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   482
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   483
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
   484
  apply (rule Suc_less_SucD)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   485
  apply (simp add: card_Suc_Diff1)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   486
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   487
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   488
lemma card_Diff2_less:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   489
    "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
   490
  apply (case_tac "x = y")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   491
   apply (simp add: card_Diff1_less)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   492
  apply (rule less_trans)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   493
   prefer 2 apply (auto intro!: card_Diff1_less)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   494
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   495
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   496
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   497
  apply (case_tac "x : A")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   498
   apply (simp_all add: card_Diff1_less less_imp_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   499
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   500
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   501
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   502
by (erule psubsetI, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   503
14889
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   504
lemma insert_partition:
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   505
     "[| x \<notin> F; \<forall>c1\<in>insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 --> c1 \<inter> c2 = {}|] 
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   506
      ==> x \<inter> \<Union> F = {}"
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   507
by auto
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   508
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   509
(* main cardinality theorem *)
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   510
lemma card_partition [rule_format]:
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   511
     "finite C ==>  
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   512
        finite (\<Union> C) -->  
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   513
        (\<forall>c\<in>C. card c = k) -->   
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   514
        (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->  
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   515
        k * card(C) = card (\<Union> C)"
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   516
apply (erule finite_induct, simp)
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   517
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   518
       finite_subset [of _ "\<Union> (insert x F)"])
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   519
done
d7711d6b9014 moved some cardinality results into main HOL
paulson
parents: 14748
diff changeset
   520
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   521
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   522
subsubsection {* Cardinality of image *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   523
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   524
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   525
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   526
  apply (simp add: le_SucI finite_imageI card_insert_if)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   527
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   528
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   529
lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
15111
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   530
by (induct set: Finites, simp_all)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   531
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   532
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
   533
  by (simp add: card_seteq card_image)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   534
15111
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   535
lemma eq_card_imp_inj_on:
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   536
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   537
apply(induct rule:finite_induct)
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   538
 apply simp
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   539
apply(frule card_image_le[where f = f])
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   540
apply(simp add:card_insert_if split:if_splits)
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   541
done
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   542
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   543
lemma inj_on_iff_eq_card:
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   544
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   545
by(blast intro: card_image eq_card_imp_inj_on)
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   546
12396
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
subsubsection {* Cardinality of the Powerset *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   549
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   550
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
   551
  apply (induct set: Finites)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   552
   apply (simp_all add: Pow_insert)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   553
  apply (subst card_Un_disjoint, blast)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   554
    apply (blast intro: finite_imageI, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   555
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   556
   apply (simp add: card_image Pow_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   557
  apply (unfold inj_on_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   558
  apply (blast elim!: equalityE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   559
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   560
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   561
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   562
  \medskip Relates to equivalence classes.  Based on a theorem of
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   563
  F. Kammüller's.  The @{prop "finite C"} premise is redundant.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   564
*}
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
lemma dvd_partition:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   567
  "finite C ==> finite (Union C) ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   568
    ALL c : C. k dvd card c ==>
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   569
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   570
  k dvd card (Union C)"
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   571
  apply (induct set: Finites, simp_all, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   572
  apply (subst card_Un_disjoint)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   573
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   574
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   575
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   576
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   577
subsection {* A fold functional for finite sets *}
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
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   580
  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
   581
  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
   582
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   583
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   584
consts
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   585
  foldSet :: "('b => 'a => 'a) => 'a => ('b set \<times> 'a) set"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   586
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   587
inductive "foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   588
  intros
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   589
    emptyI [intro]: "({}, e) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   590
    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
   591
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   592
inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f e"
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
constdefs
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   595
  fold :: "('b => 'a => 'a) => 'a => 'b set => 'a"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   596
  "fold f e A == THE x. (A, x) : foldSet f e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   597
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   598
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
   599
by (erule insert_Diff [THEN subst], rule foldSet.intros, auto)
12396
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 foldSet_imp_finite [simp]: "(A, x) : foldSet f e ==> finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   602
  by (induct set: foldSet) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   603
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   604
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
   605
  by (induct set: Finites) auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   606
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   607
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   608
subsubsection {* Left-commutative operations *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   609
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   610
locale LC =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   611
  fixes f :: "'b => 'a => 'a"    (infixl "\<cdot>" 70)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   612
  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
   613
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   614
lemma (in LC) foldSet_determ_aux:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   615
  "ALL A x. card A < n --> (A, x) : foldSet f e -->
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   616
    (ALL y. (A, y) : foldSet f e --> y = x)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   617
  apply (induct n)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   618
   apply (auto simp add: less_Suc_eq)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   619
  apply (erule foldSet.cases, blast)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   620
  apply (erule foldSet.cases, blast, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   621
  txt {* force simplification of @{text "card A < card (insert ...)"}. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   622
  apply (erule rev_mp)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   623
  apply (simp add: less_Suc_eq_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   624
  apply (rule impI)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   625
  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
   626
   apply (subgoal_tac "Aa = Ab")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   627
    prefer 2 apply (blast elim!: equalityE, blast)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   628
  txt {* case @{prop "xa \<notin> xb"}. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   629
  apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb : Aa & xa : Ab")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   630
   prefer 2 apply (blast elim!: equalityE, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   631
  apply (subgoal_tac "Aa = insert xb Ab - {xa}")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   632
   prefer 2 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   633
  apply (subgoal_tac "card Aa <= card Ab")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   634
   prefer 2
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   635
   apply (rule Suc_le_mono [THEN subst])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   636
   apply (simp add: card_Suc_Diff1)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   637
  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
   638
  apply (blast intro: foldSet_imp_finite finite_Diff)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   639
  apply (frule (1) Diff1_foldSet)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   640
  apply (subgoal_tac "ya = f xb x")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   641
   prefer 2 apply (blast del: equalityCE)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   642
  apply (subgoal_tac "(Ab - {xa}, x) : foldSet f e")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   643
   prefer 2 apply simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   644
  apply (subgoal_tac "yb = f xa x")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   645
   prefer 2 apply (blast del: equalityCE dest: Diff1_foldSet)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   646
  apply (simp (no_asm_simp) add: left_commute)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   647
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   648
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   649
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
   650
  by (blast intro: foldSet_determ_aux [rule_format])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   651
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   652
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
   653
  by (unfold fold_def) (blast intro: foldSet_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   654
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   655
lemma fold_empty [simp]: "fold f e {} = e"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   656
  by (unfold fold_def) blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   657
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   658
lemma (in LC) fold_insert_aux: "x \<notin> A ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   659
    ((insert x A, v) : foldSet f e) =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   660
    (EX y. (A, y) : foldSet f e & v = f x y)"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   661
  apply auto
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   662
  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
   663
   apply (fastsimp dest: foldSet_imp_finite)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   664
  apply (blast intro: foldSet_determ)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   665
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   666
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   667
lemma (in LC) fold_insert:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   668
    "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
   669
  apply (unfold fold_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   670
  apply (simp add: fold_insert_aux)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   671
  apply (rule the_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   672
  apply (auto intro: finite_imp_foldSet
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   673
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   674
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   675
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   676
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
   677
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   678
  apply (simp add: left_commute fold_insert)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   679
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   680
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   681
lemma (in LC) fold_nest_Un_Int:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   682
  "finite A ==> finite B
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   683
    ==> 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
   684
  apply (induct set: Finites, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   685
  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
   686
  done
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
lemma (in LC) fold_nest_Un_disjoint:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   689
  "finite A ==> finite B ==> A Int B = {}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   690
    ==> 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
   691
  by (simp add: fold_nest_Un_Int)
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
declare foldSet_imp_finite [simp del]
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   694
    empty_foldSetE [rule del]  foldSet.intros [rule del]
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   695
  -- {* Delete rules to do with @{text foldSet} relation. *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   696
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   697
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   698
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   699
subsubsection {* Commutative monoids *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   700
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   701
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   702
  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
   703
  instead of @{text "'b => 'a => 'a"}.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   704
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   705
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   706
locale ACe =
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   707
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   708
    and e :: 'a
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   709
  assumes ident [simp]: "x \<cdot> e = x"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   710
    and commute: "x \<cdot> y = y \<cdot> x"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   711
    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
   712
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   713
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
   714
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   715
  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
   716
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   717
  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
   718
  finally show ?thesis .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   719
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   720
12718
ade42a6c22ad lemmas (in ACe) AC;
wenzelm
parents: 12693
diff changeset
   721
lemmas (in ACe) AC = assoc commute left_commute
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   722
12693
827818b891c7 qualified exports from locales;
wenzelm
parents: 12396
diff changeset
   723
lemma (in ACe) left_ident [simp]: "e \<cdot> x = x"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   724
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   725
  have "x \<cdot> e = x" by (rule ident)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   726
  thus ?thesis by (subst commute)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   727
qed
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_Int:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   730
  "finite A ==> finite B ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   731
    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
   732
  apply (induct set: Finites, simp)
13400
dbb608cd11c4 accomodate cumulative locale predicates;
wenzelm
parents: 13390
diff changeset
   733
  apply (simp add: AC insert_absorb Int_insert_left
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
   734
    LC.fold_insert [OF LC.intro])
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   735
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   736
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   737
lemma (in ACe) fold_Un_disjoint:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   738
  "finite A ==> finite B ==> A Int B = {} ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   739
    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
   740
  by (simp add: fold_Un_Int)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   741
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   742
lemma (in ACe) fold_Un_disjoint2:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   743
  "finite A ==> finite B ==> A Int B = {} ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   744
    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
   745
proof -
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   746
  assume b: "finite B"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   747
  assume "finite A"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   748
  thus "A Int B = {} ==>
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   749
    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
   750
  proof induct
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   751
    case empty
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   752
    thus ?case by simp
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   753
  next
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   754
    case (insert F x)
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   755
    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
   756
      by simp
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   757
    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
   758
      by (rule LC.fold_insert [OF LC.intro])
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
   759
        (insert b insert, auto simp add: left_commute)
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   760
    also from insert have "fold (f o g) e (F \<union> B) =
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   761
      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
   762
    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
   763
      by (simp add: AC)
13571
d76a798281f4 less use of x-symbols
paulson
parents: 13490
diff changeset
   764
    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
   765
      by (rule LC.fold_insert [OF LC.intro, symmetric]) (insert b insert,
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
   766
        auto simp add: left_commute)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   767
    finally show ?case .
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   768
  qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   769
qed
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   770
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
subsection {* Generalized summation over a set *}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   773
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   774
constdefs
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   775
  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   776
  "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
   777
15042
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   778
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   779
written @{text"\<Sum>x\<in>A. e"}. *}
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   780
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   781
syntax
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   782
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   783
syntax (xsymbols)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   784
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
14565
c6dc17aab88a use more symbols in HTML output
kleing
parents: 14504
diff changeset
   785
syntax (HTML output)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   786
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   787
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   788
translations -- {* Beware of argument permutation! *}
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   789
  "SUM i:A. b" == "setsum (%i. b) A"
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   790
  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   791
15042
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   792
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   793
 @{text"\<Sum>x|P. e"}. *}
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   794
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   795
syntax
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   796
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
15042
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   797
syntax (xsymbols)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   798
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   799
syntax (HTML output)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   800
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   801
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   802
translations
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   803
  "SUM x|P. t" => "setsum (%x. t) {x. P}"
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   804
  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
15042
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   805
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   806
print_translation {*
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   807
let
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   808
  fun setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   809
    (if x<>y then raise Match
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   810
     else let val x' = Syntax.mark_bound x
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   811
              val t' = subst_bound(x',t)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   812
              val P' = subst_bound(x',P)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   813
          in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end)
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   814
in
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   815
[("setsum", setsum_tr')]
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   816
end
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   817
*}
fa7d27ef7e59 added {0::nat..n(} = {..n(}
nipkow
parents: 15004
diff changeset
   818
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
   819
text{* As Jeremy Avigad notes, setprod needs the same treatment \dots *}
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   820
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   821
lemma setsum_empty [simp]: "setsum f {} = 0"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   822
  by (simp add: setsum_def)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   823
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   824
lemma setsum_insert [simp]:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   825
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
   826
  by (simp add: setsum_def LC.fold_insert [OF LC.intro] add_left_commute)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   827
14944
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   828
lemma setsum_reindex [rule_format]:
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   829
     "finite B ==> inj_on f B --> setsum h (f ` B) = setsum (h \<circ> f) B"
15111
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
   830
by (rule finite_induct, auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   831
14944
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   832
lemma setsum_reindex_id:
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   833
     "finite B ==> inj_on f B ==> setsum f B = setsum id (f ` B)"
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   834
by (auto simp add: setsum_reindex id_o)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   835
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   836
lemma setsum_cong:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   837
  "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
   838
  apply (case_tac "finite B")
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   839
   prefer 2 apply (simp add: setsum_def, simp)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   840
  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
   841
   apply simp
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   842
  apply (erule finite_induct, simp)
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
   843
  apply (simp add: subset_insert_iff, clarify)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   844
  apply (subgoal_tac "finite C")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   845
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   846
  apply (subgoal_tac "C = insert x (C - {x})")
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   847
   prefer 2 apply blast
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   848
  apply (erule ssubst)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   849
  apply (drule spec)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   850
  apply (erule (1) notE impE)
14302
6c24235e8d5d *** empty log message ***
nipkow
parents: 14208
diff changeset
   851
  apply (simp add: Ball_def del:insert_Diff_single)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   852
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
   853
14944
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   854
lemma setsum_reindex_cong:
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   855
     "[|finite A; inj_on f A; B = f ` A; !!a. g a = h (f a)|] 
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   856
      ==> setsum h B = setsum g A"
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   857
  by (simp add: setsum_reindex cong: setsum_cong) 
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
   858
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   859
lemma setsum_0: "setsum (%i. 0) A = 0"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   860
  apply (case_tac "finite A")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   861
   prefer 2 apply (simp add: setsum_def)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   862
  apply (erule finite_induct, auto)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   863
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   864
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   865
lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   866
  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   867
  apply (erule ssubst, rule setsum_0)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   868
  apply (rule setsum_cong, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   869
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   870
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   871
lemma card_eq_setsum: "finite A ==> card A = setsum (%x. 1) A"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   872
  -- {* Could allow many @{text "card"} proofs to be simplified. *}
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   873
  by (induct set: Finites) auto
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   874
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   875
lemma setsum_Un_Int: "finite A ==> finite B
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   876
    ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   877
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   878
  apply (induct set: Finites, simp)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   879
  apply (simp add: add_ac Int_insert_left insert_absorb)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   880
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   881
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   882
lemma setsum_Un_disjoint: "finite A ==> finite B
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   883
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   884
  apply (subst setsum_Un_Int [symmetric], auto)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   885
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   886
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   887
lemma setsum_UN_disjoint:
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   888
    "finite I ==> (ALL i:I. finite (A i)) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   889
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   890
      setsum f (UNION I A) = setsum (%i. setsum f (A i)) I"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   891
  apply (induct set: Finites, simp, atomize)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   892
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   893
   prefer 2 apply blast
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   894
  apply (subgoal_tac "A x Int UNION F A = {}")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   895
   prefer 2 apply blast
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   896
  apply (simp add: setsum_Un_disjoint)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   897
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   898
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   899
lemma setsum_Union_disjoint:
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   900
  "finite C ==> (ALL A:C. finite A) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   901
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   902
      setsum f (Union C) = setsum (setsum f) C"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   903
  apply (frule setsum_UN_disjoint [of C id f])
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   904
  apply (unfold Union_def id_def, assumption+)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   905
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   906
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
   907
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   908
    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   909
    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   910
  apply (subst Sigma_def)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   911
  apply (subst setsum_UN_disjoint)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   912
  apply assumption
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   913
  apply (rule ballI)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   914
  apply (drule_tac x = i in bspec, assumption)
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
   915
  apply (subgoal_tac "(UN y:(B i). {(i, y)}) <= (%y. (i, y)) ` (B i)")
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   916
  apply (rule finite_surj)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   917
  apply auto
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   918
  apply (rule setsum_cong, rule refl)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   919
  apply (subst setsum_UN_disjoint)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   920
  apply (erule bspec, assumption)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   921
  apply auto
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   922
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   923
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   924
lemma setsum_cartesian_product: "finite A ==> finite B ==>
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   925
    (\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) =
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   926
    (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   927
  by (erule setsum_Sigma, auto);
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   928
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   929
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   930
  apply (case_tac "finite A")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   931
   prefer 2 apply (simp add: setsum_def)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   932
  apply (erule finite_induct, auto)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   933
  apply (simp add: add_ac)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   934
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   935
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   936
subsubsection {* Properties in more restricted classes of structures *}
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   937
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   938
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   939
  apply (case_tac "finite A")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   940
   prefer 2 apply (simp add: setsum_def)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   941
  apply (erule rev_mp)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   942
  apply (erule finite_induct, auto)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   943
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
   944
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   945
lemma setsum_eq_0_iff [simp]:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   946
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   947
  by (induct set: Finites) auto
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   948
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
   949
lemma setsum_constant_nat:
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
   950
    "finite A ==> (\<Sum>x\<in>A. y) = (card A) * y"
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
   951
  -- {* Generalized to any @{text comm_semiring_1_cancel} in
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
   952
        @{text IntDef} as @{text setsum_constant}. *}
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   953
  by (erule finite_induct, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   954
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   955
lemma setsum_Un: "finite A ==> finite B ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   956
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   957
  -- {* For the natural numbers, we have subtraction. *}
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   958
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   959
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   960
lemma setsum_Un_ring: "finite A ==> finite B ==>
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   961
    (setsum f (A Un B) :: 'a :: comm_ring_1) =
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   962
      setsum f A + setsum f B - setsum f (A Int B)"
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
   963
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   964
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   965
lemma setsum_diff1: "(setsum f (A - {a}) :: nat) =
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   966
    (if a:A then setsum f A - f a else setsum f A)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   967
  apply (case_tac "finite A")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   968
   prefer 2 apply (simp add: setsum_def)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   969
  apply (erule finite_induct)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   970
   apply (auto simp add: insert_Diff_if)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   971
  apply (drule_tac a = a in mk_disjoint_insert, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   972
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
   973
15124
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   974
(* By Jeremy Siek: *)
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   975
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   976
lemma setsum_diff: 
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   977
  assumes finB: "finite B"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   978
  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   979
using finB
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   980
proof (induct)
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   981
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   982
next
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   983
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   984
    and xFinA: "insert x F \<subseteq> A"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   985
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   986
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   987
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   988
    by (simp add: setsum_diff1)
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   989
  from xFinA have "F \<subseteq> A" by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   990
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   991
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   992
    by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   993
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   994
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   995
    by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   996
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   997
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   998
    by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
   999
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
  1000
qed
1d9b4fcd222d Aded a thm.
nipkow
parents: 15111
diff changeset
  1001
15311
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1002
lemma setsum_mono:
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1003
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1004
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1005
proof (cases "finite K")
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1006
  case True
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1007
  thus ?thesis using le
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1008
  proof (induct)
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1009
    case empty
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1010
    thus ?case by simp
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1011
  next
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1012
    case insert
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1013
    thus ?case using add_mono 
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1014
      by force
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1015
  qed
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1016
next
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1017
  case False
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1018
  thus ?thesis
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1019
    by (simp add: setsum_def)
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1020
qed
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1021
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1022
lemma finite_setsum_diff1: "finite A \<Longrightarrow> (setsum f (A - {a}) :: ('a::{pordered_ab_group_add})) =
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1023
    (if a:A then setsum f A - f a else setsum f A)"
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1024
  by (erule finite_induct) (auto simp add: insert_Diff_if)
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1025
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1026
lemma finite_setsum_diff:
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1027
  assumes le: "finite A" "B \<subseteq> A"
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1028
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::pordered_ab_group_add))"
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1029
proof -
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1030
  from le have finiteB: "finite B" using finite_subset by auto
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1031
  show ?thesis using le finiteB
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1032
    proof (induct rule: Finites.induct[OF finiteB])
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1033
      case 1
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1034
      thus ?case by auto
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1035
    next
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1036
      case 2
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1037
      thus ?case using le 
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1038
	apply auto
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1039
	apply (subst Diff_insert)
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1040
	apply (subst finite_setsum_diff1)
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1041
	apply (auto simp add: insert_absorb)
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1042
	done
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1043
    qed
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1044
  qed
2ca1c66a6758 Added lemmas setsum_mono, finite_setsum_diff1, finite_setsum_diff
obua
parents: 15309
diff changeset
  1045
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1046
lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::comm_ring_1) A =
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1047
  - setsum f A"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1048
  by (induct set: Finites, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1049
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1050
lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::comm_ring_1) - g x) A =
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1051
  setsum f A - setsum g A"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1052
  by (simp add: diff_minus setsum_addf setsum_negf)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1053
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1054
lemma setsum_nonneg: "[| finite A;
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1055
    \<forall>x \<in> A. (0::'a::ordered_semidom) \<le> f x |] ==>
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1056
    0 \<le>  setsum f A";
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1057
  apply (induct set: Finites, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1058
  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1059
  apply (blast intro: add_mono)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1060
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1061
15308
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1062
lemma setsum_nonpos: "[| finite A;
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1063
    \<forall>x \<in> A. f x \<le> (0::'a::ordered_semidom) |] ==>
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1064
    setsum f A \<le> 0";
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1065
  apply (induct set: Finites, auto)
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1066
  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1067
  apply (blast intro: add_mono)
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1068
  done
56c830f91b38 added lemma
nipkow
parents: 15281
diff changeset
  1069
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1070
lemma setsum_mult: 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1071
  fixes f :: "'a => ('b::semiring_0_cancel)"
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1072
  shows "r * setsum f A = setsum (%n. r * f n) A"
15309
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1073
proof (cases "finite A")
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1074
  case True
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1075
  thus ?thesis
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1076
  proof (induct)
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1077
    case empty thus ?case by simp
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1078
  next
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1079
    case (insert A x) thus ?case by (simp add: right_distrib)
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1080
  qed
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1081
next
15309
173669c88fd2 generalized lemma
nipkow
parents: 15308
diff changeset
  1082
  case False thus ?thesis by (simp add: setsum_def)
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1083
qed
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1084
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1085
lemma setsum_abs: 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1086
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1087
  assumes fin: "finite A" 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1088
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1089
using fin 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1090
proof (induct) 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1091
  case empty thus ?case by simp
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1092
next
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1093
  case (insert A x)
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1094
  thus ?case by (auto intro: abs_triangle_ineq order_trans)
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1095
qed
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1096
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1097
lemma setsum_abs_ge_zero: 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1098
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1099
  assumes fin: "finite A" 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1100
  shows "0 \<le> setsum (%i. abs(f i)) A"
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1101
using fin 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1102
proof (induct) 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1103
  case empty thus ?case by simp
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1104
next
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1105
  case (insert A x) thus ?case by (auto intro: order_trans)
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1106
qed
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1107
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1108
subsubsection {* Cardinality of unions and Sigma sets *}
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1109
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1110
lemma card_UN_disjoint:
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1111
    "finite I ==> (ALL i:I. finite (A i)) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1112
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1113
      card (UNION I A) = setsum (%i. card (A i)) I"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1114
  apply (subst card_eq_setsum)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1115
  apply (subst finite_UN, assumption+)
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1116
  apply (subgoal_tac
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1117
           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1118
  apply (simp add: setsum_UN_disjoint) 
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1119
  apply (simp add: setsum_constant_nat cong: setsum_cong) 
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1120
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1121
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1122
lemma card_Union_disjoint:
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1123
  "finite C ==> (ALL A:C. finite A) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1124
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1125
      card (Union C) = setsum card C"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1126
  apply (frule card_UN_disjoint [of C id])
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1127
  apply (unfold Union_def id_def, assumption+)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1128
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1129
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1130
lemma SigmaI_insert: "y \<notin> A ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1131
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1132
  by auto
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1133
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1134
lemma card_cartesian_product_singleton: "finite A ==>
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1135
    card({x} <*> A) = card(A)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1136
  apply (subgoal_tac "inj_on (%y .(x,y)) A")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1137
  apply (frule card_image, assumption)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1138
  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1139
  apply (auto simp add: inj_on_def)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1140
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1141
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1142
lemma card_SigmaI [rule_format,simp]: "finite A ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1143
  (ALL a:A. finite (B a)) -->
15074
277b3a4da341 Modified \<Sum> syntax a little.
nipkow
parents: 15047
diff changeset
  1144
  card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1145
  apply (erule finite_induct, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1146
  apply (subst SigmaI_insert, assumption)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1147
  apply (subst card_Un_disjoint)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1148
  apply (auto intro: finite_SigmaI simp add: card_cartesian_product_singleton)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1149
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1150
15047
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1151
lemma card_cartesian_product:
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1152
     "[| finite A; finite B |] ==> card (A <*> B) = card(A) * card(B)"
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1153
  by (simp add: setsum_constant_nat)
fa62de5862b9 redefining sumr to be a translation to setsum
paulson
parents: 15042
diff changeset
  1154
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1155
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1156
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1157
subsection {* Generalized product over a set *}
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1158
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1159
constdefs
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1160
  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1161
  "setprod f A == if finite A then fold (op * o f) 1 A else 1"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1162
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1163
syntax
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1164
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1165
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1166
syntax (xsymbols)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1167
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
14565
c6dc17aab88a use more symbols in HTML output
kleing
parents: 14504
diff changeset
  1168
syntax (HTML output)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1169
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1170
translations
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1171
  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1172
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1173
lemma setprod_empty [simp]: "setprod f {} = 1"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1174
  by (auto simp add: setprod_def)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1175
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1176
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1177
    setprod f (insert a A) = f a * setprod f A"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1178
  by (auto simp add: setprod_def LC_def LC.fold_insert
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1179
      mult_left_commute)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1180
14748
001323d6d75b removed a premise of card_inj_on_le
paulson
parents: 14740
diff changeset
  1181
lemma setprod_reindex [rule_format]:
001323d6d75b removed a premise of card_inj_on_le
paulson
parents: 14740
diff changeset
  1182
     "finite B ==> inj_on f B --> setprod h (f ` B) = setprod (h \<circ> f) B"
15111
c108189645f8 added some inj_on thms
nipkow
parents: 15074
diff changeset
  1183
by (rule finite_induct, auto)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1184
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1185
lemma setprod_reindex_id: "finite B ==> inj_on f B ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1186
    setprod f B = setprod id (f ` B)"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1187
by (auto simp add: setprod_reindex id_o)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1188
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1189
lemma setprod_reindex_cong: "finite A ==> inj_on f A ==>
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1190
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1191
  by (frule setprod_reindex, assumption, simp)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1192
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1193
lemma setprod_cong:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1194
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1195
  apply (case_tac "finite B")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1196
   prefer 2 apply (simp add: setprod_def, simp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1197
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setprod f C = setprod g C")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1198
   apply simp
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1199
  apply (erule finite_induct, simp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1200
  apply (simp add: subset_insert_iff, clarify)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1201
  apply (subgoal_tac "finite C")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1202
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1203
  apply (subgoal_tac "C = insert x (C - {x})")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1204
   prefer 2 apply blast
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1205
  apply (erule ssubst)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1206
  apply (drule spec)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1207
  apply (erule (1) notE impE)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1208
  apply (simp add: Ball_def del:insert_Diff_single)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1209
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1210
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1211
lemma setprod_1: "setprod (%i. 1) A = 1"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1212
  apply (case_tac "finite A")
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1213
  apply (erule finite_induct, auto simp add: mult_ac)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1214
  apply (simp add: setprod_def)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1215
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1216
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1217
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1218
  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1219
  apply (erule ssubst, rule setprod_1)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1220
  apply (rule setprod_cong, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1221
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1222
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1223
lemma setprod_Un_Int: "finite A ==> finite B
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1224
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1225
  apply (induct set: Finites, simp)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1226
  apply (simp add: mult_ac insert_absorb)
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1227
  apply (simp add: mult_ac Int_insert_left insert_absorb)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1228
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1229
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1230
lemma setprod_Un_disjoint: "finite A ==> finite B
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1231
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1232
  apply (subst setprod_Un_Int [symmetric], auto simp add: mult_ac)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1233
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1234
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1235
lemma setprod_UN_disjoint:
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1236
    "finite I ==> (ALL i:I. finite (A i)) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1237
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1238
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1239
  apply (induct set: Finites, simp, atomize)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1240
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1241
   prefer 2 apply blast
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1242
  apply (subgoal_tac "A x Int UNION F A = {}")
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1243
   prefer 2 apply blast
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1244
  apply (simp add: setprod_Un_disjoint)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1245
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1246
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1247
lemma setprod_Union_disjoint:
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1248
  "finite C ==> (ALL A:C. finite A) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1249
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1250
      setprod f (Union C) = setprod (setprod f) C"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1251
  apply (frule setprod_UN_disjoint [of C id f])
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1252
  apply (unfold Union_def id_def, assumption+)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1253
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1254
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1255
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1256
    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1257
    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1258
  apply (subst Sigma_def)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1259
  apply (subst setprod_UN_disjoint)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1260
  apply assumption
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1261
  apply (rule ballI)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1262
  apply (drule_tac x = i in bspec, assumption)
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1263
  apply (subgoal_tac "(UN y:(B i). {(i, y)}) <= (%y. (i, y)) ` (B i)")
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1264
  apply (rule finite_surj)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1265
  apply auto
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1266
  apply (rule setprod_cong, rule refl)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1267
  apply (subst setprod_UN_disjoint)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1268
  apply (erule bspec, assumption)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1269
  apply auto
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1270
  done
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1271
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1272
lemma setprod_cartesian_product: "finite A ==> finite B ==>
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1273
    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1274
    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1275
  by (erule setprod_Sigma, auto)
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1276
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1277
lemma setprod_timesf: "setprod (%x. f x * g x) A =
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1278
    (setprod f A * setprod g A)"
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1279
  apply (case_tac "finite A")
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1280
   prefer 2 apply (simp add: setprod_def mult_ac)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1281
  apply (erule finite_induct, auto)
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1282
  apply (simp add: mult_ac)
14485
ea2707645af8 new material from Avigad
paulson
parents: 14449
diff changeset
  1283
  done
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1284
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1285
subsubsection {* Properties in more restricted classes of structures *}
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1286
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1287
lemma setprod_eq_1_iff [simp]:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1288
    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1289
  by (induct set: Finites) auto
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1290
15004
44ac09ba7213 ringpower to recpower
paulson
parents: 14944
diff changeset
  1291
lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1292
  apply (erule finite_induct)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1293
  apply (auto simp add: power_Suc)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1294
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1295
15004
44ac09ba7213 ringpower to recpower
paulson
parents: 14944
diff changeset
  1296
lemma setprod_zero:
44ac09ba7213 ringpower to recpower
paulson
parents: 14944
diff changeset
  1297
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1298
  apply (induct set: Finites, force, clarsimp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1299
  apply (erule disjE, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1300
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1301
15004
44ac09ba7213 ringpower to recpower
paulson
parents: 14944
diff changeset
  1302
lemma setprod_nonneg [rule_format]:
44ac09ba7213 ringpower to recpower
paulson
parents: 14944
diff changeset
  1303
     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1304
  apply (case_tac "finite A")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1305
  apply (induct set: Finites, force, clarsimp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1306
  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1307
  apply (rule mult_mono, assumption+)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1308
  apply (auto simp add: setprod_def)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1309
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1310
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1311
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1312
     --> 0 < setprod f A"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1313
  apply (case_tac "finite A")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1314
  apply (induct set: Finites, force, clarsimp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1315
  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1316
  apply (rule mult_strict_mono, assumption+)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1317
  apply (auto simp add: setprod_def)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1318
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1319
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1320
lemma setprod_nonzero [rule_format]:
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1321
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1322
      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1323
  apply (erule finite_induct, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1324
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1325
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1326
lemma setprod_zero_eq:
14738
83f1a514dcb4 changes made due to new Ring_and_Field theory
obua
parents: 14661
diff changeset
  1327
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1328
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1329
  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1330
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1331
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1332
lemma setprod_nonzero_field:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1333
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1334
  apply (rule setprod_nonzero, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1335
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1336
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1337
lemma setprod_zero_eq_field:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1338
    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1339
  apply (rule setprod_zero_eq, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1340
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1341
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1342
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1343
    (setprod f (A Un B) :: 'a ::{field})
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1344
      = setprod f A * setprod f B / setprod f (A Int B)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1345
  apply (subst setprod_Un_Int [symmetric], auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1346
  apply (subgoal_tac "finite (A Int B)")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1347
  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
15228
4d332d10fa3d revised simprules for division
paulson
parents: 15140
diff changeset
  1348
  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1349
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1350
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1351
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1352
    (setprod f (A - {a}) :: 'a :: {field}) =
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1353
      (if a:A then setprod f A / f a else setprod f A)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1354
  apply (erule finite_induct)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1355
   apply (auto simp add: insert_Diff_if)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1356
  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1357
  apply (erule ssubst)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1358
  apply (subst times_divide_eq_right [THEN sym])
15234
ec91a90c604e simplification tweaks for better arithmetic reasoning
paulson
parents: 15228
diff changeset
  1359
  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1360
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1361
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1362
lemma setprod_inversef: "finite A ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1363
    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1364
      setprod (inverse \<circ> f) A = inverse (setprod f A)"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1365
  apply (erule finite_induct)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1366
  apply (simp, simp)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1367
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1368
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1369
lemma setprod_dividef:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1370
     "[|finite A;
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1371
        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1372
      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1373
  apply (subgoal_tac
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1374
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1375
  apply (erule ssubst)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1376
  apply (subst divide_inverse)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1377
  apply (subst setprod_timesf)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1378
  apply (subst setprod_inversef, assumption+, rule refl)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1379
  apply (rule setprod_cong, rule refl)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1380
  apply (subst divide_inverse, auto)
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1381
  done
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1382
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1383
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1384
subsection{* Min and Max of finite linearly ordered sets *}
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1385
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1386
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
  1387
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1388
lemma ex_Max: fixes S :: "('a::linorder)set"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1389
  assumes fin: "finite S" shows "S \<noteq> {} ==> \<exists>m\<in>S. \<forall>s \<in> S. s \<le> m"
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1390
using fin
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1391
proof (induct)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1392
  case empty thus ?case by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1393
next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1394
  case (insert S x)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1395
  show ?case
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1396
  proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1397
    assume "S = {}" thus ?thesis by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1398
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1399
    assume nonempty: "S \<noteq> {}"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1400
    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
  1401
    show ?thesis
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1402
    proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1403
      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
  1404
    next
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1405
      assume "~ x \<le> m" thus ?thesis using m
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1406
        by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1407
    qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1408
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1409
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1410
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1411
lemma ex_Min: fixes S :: "('a::linorder)set"
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1412
  assumes fin: "finite S" shows "S \<noteq> {} ==> \<exists>m\<in>S. \<forall>s \<in> S. m \<le> s"
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1413
using fin
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1414
proof (induct)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1415
  case empty thus ?case by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1416
next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1417
  case (insert S x)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1418
  show ?case
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1419
  proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1420
    assume "S = {}" thus ?thesis by simp
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1421
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1422
    assume nonempty: "S \<noteq> {}"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1423
    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
  1424
    show ?thesis
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1425
    proof (cases)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1426
      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
  1427
    next
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1428
      assume "~ m \<le> x" thus ?thesis using m
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1429
        by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1430
    qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1431
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1432
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1433
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1434
constdefs
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1435
  Min :: "('a::linorder)set => 'a"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1436
  "Min S  ==  THE m. m \<in> S \<and> (\<forall>s \<in> S. m \<le> s)"
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1437
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1438
  Max :: "('a::linorder)set => 'a"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1439
  "Max S  ==  THE m. m \<in> S \<and> (\<forall>s \<in> S. s \<le> m)"
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1440
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1441
lemma Min [simp]:
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1442
  assumes a: "finite S"  "S \<noteq> {}"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1443
  shows "Min S \<in> S \<and> (\<forall>s \<in> S. Min S \<le> s)" (is "?P(Min S)")
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1444
proof (unfold Min_def, rule theI')
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1445
  show "\<exists>!m. ?P m"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1446
  proof (rule ex_ex1I)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1447
    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
  1448
  next
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1449
    fix m1 m2 assume "?P m1" and "?P m2"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1450
    thus "m1 = m2" by (blast dest: order_antisym)
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1451
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1452
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1453
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1454
lemma Max [simp]:
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1455
  assumes a: "finite S"  "S \<noteq> {}"
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1456
  shows "Max S \<in> S \<and> (\<forall>s \<in> S. s \<le> Max S)" (is "?P(Max S)")
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1457
proof (unfold Max_def, rule theI')
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1458
  show "\<exists>!m. ?P m"
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1459
  proof (rule ex_ex1I)
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1460
    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
  1461
  next
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1462
    fix m1 m2 assume "?P m1" "?P m2"
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1463
    thus "m1 = m2" by (blast dest: order_antisym)
13490
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1464
  qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1465
qed
44bdc150211b Added Mi and Max on sets, hid Min and Pls on numerals.
nipkow
parents: 13421
diff changeset
  1466
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1467
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1468
subsection {* Theorems about @{text "choose"} *}
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1469
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1470
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1471
  \medskip Basic theorem about @{text "choose"}.  By Florian
14661
9ead82084de8 tuned notation;
wenzelm
parents: 14565
diff changeset
  1472
  Kamm\"uller, tidied by LCP.
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1473
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1474
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1475
lemma card_s_0_eq_empty:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1476
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1477
  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
  1478
  apply (simp cong add: rev_conj_cong)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1479
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1480
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1481
lemma choose_deconstruct: "finite M ==> x \<notin> M
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1482
  ==> {s. s <= insert x M & card(s) = Suc k}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1483
       = {s. s <= M & card(s) = Suc k} Un
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1484
         {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
  1485
  apply safe
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1486
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1487
  apply (drule_tac x = "xa - {x}" in spec)
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1488
  apply (subgoal_tac "x \<notin> xa", auto)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1489
  apply (erule rev_mp, subst card_Diff_singleton)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1490
  apply (auto intro: finite_subset)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1491
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1492
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1493
lemma card_inj_on_le:
14748
001323d6d75b removed a premise of card_inj_on_le
paulson
parents: 14740
diff changeset
  1494
    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
001323d6d75b removed a premise of card_inj_on_le
paulson
parents: 14740
diff changeset
  1495
apply (subgoal_tac "finite A") 
001323d6d75b removed a premise of card_inj_on_le
paulson
parents: 14740
diff changeset
  1496
 apply (force intro: card_mono simp add: card_image [symmetric])
14944
efbaecbdc05c strengthened some theorems
paulson
parents: 14889
diff changeset
  1497
apply (blast intro: finite_imageD dest: finite_subset) 
14748
001323d6d75b removed a premise of card_inj_on_le
paulson
parents: 14740
diff changeset
  1498
done
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1499
14430
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1500
lemma card_bij_eq:
5cb24165a2e1 new material from Avigad, and simplified treatment of division by 0
paulson
parents: 14331
diff changeset
  1501
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1502
       finite A; finite B |] ==> card A = card B"
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1503
  by (auto intro: le_anti_sym card_inj_on_le)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1504
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1505
text{*There are as many subsets of @{term A} having cardinality @{term k}
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1506
 as there are sets obtained from the former by inserting a fixed element
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1507
 @{term x} into each.*}
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1508
lemma constr_bij:
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1509
   "[|finite A; x \<notin> A|] ==>
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1510
    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
  1511
    card {B. B <= A & card(B) = k}"
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1512
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
13595
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1513
       apply (auto elim!: equalityE simp add: inj_on_def)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1514
    apply (subst Diff_insert0, auto)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1515
   txt {* finiteness of the two sets *}
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1516
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1517
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
7e6cdcd113a2 Proof tidying
paulson
parents: 13571
diff changeset
  1518
   apply fast+
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1519
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1520
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1521
text {*
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1522
  Main theorem: combinatorial statement about number of subsets of a set.
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1523
*}
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1524
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1525
lemma n_sub_lemma:
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1526
  "!!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
  1527
  apply (induct k)
14208
144f45277d5a misc tidying
paulson
parents: 13825
diff changeset
  1528
   apply (simp add: card_s_0_eq_empty, atomize)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1529
  apply (rotate_tac -1, erule finite_induct)
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1530
   apply (simp_all (no_asm_simp) cong add: conj_cong
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1531
     add: card_s_0_eq_empty choose_deconstruct)
12396
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1532
  apply (subst card_Un_disjoint)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1533
     prefer 4 apply (force simp add: constr_bij)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1534
    prefer 3 apply force
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1535
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1536
     finite_subset [of _ "Pow (insert x F)", standard])
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1537
  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
  1538
  done
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1539
13421
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1540
theorem n_subsets:
8fcdf4a26468 simplified locale predicates;
wenzelm
parents: 13400
diff changeset
  1541
    "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
  1542
  by (simp add: n_sub_lemma)
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1543
2298d5b8e530 renamed theory Finite to Finite_Set and converted;
wenzelm
parents:
diff changeset
  1544
end