src/HOL/NumberTheory/Finite2.thy
changeset 18369 694ea14ab4f2
parent 15402 97204f3b4705
child 19670 2e4a143c73c5
equal deleted inserted replaced
18368:2f9b2539c5bb 18369:694ea14ab4f2
    21 (*                                                                *)
    21 (*                                                                *)
    22 (******************************************************************)
    22 (******************************************************************)
    23 
    23 
    24 subsection {* Useful properties of sums and products *}
    24 subsection {* Useful properties of sums and products *}
    25 
    25 
    26 lemma setsum_same_function_zcong: 
    26 lemma setsum_same_function_zcong:
    27 assumes a: "\<forall>x \<in> S. [f x = g x](mod m)"
    27 assumes a: "\<forall>x \<in> S. [f x = g x](mod m)"
    28 shows "[setsum f S = setsum g S] (mod m)"
    28 shows "[setsum f S = setsum g S] (mod m)"
    29 proof cases
    29 proof cases
    30   assume "finite S"
    30   assume "finite S"
    31   thus ?thesis using a by induct (simp_all add: zcong_zadd)
    31   thus ?thesis using a by induct (simp_all add: zcong_zadd)
    46 lemma setsum_const: "finite X ==> setsum (%x. (c :: int)) X = c * int(card X)"
    46 lemma setsum_const: "finite X ==> setsum (%x. (c :: int)) X = c * int(card X)"
    47   apply (induct set: Finites)
    47   apply (induct set: Finites)
    48   apply (auto simp add: left_distrib right_distrib int_eq_of_nat)
    48   apply (auto simp add: left_distrib right_distrib int_eq_of_nat)
    49   done
    49   done
    50 
    50 
    51 lemma setsum_const2: "finite X ==> int (setsum (%x. (c :: nat)) X) = 
    51 lemma setsum_const2: "finite X ==> int (setsum (%x. (c :: nat)) X) =
    52     int(c) * int(card X)"
    52     int(c) * int(card X)"
    53   apply (induct set: Finites)
    53   apply (induct set: Finites)
    54   apply (auto simp add: zadd_zmult_distrib2)
    54   apply (auto simp add: zadd_zmult_distrib2)
    55 done
    55   done
    56 
    56 
    57 lemma setsum_const_mult: "finite A ==> setsum (%x. c * ((f x)::int)) A = 
    57 lemma setsum_const_mult: "finite A ==> setsum (%x. c * ((f x)::int)) A =
    58     c * setsum f A"
    58     c * setsum f A"
    59   apply (induct set: Finites, auto)
    59   by (induct set: Finites) (auto simp add: zadd_zmult_distrib2)
    60   by (auto simp only: zadd_zmult_distrib2)
    60 
    61 
    61 
    62 (******************************************************************)
    62 (******************************************************************)
    63 (*                                                                *)
    63 (*                                                                *)
    64 (* Cardinality of some explicit finite sets                       *)
    64 (* Cardinality of some explicit finite sets                       *)
    65 (*                                                                *)
    65 (*                                                                *)
    66 (******************************************************************)
    66 (******************************************************************)
    67 
    67 
    68 subsection {* Cardinality of explicit finite sets *}
    68 subsection {* Cardinality of explicit finite sets *}
    69 
    69 
    70 lemma finite_surjI: "[| B \<subseteq> f ` A; finite A |] ==> finite B"
    70 lemma finite_surjI: "[| B \<subseteq> f ` A; finite A |] ==> finite B"
    71 by (simp add: finite_subset finite_imageI)
    71   by (simp add: finite_subset finite_imageI)
    72 
    72 
    73 lemma bdd_nat_set_l_finite: "finite { y::nat . y < x}"
    73 lemma bdd_nat_set_l_finite: "finite {y::nat . y < x}"
    74 apply (rule_tac N = "{y. y < x}" and n = x in bounded_nat_set_is_finite)
    74   by (rule bounded_nat_set_is_finite) blast
    75 by auto
    75 
    76 
    76 lemma bdd_nat_set_le_finite: "finite {y::nat . y \<le> x}"
    77 lemma bdd_nat_set_le_finite: "finite { y::nat . y \<le> x  }"
    77 proof -
    78 apply (subgoal_tac "{ y::nat . y \<le> x  } = { y::nat . y < Suc x}")
    78   have "{y::nat . y \<le> x} = {y::nat . y < Suc x}" by auto
    79 by (auto simp add: bdd_nat_set_l_finite)
    79   then show ?thesis by (auto simp add: bdd_nat_set_l_finite)
    80 
    80 qed
    81 lemma  bdd_int_set_l_finite: "finite { x::int . 0 \<le> x & x < n}"
    81 
    82 apply (subgoal_tac " {(x :: int). 0 \<le> x & x < n} \<subseteq> 
    82 lemma  bdd_int_set_l_finite: "finite {x::int. 0 \<le> x & x < n}"
       
    83 apply (subgoal_tac " {(x :: int). 0 \<le> x & x < n} \<subseteq>
    83     int ` {(x :: nat). x < nat n}")
    84     int ` {(x :: nat). x < nat n}")
    84 apply (erule finite_surjI)
    85 apply (erule finite_surjI)
    85 apply (auto simp add: bdd_nat_set_l_finite image_def)
    86 apply (auto simp add: bdd_nat_set_l_finite image_def)
    86 apply (rule_tac x = "nat x" in exI, simp) 
    87 apply (rule_tac x = "nat x" in exI, simp)
    87 done
    88 done
    88 
    89 
    89 lemma bdd_int_set_le_finite: "finite {x::int. 0 \<le> x & x \<le> n}"
    90 lemma bdd_int_set_le_finite: "finite {x::int. 0 \<le> x & x \<le> n}"
    90 apply (subgoal_tac "{x. 0 \<le> x & x \<le> n} = {x. 0 \<le> x & x < n + 1}")
    91 apply (subgoal_tac "{x. 0 \<le> x & x \<le> n} = {x. 0 \<le> x & x < n + 1}")
    91 apply (erule ssubst)
    92 apply (erule ssubst)
    92 apply (rule bdd_int_set_l_finite)
    93 apply (rule bdd_int_set_l_finite)
    93 by auto
    94 apply auto
       
    95 done
    94 
    96 
    95 lemma bdd_int_set_l_l_finite: "finite {x::int. 0 < x & x < n}"
    97 lemma bdd_int_set_l_l_finite: "finite {x::int. 0 < x & x < n}"
    96 apply (subgoal_tac "{x::int. 0 < x & x < n} \<subseteq> {x::int. 0 \<le> x & x < n}")
    98 proof -
    97 by (auto simp add: bdd_int_set_l_finite finite_subset)
    99   have "{x::int. 0 < x & x < n} \<subseteq> {x::int. 0 \<le> x & x < n}"
       
   100     by auto
       
   101   then show ?thesis by (auto simp add: bdd_int_set_l_finite finite_subset)
       
   102 qed
    98 
   103 
    99 lemma bdd_int_set_l_le_finite: "finite {x::int. 0 < x & x \<le> n}"
   104 lemma bdd_int_set_l_le_finite: "finite {x::int. 0 < x & x \<le> n}"
   100 apply (subgoal_tac "{x::int. 0 < x & x \<le> n} \<subseteq> {x::int. 0 \<le> x & x \<le> n}")
   105 proof -
   101 by (auto simp add: bdd_int_set_le_finite finite_subset)
   106   have "{x::int. 0 < x & x \<le> n} \<subseteq> {x::int. 0 \<le> x & x \<le> n}"
       
   107     by auto
       
   108   then show ?thesis by (auto simp add: bdd_int_set_le_finite finite_subset)
       
   109 qed
   102 
   110 
   103 lemma card_bdd_nat_set_l: "card {y::nat . y < x} = x"
   111 lemma card_bdd_nat_set_l: "card {y::nat . y < x} = x"
   104 apply (induct_tac x, force)
   112 proof (induct x)
   105 proof -
   113   show "card {y::nat . y < 0} = 0" by simp
       
   114 next
   106   fix n::nat
   115   fix n::nat
   107   assume "card {y. y < n} = n" 
   116   assume "card {y. y < n} = n"
   108   have "{y. y < Suc n} = insert n {y. y < n}"
   117   have "{y. y < Suc n} = insert n {y. y < n}"
   109     by auto
   118     by auto
   110   then have "card {y. y < Suc n} = card (insert n {y. y < n})"
   119   then have "card {y. y < Suc n} = card (insert n {y. y < n})"
   111     by auto
   120     by auto
   112   also have "... = Suc (card {y. y < n})"
   121   also have "... = Suc (card {y. y < n})"
   113     apply (rule card_insert_disjoint)
   122     by (rule card_insert_disjoint) (auto simp add: bdd_nat_set_l_finite)
   114     by (auto simp add: bdd_nat_set_l_finite)
   123   finally show "card {y. y < Suc n} = Suc n"
   115   finally show "card {y. y < Suc n} = Suc n" 
       
   116     by (simp add: prems)
   124     by (simp add: prems)
   117 qed
   125 qed
   118 
   126 
   119 lemma card_bdd_nat_set_le: "card { y::nat. y \<le> x} = Suc x"
   127 lemma card_bdd_nat_set_le: "card { y::nat. y \<le> x} = Suc x"
   120 apply (subgoal_tac "{ y::nat. y \<le> x} = { y::nat. y < Suc x}")
   128 proof -
   121 by (auto simp add: card_bdd_nat_set_l)
   129   have "{y::nat. y \<le> x} = { y::nat. y < Suc x}"
       
   130     by auto
       
   131   then show ?thesis by (auto simp add: card_bdd_nat_set_l)
       
   132 qed
   122 
   133 
   123 lemma card_bdd_int_set_l: "0 \<le> (n::int) ==> card {y. 0 \<le> y & y < n} = nat n"
   134 lemma card_bdd_int_set_l: "0 \<le> (n::int) ==> card {y. 0 \<le> y & y < n} = nat n"
   124 proof -
   135 proof -
   125   fix n::int
       
   126   assume "0 \<le> n"
   136   assume "0 \<le> n"
   127   have "inj_on (%y. int y) {y. y < nat n}"
   137   have "inj_on (%y. int y) {y. y < nat n}"
   128     by (auto simp add: inj_on_def)
   138     by (auto simp add: inj_on_def)
   129   hence "card (int ` {y. y < nat n}) = card {y. y < nat n}"
   139   hence "card (int ` {y. y < nat n}) = card {y. y < nat n}"
   130     by (rule card_image)
   140     by (rule card_image)
   131   also from prems have "int ` {y. y < nat n} = {y. 0 \<le> y & y < n}"
   141   also from prems have "int ` {y. y < nat n} = {y. 0 \<le> y & y < n}"
   132     apply (auto simp add: zless_nat_eq_int_zless image_def)
   142     apply (auto simp add: zless_nat_eq_int_zless image_def)
   133     apply (rule_tac x = "nat x" in exI)
   143     apply (rule_tac x = "nat x" in exI)
   134     by (auto simp add: nat_0_le)
   144     apply (auto simp add: nat_0_le)
   135   also have "card {y. y < nat n} = nat n" 
   145     done
       
   146   also have "card {y. y < nat n} = nat n"
   136     by (rule card_bdd_nat_set_l)
   147     by (rule card_bdd_nat_set_l)
   137   finally show "card {y. 0 \<le> y & y < n} = nat n" .
   148   finally show "card {y. 0 \<le> y & y < n} = nat n" .
   138 qed
   149 qed
   139 
   150 
   140 lemma card_bdd_int_set_le: "0 \<le> (n::int) ==> card {y. 0 \<le> y & y \<le> n} = 
   151 lemma card_bdd_int_set_le: "0 \<le> (n::int) ==> card {y. 0 \<le> y & y \<le> n} =
   141   nat n + 1"
   152   nat n + 1"
   142 apply (subgoal_tac "{y. 0 \<le> y & y \<le> n} = {y. 0 \<le> y & y < n+1}")
   153 proof -
   143 apply (insert card_bdd_int_set_l [of "n+1"])
   154   assume "0 \<le> n"
   144 by (auto simp add: nat_add_distrib)
   155   moreover have "{y. 0 \<le> y & y \<le> n} = {y. 0 \<le> y & y < n+1}" by auto
   145 
   156   ultimately show ?thesis
   146 lemma card_bdd_int_set_l_le: "0 \<le> (n::int) ==> 
   157     using card_bdd_int_set_l [of "n + 1"]
       
   158     by (auto simp add: nat_add_distrib)
       
   159 qed
       
   160 
       
   161 lemma card_bdd_int_set_l_le: "0 \<le> (n::int) ==>
   147     card {x. 0 < x & x \<le> n} = nat n"
   162     card {x. 0 < x & x \<le> n} = nat n"
   148 proof -
   163 proof -
   149   fix n::int
       
   150   assume "0 \<le> n"
   164   assume "0 \<le> n"
   151   have "inj_on (%x. x+1) {x. 0 \<le> x & x < n}"
   165   have "inj_on (%x. x+1) {x. 0 \<le> x & x < n}"
   152     by (auto simp add: inj_on_def)
   166     by (auto simp add: inj_on_def)
   153   hence "card ((%x. x+1) ` {x. 0 \<le> x & x < n}) = 
   167   hence "card ((%x. x+1) ` {x. 0 \<le> x & x < n}) =
   154      card {x. 0 \<le> x & x < n}"
   168      card {x. 0 \<le> x & x < n}"
   155     by (rule card_image)
   169     by (rule card_image)
   156   also from prems have "... = nat n"
   170   also from `0 \<le> n` have "... = nat n"
   157     by (rule card_bdd_int_set_l)
   171     by (rule card_bdd_int_set_l)
   158   also have "(%x. x + 1) ` {x. 0 \<le> x & x < n} = {x. 0 < x & x<= n}"
   172   also have "(%x. x + 1) ` {x. 0 \<le> x & x < n} = {x. 0 < x & x<= n}"
   159     apply (auto simp add: image_def)
   173     apply (auto simp add: image_def)
   160     apply (rule_tac x = "x - 1" in exI)
   174     apply (rule_tac x = "x - 1" in exI)
   161     by arith
   175     apply arith
   162   finally show "card {x. 0 < x & x \<le> n} = nat n".
   176     done
   163 qed
   177   finally show "card {x. 0 < x & x \<le> n} = nat n" .
   164 
   178 qed
   165 lemma card_bdd_int_set_l_l: "0 < (n::int) ==> 
   179 
   166     card {x. 0 < x & x < n} = nat n - 1"
   180 lemma card_bdd_int_set_l_l: "0 < (n::int) ==>
   167   apply (subgoal_tac "{x. 0 < x & x < n} = {x. 0 < x & x \<le> n - 1}")
   181   card {x. 0 < x & x < n} = nat n - 1"
   168   apply (insert card_bdd_int_set_l_le [of "n - 1"])
   182 proof -
   169   by (auto simp add: nat_diff_distrib)
   183   assume "0 < n"
   170 
   184   moreover have "{x. 0 < x & x < n} = {x. 0 < x & x \<le> n - 1}"
   171 lemma int_card_bdd_int_set_l_l: "0 < n ==> 
   185     by simp
       
   186   ultimately show ?thesis
       
   187     using insert card_bdd_int_set_l_le [of "n - 1"]
       
   188     by (auto simp add: nat_diff_distrib)
       
   189 qed
       
   190 
       
   191 lemma int_card_bdd_int_set_l_l: "0 < n ==>
   172     int(card {x. 0 < x & x < n}) = n - 1"
   192     int(card {x. 0 < x & x < n}) = n - 1"
   173   apply (auto simp add: card_bdd_int_set_l_l)
   193   apply (auto simp add: card_bdd_int_set_l_l)
   174   apply (subgoal_tac "Suc 0 \<le> nat n")
   194   apply (subgoal_tac "Suc 0 \<le> nat n")
   175   apply (auto simp add: zdiff_int [THEN sym])
   195   apply (auto simp add: zdiff_int [symmetric])
   176   apply (subgoal_tac "0 < nat n", arith)
   196   apply (subgoal_tac "0 < nat n", arith)
   177   by (simp add: zero_less_nat_eq)
   197   apply (simp add: zero_less_nat_eq)
   178 
   198   done
   179 lemma int_card_bdd_int_set_l_le: "0 \<le> n ==> 
   199 
       
   200 lemma int_card_bdd_int_set_l_le: "0 \<le> n ==>
   180     int(card {x. 0 < x & x \<le> n}) = n"
   201     int(card {x. 0 < x & x \<le> n}) = n"
   181   by (auto simp add: card_bdd_int_set_l_le)
   202   by (auto simp add: card_bdd_int_set_l_le)
   182 
   203 
   183 (******************************************************************)
   204 (******************************************************************)
   184 (*                                                                *)
   205 (*                                                                *)
   199 (*                                                                *)
   220 (*                                                                *)
   200 (******************************************************************)
   221 (******************************************************************)
   201 
   222 
   202 subsection {* Lemmas for counting arguments *}
   223 subsection {* Lemmas for counting arguments *}
   203 
   224 
   204 lemma setsum_bij_eq: "[| finite A; finite B; f ` A \<subseteq> B; inj_on f A; 
   225 lemma setsum_bij_eq: "[| finite A; finite B; f ` A \<subseteq> B; inj_on f A;
   205     g ` B \<subseteq> A; inj_on g B |] ==> setsum g B = setsum (g \<circ> f) A"
   226     g ` B \<subseteq> A; inj_on g B |] ==> setsum g B = setsum (g \<circ> f) A"
   206 apply (frule_tac h = g and f = f in setsum_reindex)
   227 apply (frule_tac h = g and f = f in setsum_reindex)
   207 apply (subgoal_tac "setsum g B = setsum g (f ` A)")
   228 apply (subgoal_tac "setsum g B = setsum g (f ` A)")
   208 apply (simp add: inj_on_def)
   229 apply (simp add: inj_on_def)
   209 apply (subgoal_tac "card A = card B")
   230 apply (subgoal_tac "card A = card B")
   210 apply (drule_tac A = "f ` A" and B = B in card_seteq)
   231 apply (drule_tac A = "f ` A" and B = B in card_seteq)
   211 apply (auto simp add: card_image)
   232 apply (auto simp add: card_image)
   212 apply (frule_tac A = A and B = B and f = f in card_inj_on_le, auto)
   233 apply (frule_tac A = A and B = B and f = f in card_inj_on_le, auto)
   213 apply (frule_tac A = B and B = A and f = g in card_inj_on_le)
   234 apply (frule_tac A = B and B = A and f = g in card_inj_on_le)
   214 by auto
   235 apply auto
   215 
   236 done
   216 lemma setprod_bij_eq: "[| finite A; finite B; f ` A \<subseteq> B; inj_on f A; 
   237 
       
   238 lemma setprod_bij_eq: "[| finite A; finite B; f ` A \<subseteq> B; inj_on f A;
   217     g ` B \<subseteq> A; inj_on g B |] ==> setprod g B = setprod (g \<circ> f) A"
   239     g ` B \<subseteq> A; inj_on g B |] ==> setprod g B = setprod (g \<circ> f) A"
   218   apply (frule_tac h = g and f = f in setprod_reindex)
   240   apply (frule_tac h = g and f = f in setprod_reindex)
   219   apply (subgoal_tac "setprod g B = setprod g (f ` A)") 
   241   apply (subgoal_tac "setprod g B = setprod g (f ` A)")
   220   apply (simp add: inj_on_def)
   242   apply (simp add: inj_on_def)
   221   apply (subgoal_tac "card A = card B")
   243   apply (subgoal_tac "card A = card B")
   222   apply (drule_tac A = "f ` A" and B = B in card_seteq)
   244   apply (drule_tac A = "f ` A" and B = B in card_seteq)
   223   apply (auto simp add: card_image)
   245   apply (auto simp add: card_image)
   224   apply (frule_tac A = A and B = B and f = f in card_inj_on_le, auto)
   246   apply (frule_tac A = A and B = B and f = f in card_inj_on_le, auto)
   225 by (frule_tac A = B and B = A and f = g in card_inj_on_le, auto)
   247   apply (frule_tac A = B and B = A and f = g in card_inj_on_le, auto)
       
   248   done
   226 
   249 
   227 end
   250 end