| author | wenzelm | 
| Sat, 13 Feb 2016 12:39:00 +0100 | |
| changeset 62290 | 658276428cfc | 
| parent 61609 | 77b453bd616f | 
| child 63040 | eb4ddd18d635 | 
| permissions | -rw-r--r-- | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 1 | (* Title: HOL/ex/Ballot.thy | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 2 | Author: Lukas Bulwahn <lukas.bulwahn-at-gmail.com> | 
| 60604 | 3 | Author: Johannes Hölzl <hoelzl@in.tum.de> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 4 | *) | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 5 | |
| 61343 | 6 | section \<open>Bertrand's Ballot Theorem\<close> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 7 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 8 | theory Ballot | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 9 | imports | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 10 | Complex_Main | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 11 | "~~/src/HOL/Library/FuncSet" | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 12 | begin | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 13 | |
| 61343 | 14 | subsection \<open>Preliminaries\<close> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 15 | |
| 60604 | 16 | lemma card_bij': | 
| 17 | assumes "f \<in> A \<rightarrow> B" "\<And>x. x \<in> A \<Longrightarrow> g (f x) = x" | |
| 18 | and "g \<in> B \<rightarrow> A" "\<And>x. x \<in> B \<Longrightarrow> f (g x) = x" | |
| 19 | shows "card A = card B" | |
| 20 | apply (rule bij_betw_same_card) | |
| 21 | apply (rule bij_betwI) | |
| 22 | apply fact+ | |
| 23 | done | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 24 | |
| 61343 | 25 | subsection \<open>Formalization of Problem Statement\<close> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 26 | |
| 61343 | 27 | subsubsection \<open>Basic Definitions\<close> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 28 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 29 | datatype vote = A | B | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 30 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 31 | definition | 
| 60604 | 32 |   "all_countings a b = card {f \<in> {1 .. a + b} \<rightarrow>\<^sub>E {A, B}.
 | 
| 33 |       card {x \<in> {1 .. a + b}. f x = A} = a \<and> card {x \<in> {1 .. a + b}. f x = B} = b}"
 | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 34 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 35 | definition | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 36 | "valid_countings a b = | 
| 60604 | 37 |     card {f\<in>{1..a+b} \<rightarrow>\<^sub>E {A, B}.
 | 
| 38 |       card {x\<in>{1..a+b}. f x = A} = a \<and> card {x\<in>{1..a+b}. f x = B} = b \<and>
 | |
| 39 |       (\<forall>m\<in>{1..a+b}. card {x\<in>{1..m}. f x = A} > card {x\<in>{1..m}. f x = B})}"
 | |
| 40 | ||
| 61343 | 41 | subsubsection \<open>Equivalence with Set Cardinality\<close> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 42 | |
| 60604 | 43 | lemma Collect_on_transfer: | 
| 44 | assumes "rel_set R X Y" | |
| 45 |   shows "rel_fun (rel_fun R op =) (rel_set R) (\<lambda>P. {x\<in>X. P x}) (\<lambda>P. {y\<in>Y. P y})"
 | |
| 46 | using assms unfolding rel_fun_def rel_set_def by fast | |
| 47 | ||
| 48 | lemma rel_fun_trans: | |
| 49 | "rel_fun P Q g g' \<Longrightarrow> rel_fun R P f f' \<Longrightarrow> rel_fun R Q (\<lambda>x. g (f x)) (\<lambda>y. g' (f' y))" | |
| 50 | by (auto simp: rel_fun_def) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 51 | |
| 60604 | 52 | lemma rel_fun_trans2: | 
| 53 | "rel_fun P1 (rel_fun P2 Q) g g' \<Longrightarrow> rel_fun R P1 f1 f1' \<Longrightarrow> rel_fun R P2 f2 f2' \<Longrightarrow> | |
| 54 | rel_fun R Q (\<lambda>x. g (f1 x) (f2 x)) (\<lambda>y. g' (f1' y) (f2' y))" | |
| 55 | by (auto simp: rel_fun_def) | |
| 56 | ||
| 57 | lemma rel_fun_trans2': | |
| 58 | "rel_fun R (op =) f1 f1' \<Longrightarrow> rel_fun R (op =) f2 f2' \<Longrightarrow> | |
| 59 | rel_fun R (op =) (\<lambda>x. g (f1 x) (f2 x)) (\<lambda>y. g (f1' y) (f2' y))" | |
| 60 | by (auto simp: rel_fun_def) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 61 | |
| 60604 | 62 | lemma rel_fun_const: "rel_fun R (op =) (\<lambda>x. a) (\<lambda>y. a)" | 
| 63 | by auto | |
| 64 | ||
| 65 | lemma rel_fun_conj: | |
| 66 | "rel_fun R (op =) f f' \<Longrightarrow> rel_fun R (op =) g g' \<Longrightarrow> rel_fun R (op =) (\<lambda>x. f x \<and> g x) (\<lambda>y. f' y \<and> g' y)" | |
| 67 | by (auto simp: rel_fun_def) | |
| 68 | ||
| 69 | lemma rel_fun_ball: | |
| 70 | "(\<And>i. i \<in> I \<Longrightarrow> rel_fun R (op =) (f i) (f' i)) \<Longrightarrow> rel_fun R (op =) (\<lambda>x. \<forall>i\<in>I. f i x) (\<lambda>y. \<forall>i\<in>I. f' i y)" | |
| 71 | by (auto simp: rel_fun_def rel_set_def) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 72 | |
| 60604 | 73 | lemma | 
| 74 |   shows all_countings_set: "all_countings a b = card {V\<in>Pow {0..<a+b}. card V = a}"
 | |
| 75 | (is "_ = card ?A") | |
| 76 | and valid_countings_set: "valid_countings a b = | |
| 77 |       card {V\<in>Pow {0..<a+b}. card V = a \<and> (\<forall>m\<in>{1..a+b}. card ({0..<m} \<inter> V) > m - card ({0..<m} \<inter> V))}"
 | |
| 78 | (is "_ = card ?V") | |
| 79 | proof - | |
| 80 | def P \<equiv> "\<lambda>j i. i < a + b \<and> j = Suc i" | |
| 81 |   have unique_P: "bi_unique P" and total_P: "\<And>m. m \<le> a + b \<Longrightarrow> rel_set P {1..m} {0..<m}"
 | |
| 82 | by (auto simp add: bi_unique_def rel_set_def P_def Suc_le_eq gr0_conv_Suc) | |
| 83 | have rel_fun_P: "\<And>R f g. (\<And>i. i < a+b \<Longrightarrow> R (f (Suc i)) (g i)) \<Longrightarrow> rel_fun P R f g" | |
| 84 | by (simp add: rel_fun_def P_def) | |
| 85 | ||
| 86 |   def R \<equiv> "\<lambda>f V. V \<subseteq> {0..<a+b} \<and> f \<in> extensional {1..a+b} \<and> (\<forall>i<a+b. i \<in> V \<longleftrightarrow> f (Suc i) = A)"
 | |
| 87 |   { fix f g :: "nat \<Rightarrow> vote" assume "f \<in> extensional {1..a + b}" "g \<in> extensional {1..a + b}" 
 | |
| 88 | moreover assume "\<forall>i<a + b. (f (Suc i) = A) = (g (Suc i) = A)" | |
| 89 | then have "\<forall>i<a + b. f (Suc i) = g (Suc i)" | |
| 90 | by (metis vote.nchotomy) | |
| 91 | ultimately have "f i = g i" for i | |
| 92 |       by (cases "i \<in> {1..a+b}") (auto simp: extensional_def Suc_le_eq gr0_conv_Suc) }
 | |
| 93 | then have unique_R: "bi_unique R" | |
| 94 | by (auto simp: bi_unique_def R_def) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 95 | |
| 60604 | 96 |   have "f \<in> extensional {1..a + b} \<Longrightarrow> \<exists>V\<in>Pow {0..<a + b}. R f V" for f
 | 
| 97 |     by (intro bexI[of _ "{i. i < a+b \<and> f (Suc i) = A}"]) (auto simp add: R_def PiE_def)
 | |
| 98 |   moreover have "V \<in> Pow {0..<a + b} \<Longrightarrow> \<exists>f\<in>extensional {1..a+b}. R f V" for V
 | |
| 99 |     by (intro bexI[of _ "\<lambda>i\<in>{1..a+b}. if i - 1 \<in> V then A else B"]) (auto simp add: R_def PiE_def)
 | |
| 100 |   ultimately have total_R: "rel_set R (extensional {1..a+b}) (Pow {0..<a+b})"
 | |
| 101 | by (auto simp: rel_set_def) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 102 | |
| 60604 | 103 | have P: "rel_fun R (rel_fun P op =) (\<lambda>f x. f x = A) (\<lambda>V y. y \<in> V)" | 
| 104 | by (auto simp: P_def R_def Suc_le_eq gr0_conv_Suc rel_fun_def) | |
| 105 | ||
| 106 | have eq_B: "x = B \<longleftrightarrow> x \<noteq> A" for x | |
| 107 | by (cases x; simp) | |
| 108 | ||
| 109 |   { fix f and m :: nat
 | |
| 110 |     have "card {x\<in>{1..m}. f x = B} = card ({1..m} - {x\<in>{1..m}. f x = A})"
 | |
| 111 | by (simp add: eq_B set_diff_eq cong: conj_cong) | |
| 112 |     also have "\<dots> = m - card {x\<in>{1..m}. f x = A}"
 | |
| 113 | by (subst card_Diff_subset) auto | |
| 114 |     finally have "card {x\<in>{1..m}. f x = B} = m - card {x\<in>{1..m}. f x = A}" . }
 | |
| 115 | note card_B = this | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 116 | |
| 60604 | 117 | note transfers = rel_fun_const card_transfer[THEN rel_funD, OF unique_R] rel_fun_conj rel_fun_ball | 
| 118 | Collect_on_transfer[THEN rel_funD, OF total_R] Collect_on_transfer[THEN rel_funD, OF total_P] | |
| 119 | rel_fun_trans[OF card_transfer, OF unique_P] rel_fun_trans[OF Collect_on_transfer[OF total_P]] | |
| 120 | rel_fun_trans2'[where g="op ="] rel_fun_trans2'[where g="op <"] rel_fun_trans2'[where g="op -"] | |
| 121 | ||
| 122 |   have "all_countings a b = card {f \<in> extensional {1..a + b}. card {x \<in> {1..a + b}. f x = A} = a}"
 | |
| 123 | using card_B by (simp add: all_countings_def PiE_iff vote.nchotomy cong: conj_cong) | |
| 124 |   also have "\<dots> = card {V\<in>Pow {0..<a+b}. card ({x\<in>{0 ..< a + b}. x \<in> V}) = a}"
 | |
| 125 | by (intro P order_refl transfers) | |
| 126 | finally show "all_countings a b = card ?A" | |
| 127 | unfolding Int_def[symmetric] by (simp add: Int_absorb1 cong: conj_cong) | |
| 128 | ||
| 129 |   have "valid_countings a b = card {f\<in>extensional {1..a+b}.
 | |
| 130 |       card {x\<in>{1..a+b}. f x = A} = a \<and> (\<forall>m\<in>{1..a+b}. card {x\<in>{1..m}. f x = A} > m - card {x\<in>{1..m}. f x = A})}"
 | |
| 131 | using card_B by (simp add: valid_countings_def PiE_iff vote.nchotomy cong: conj_cong) | |
| 132 |   also have "\<dots> = card {V\<in>Pow {0..<a+b}. card {x\<in>{0..<a+b}. x\<in>V} = a \<and>
 | |
| 133 |     (\<forall>m\<in>{1..a+b}. card {x\<in>{0..<m}. x\<in>V} > m - card {x\<in>{0..<m}. x\<in>V})}"
 | |
| 134 | by (intro P order_refl transfers) auto | |
| 135 | finally show "valid_countings a b = card ?V" | |
| 136 | unfolding Int_def[symmetric] by (simp add: Int_absorb1 cong: conj_cong) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 137 | qed | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 138 | |
| 60604 | 139 | lemma all_countings: "all_countings a b = (a + b) choose a" | 
| 140 | unfolding all_countings_set by (simp add: n_subsets) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 141 | |
| 61343 | 142 | subsection \<open>Facts About @{term valid_countings}\<close>
 | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 143 | |
| 61343 | 144 | subsubsection \<open>Non-Recursive Cases\<close> | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 145 | |
| 60604 | 146 | lemma card_V_eq_a: "V \<subseteq> {0..<a} \<Longrightarrow> card V = a \<longleftrightarrow> V = {0..<a}"
 | 
| 147 |   using card_subset_eq[of "{0..<a}" V] by auto
 | |
| 148 | ||
| 149 | lemma valid_countings_a_0: "valid_countings a 0 = 1" | |
| 150 | by (simp add: valid_countings_set card_V_eq_a cong: conj_cong) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 151 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 152 | lemma valid_countings_eq_zero: | 
| 60604 | 153 | "a \<le> b \<Longrightarrow> 0 < b \<Longrightarrow> valid_countings a b = 0" | 
| 154 | by (auto simp add: valid_countings_set Int_absorb1 intro!: bexI[of _ "a + b"]) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 155 | |
| 60604 | 156 | lemma Ico_subset_finite: "i \<subseteq> {a ..< b::nat} \<Longrightarrow> finite i"
 | 
| 157 | by (auto dest: finite_subset) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 158 | |
| 60604 | 159 | lemma Icc_Suc2: "a \<le> b \<Longrightarrow> {a..Suc b} = insert (Suc b) {a..b}"
 | 
| 160 | by auto | |
| 161 | ||
| 162 | lemma Ico_Suc2: "a \<le> b \<Longrightarrow> {a..<Suc b} = insert b {a..<b}"
 | |
| 163 | by auto | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 164 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 165 | lemma valid_countings_Suc_Suc: | 
| 60604 | 166 | assumes "b < a" | 
| 167 | shows "valid_countings (Suc a) (Suc b) = valid_countings a (Suc b) + valid_countings (Suc a) b" | |
| 168 | proof - | |
| 169 | let ?l = "Suc (a + b)" | |
| 170 |   let ?Q = "\<lambda>V c. \<forall>m\<in>{1..c}. m - card ({0..<m} \<inter> V) < card ({0..<m} \<inter> V)"
 | |
| 171 |   let ?V = "\<lambda>P. {V. (V \<in> Pow {0..<Suc ?l} \<and> P V) \<and> card V = Suc a \<and> ?Q V (Suc ?l)}"
 | |
| 172 | have "valid_countings (Suc a) (Suc b) = card (?V (\<lambda>V. ?l \<notin> V)) + card (?V (\<lambda>V. ?l \<in> V))" | |
| 173 | unfolding valid_countings_set | |
| 174 | by (subst card_Un_disjoint[symmetric]) (auto simp add: set_eq_iff intro!: arg_cong[where f=card]) | |
| 175 | also have "card (?V (\<lambda>V. ?l \<in> V)) = valid_countings a (Suc b)" | |
| 176 | unfolding valid_countings_set | |
| 177 |   proof (rule card_bij'[where f="\<lambda>V. V - {?l}" and g="insert ?l"])
 | |
| 178 |     have *: "\<And>m V. m \<in> {1..a + Suc b} \<Longrightarrow> {0..<m} \<inter> (V - {?l}) = {0..<m} \<inter> V"
 | |
| 179 | by auto | |
| 180 |     show "(\<lambda>V. V - {?l}) \<in> ?V (\<lambda>V. ?l \<in> V) \<rightarrow> {V \<in> Pow {0..<a + Suc b}. card V = a \<and> ?Q V (a + Suc b)}"
 | |
| 181 | by (auto simp: Ico_subset_finite *) | |
| 182 |     { fix V assume "V \<subseteq> {0..<?l}"
 | |
| 183 |       moreover then have "finite V" "?l \<notin> V" "{0..<Suc ?l} \<inter> V = V"
 | |
| 184 | by (auto dest: finite_subset) | |
| 185 | ultimately have "card (insert ?l V) = Suc (card V)" | |
| 186 |         "card ({0..<m} \<inter> insert ?l V) = (if m = Suc ?l then Suc (card V) else card ({0..<m} \<inter> V))"
 | |
| 187 | if "m \<le> Suc ?l" for m | |
| 188 | using that by auto } | |
| 189 |     then show "insert ?l \<in> {V \<in> Pow {0..<a + Suc b}. card V = a \<and> ?Q V (a + Suc b)} \<rightarrow> ?V (\<lambda>V. ?l \<in> V)"
 | |
| 61343 | 190 | using \<open>b < a\<close> by auto | 
| 60604 | 191 | qed auto | 
| 192 | also have "card (?V (\<lambda>V. ?l \<notin> V)) = valid_countings (Suc a) b" | |
| 193 | unfolding valid_countings_set | |
| 194 |   proof (intro arg_cong[where f="\<lambda>P. card {x. P x}"] ext conj_cong)
 | |
| 195 |     fix V assume "V \<in> Pow {0..<Suc a + b}" and [simp]: "card V = Suc a"
 | |
| 196 |     then have [simp]: "V \<subseteq> {0..<Suc ?l}"
 | |
| 197 | by auto | |
| 198 | show "?Q V (Suc ?l) = ?Q V (Suc a + b)" | |
| 61343 | 199 | using \<open>b<a\<close> by (simp add: Int_absorb1 Icc_Suc2) | 
| 60604 | 200 | qed (auto simp: subset_eq less_Suc_eq) | 
| 201 | finally show ?thesis | |
| 202 | by simp | |
| 203 | qed | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 204 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 205 | lemma valid_countings: | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 206 | "(a + b) * valid_countings a b = (a - b) * ((a + b) choose a)" | 
| 60604 | 207 | proof (induct a arbitrary: b) | 
| 208 | case 0 show ?case | |
| 209 | by (cases b) (simp_all add: valid_countings_eq_zero) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 210 | next | 
| 60604 | 211 | case (Suc a) note Suc_a = this | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 212 | show ?case | 
| 60604 | 213 | proof (induct b) | 
| 214 | case (Suc b) note Suc_b = this | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 215 | show ?case | 
| 60604 | 216 | proof cases | 
| 217 | assume "a \<le> b" then show ?thesis | |
| 218 | by (simp add: valid_countings_eq_zero) | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 219 | next | 
| 60604 | 220 | assume "\<not> a \<le> b" | 
| 221 | then have "b < a" by simp | |
| 222 | ||
| 223 | have "Suc a * (a - Suc b) + (Suc a - b) * Suc b = | |
| 224 | (Suc a * a - Suc a * Suc b) + (Suc a * Suc b - Suc b * b)" | |
| 225 | by (simp add: sign_simps) | |
| 226 | also have "\<dots> = (Suc a * a + (Suc a * Suc b - Suc b * b)) - Suc a * Suc b" | |
| 61343 | 227 | using \<open>b<a\<close> by (intro add_diff_assoc2 mult_mono) auto | 
| 60604 | 228 | also have "\<dots> = (Suc a * a + Suc a * Suc b) - Suc b * b - Suc a * Suc b" | 
| 61343 | 229 | using \<open>b<a\<close> by (intro arg_cong2[where f="op -"] add_diff_assoc mult_mono) auto | 
| 60604 | 230 | also have "\<dots> = (Suc a * Suc (a + b)) - (Suc b * Suc (a + b))" | 
| 231 | by (simp add: sign_simps) | |
| 232 | finally have rearrange: "Suc a * (a - Suc b) + (Suc a - b) * Suc b = (Suc a - Suc b) * Suc (a + b)" | |
| 233 | unfolding diff_mult_distrib by simp | |
| 234 | ||
| 235 | have "(Suc a * Suc (a + b)) * ((Suc a + Suc b) * valid_countings (Suc a) (Suc b)) = | |
| 236 | (Suc a + Suc b) * Suc a * ((a + Suc b) * valid_countings a (Suc b) + (Suc a + b) * valid_countings (Suc a) b)" | |
| 61343 | 237 | unfolding valid_countings_Suc_Suc[OF \<open>b < a\<close>] by (simp add: field_simps) | 
| 60604 | 238 | also have "... = (Suc a + Suc b) * ((a - Suc b) * (Suc a * (Suc (a + b) choose a)) + | 
| 239 | (Suc a - b) * (Suc a * (Suc (a + b) choose Suc a)))" | |
| 240 | unfolding Suc_a Suc_b by (simp add: field_simps) | |
| 241 | also have "... = (Suc a * (a - Suc b) + (Suc a - b) * Suc b) * (Suc (Suc a + b) * (Suc a + b choose a))" | |
| 242 | unfolding Suc_times_binomial_add by (simp add: field_simps) | |
| 243 | also have "... = Suc a * (Suc a * (a - Suc b) + (Suc a - b) * Suc b) * (Suc a + Suc b choose Suc a)" | |
| 244 | unfolding Suc_times_binomial_eq by (simp add: field_simps) | |
| 245 | also have "... = (Suc a * Suc (a + b)) * ((Suc a - Suc b) * (Suc a + Suc b choose Suc a))" | |
| 246 | unfolding rearrange by (simp only: mult_ac) | |
| 247 | finally show ?thesis | |
| 248 | unfolding mult_cancel1 by simp | |
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 249 | qed | 
| 60604 | 250 | qed (simp add: valid_countings_a_0) | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 251 | qed | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 252 | |
| 60604 | 253 | lemma valid_countings_eq[code]: | 
| 254 | "valid_countings a b = (if a + b = 0 then 1 else ((a - b) * ((a + b) choose a)) div (a + b))" | |
| 255 | by (simp add: valid_countings[symmetric] valid_countings_a_0) | |
| 256 | ||
| 61343 | 257 | subsection \<open>Relation Between @{term valid_countings} and @{term all_countings}\<close>
 | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 258 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 259 | lemma main_nat: "(a + b) * valid_countings a b = (a - b) * all_countings a b" | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 260 | unfolding valid_countings all_countings .. | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 261 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 262 | lemma main_real: | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 263 | assumes "b < a" | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 264 | shows "valid_countings a b = (a - b) / (a + b) * all_countings a b" | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 265 | using assms | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 266 | proof - | 
| 61343 | 267 | from main_nat[of a b] \<open>b < a\<close> have | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 268 | "(real a + real b) * real (valid_countings a b) = (real a - real b) * real (all_countings a b)" | 
| 61609 
77b453bd616f
Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
 paulson <lp15@cam.ac.uk> parents: 
61343diff
changeset | 269 | by (simp only: of_nat_add[symmetric] of_nat_mult[symmetric]) auto | 
| 61343 | 270 | from this \<open>b < a\<close> show ?thesis | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 271 | by (subst mult_left_cancel[of "real a + real b", symmetric]) auto | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 272 | qed | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 273 | |
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 274 | lemma | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 275 | "valid_countings a b = (if a \<le> b then (if b = 0 then 1 else 0) else (a - b) / (a + b) * all_countings a b)" | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 276 | proof (cases "a \<le> b") | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 277 | case False | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 278 | from this show ?thesis by (simp add: main_real) | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 279 | next | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 280 | case True | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 281 | from this show ?thesis | 
| 60604 | 282 | by (auto simp add: valid_countings_a_0 all_countings valid_countings_eq_zero) | 
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 283 | qed | 
| 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 284 | |
| 61343 | 285 | subsubsection \<open>Executable Definition\<close> | 
| 60604 | 286 | |
| 287 | declare all_countings_def [code del] | |
| 288 | declare all_countings[code] | |
| 289 | ||
| 290 | value "all_countings 1 0" | |
| 291 | value "all_countings 0 1" | |
| 292 | value "all_countings 1 1" | |
| 293 | value "all_countings 2 1" | |
| 294 | value "all_countings 1 2" | |
| 295 | value "all_countings 2 4" | |
| 296 | value "all_countings 4 2" | |
| 297 | ||
| 61343 | 298 | subsubsection \<open>Executable Definition\<close> | 
| 60604 | 299 | |
| 300 | declare valid_countings_def [code del] | |
| 301 | ||
| 302 | value "valid_countings 1 0" | |
| 303 | value "valid_countings 0 1" | |
| 304 | value "valid_countings 1 1" | |
| 305 | value "valid_countings 2 1" | |
| 306 | value "valid_countings 1 2" | |
| 307 | value "valid_countings 2 4" | |
| 308 | value "valid_countings 4 2" | |
| 309 | ||
| 60603 
09ecbd791d4a
add examples from Freek's top 100 theorems (thms 30, 73, 77)
 bulwahn parents: diff
changeset | 310 | end |