src/HOL/Library/Stirling.thy
changeset 63071 3ca3bc795908
child 63145 703edebd1d92
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Stirling.thy	Wed May 04 10:19:01 2016 +0200
     1.3 @@ -0,0 +1,281 @@
     1.4 +(* Authors: Amine Chaieb & Florian Haftmann, TU Muenchen
     1.5 +            with contributions by Lukas Bulwahn and Manuel Eberl*)
     1.6 +
     1.7 +section {* Stirling numbers of first and second kind *}
     1.8 +
     1.9 +theory Stirling
    1.10 +imports Binomial
    1.11 +begin
    1.12 +
    1.13 +subsection {* Stirling numbers of the second kind *}
    1.14 +
    1.15 +fun Stirling :: "nat \<Rightarrow> nat \<Rightarrow> nat"
    1.16 +where
    1.17 +  "Stirling 0 0 = 1"
    1.18 +| "Stirling 0 (Suc k) = 0"
    1.19 +| "Stirling (Suc n) 0 = 0"
    1.20 +| "Stirling (Suc n) (Suc k) = Suc k * Stirling n (Suc k) + Stirling n k"
    1.21 +
    1.22 +lemma Stirling_1 [simp]:
    1.23 +  "Stirling (Suc n) (Suc 0) = 1"
    1.24 +  by (induct n) simp_all
    1.25 +
    1.26 +lemma Stirling_less [simp]:
    1.27 +  "n < k \<Longrightarrow> Stirling n k = 0"
    1.28 +  by (induct n k rule: Stirling.induct) simp_all
    1.29 +
    1.30 +lemma Stirling_same [simp]:
    1.31 +  "Stirling n n = 1"
    1.32 +  by (induct n) simp_all
    1.33 +
    1.34 +lemma Stirling_2_2:
    1.35 +  "Stirling (Suc (Suc n)) (Suc (Suc 0)) = 2 ^ Suc n - 1"
    1.36 +proof (induct n)
    1.37 +  case 0 then show ?case by simp
    1.38 +next
    1.39 +  case (Suc n)
    1.40 +  have "Stirling (Suc (Suc (Suc n))) (Suc (Suc 0)) =
    1.41 +    2 * Stirling (Suc (Suc n)) (Suc (Suc 0)) + Stirling (Suc (Suc n)) (Suc 0)" by simp
    1.42 +  also have "\<dots> = 2 * (2 ^ Suc n - 1) + 1"
    1.43 +    by (simp only: Suc Stirling_1)
    1.44 +  also have "\<dots> = 2 ^ Suc (Suc n) - 1"
    1.45 +  proof -
    1.46 +    have "(2::nat) ^ Suc n - 1 > 0" by (induct n) simp_all
    1.47 +    then have "2 * ((2::nat) ^ Suc n - 1) > 0" by simp
    1.48 +    then have "2 \<le> 2 * ((2::nat) ^ Suc n)" by simp
    1.49 +    with add_diff_assoc2 [of 2 "2 * 2 ^ Suc n" 1]
    1.50 +      have "2 * 2 ^ Suc n - 2 + (1::nat) = 2 * 2 ^ Suc n + 1 - 2" .
    1.51 +    then show ?thesis by (simp add: nat_distrib)
    1.52 +  qed
    1.53 +  finally show ?case by simp
    1.54 +qed
    1.55 +
    1.56 +lemma Stirling_2:
    1.57 +  "Stirling (Suc n) (Suc (Suc 0)) = 2 ^ n - 1"
    1.58 +  using Stirling_2_2 by (cases n) simp_all
    1.59 +
    1.60 +subsection {* Stirling numbers of the first kind *}
    1.61 +
    1.62 +fun stirling :: "nat \<Rightarrow> nat \<Rightarrow> nat"
    1.63 +where
    1.64 +  "stirling 0 0 = 1"
    1.65 +| "stirling 0 (Suc k) = 0"
    1.66 +| "stirling (Suc n) 0 = 0"
    1.67 +| "stirling (Suc n) (Suc k) = n * stirling n (Suc k) + stirling n k"
    1.68 +
    1.69 +lemma stirling_0 [simp]: "n > 0 \<Longrightarrow> stirling n 0 = 0"
    1.70 +  by (cases n) simp_all
    1.71 +
    1.72 +lemma stirling_less [simp]:
    1.73 +  "n < k \<Longrightarrow> stirling n k = 0"
    1.74 +  by (induct n k rule: stirling.induct) simp_all
    1.75 +
    1.76 +lemma stirling_same [simp]:
    1.77 +  "stirling n n = 1"
    1.78 +  by (induct n) simp_all
    1.79 +
    1.80 +lemma stirling_Suc_n_1:
    1.81 +  "stirling (Suc n) (Suc 0) = fact n"
    1.82 +  by (induct n) auto
    1.83 +
    1.84 +lemma stirling_Suc_n_n:
    1.85 +  shows "stirling (Suc n) n = Suc n choose 2"
    1.86 +by (induct n) (auto simp add: numerals(2))
    1.87 +
    1.88 +lemma stirling_Suc_n_2:
    1.89 +  assumes "n \<ge> Suc 0"
    1.90 +  shows "stirling (Suc n) 2 = (\<Sum>k=1..n. fact n div k)"
    1.91 +using assms
    1.92 +proof (induct n)
    1.93 +  case 0 from this show ?case by simp
    1.94 +next
    1.95 +  case (Suc n)
    1.96 +  show ?case
    1.97 +  proof (cases n)
    1.98 +    case 0 from this show ?thesis by (simp add: numerals(2))
    1.99 +  next
   1.100 +    case Suc
   1.101 +    from this have geq1: "Suc 0 \<le> n" by simp
   1.102 +    have "stirling (Suc (Suc n)) 2 = Suc n * stirling (Suc n) 2 + stirling (Suc n) (Suc 0)"
   1.103 +      by (simp only: stirling.simps(4)[of "Suc n"] numerals(2))
   1.104 +    also have "... = Suc n * (\<Sum>k=1..n. fact n div k) + fact n"
   1.105 +      using Suc.hyps[OF geq1]
   1.106 +      by (simp only: stirling_Suc_n_1 of_nat_fact of_nat_add of_nat_mult)
   1.107 +    also have "... = Suc n * (\<Sum>k=1..n. fact n div k) + Suc n * fact n div Suc n"
   1.108 +      by (metis nat.distinct(1) nonzero_mult_divide_cancel_left)
   1.109 +    also have "... = (\<Sum>k=1..n. fact (Suc n) div k) + fact (Suc n) div Suc n"
   1.110 +      by (simp add: setsum_right_distrib div_mult_swap dvd_fact)
   1.111 +    also have "... = (\<Sum>k=1..Suc n. fact (Suc n) div k)" by simp
   1.112 +    finally show ?thesis .
   1.113 +  qed
   1.114 +qed
   1.115 +
   1.116 +lemma of_nat_stirling_Suc_n_2:
   1.117 +  assumes "n \<ge> Suc 0"
   1.118 +  shows "(of_nat (stirling (Suc n) 2)::'a::field_char_0) = fact n * (\<Sum>k=1..n. (1 / of_nat k))"
   1.119 +using assms
   1.120 +proof (induct n)
   1.121 +  case 0 from this show ?case by simp
   1.122 +next
   1.123 +  case (Suc n)
   1.124 +  show ?case
   1.125 +  proof (cases n)
   1.126 +    case 0 from this show ?thesis by (auto simp add: numerals(2))
   1.127 +  next
   1.128 +    case Suc
   1.129 +    from this have geq1: "Suc 0 \<le> n" by simp
   1.130 +    have "(of_nat (stirling (Suc (Suc n)) 2)::'a) =
   1.131 +      of_nat (Suc n * stirling (Suc n) 2 + stirling (Suc n) (Suc 0))"
   1.132 +      by (simp only: stirling.simps(4)[of "Suc n"] numerals(2))
   1.133 +    also have "... = of_nat (Suc n) * (fact n * (\<Sum>k = 1..n. 1 / of_nat k)) + fact n"
   1.134 +      using Suc.hyps[OF geq1]
   1.135 +      by (simp only: stirling_Suc_n_1 of_nat_fact of_nat_add of_nat_mult)
   1.136 +    also have "... = fact (Suc n) * (\<Sum>k = 1..n. 1 / of_nat k) + fact (Suc n) * (1 / of_nat (Suc n))"
   1.137 +      using of_nat_neq_0 by auto
   1.138 +    also have "... = fact (Suc n) * (\<Sum>k = 1..Suc n. 1 / of_nat k)"
   1.139 +      by (simp add: distrib_left)
   1.140 +    finally show ?thesis .
   1.141 +  qed
   1.142 +qed
   1.143 +
   1.144 +lemma setsum_stirling:
   1.145 +  "(\<Sum>k\<le>n. stirling n k) = fact n"
   1.146 +proof (induct n)
   1.147 +  case 0
   1.148 +  from this show ?case by simp
   1.149 +next
   1.150 +  case (Suc n)
   1.151 +  have "(\<Sum>k\<le>Suc n. stirling (Suc n) k) = stirling (Suc n) 0 + (\<Sum>k\<le>n. stirling (Suc n) (Suc k))"
   1.152 +    by (simp only: setsum_atMost_Suc_shift)
   1.153 +  also have "\<dots> = (\<Sum>k\<le>n. stirling (Suc n) (Suc k))" by simp
   1.154 +  also have "\<dots> = (\<Sum>k\<le>n. n * stirling n (Suc k) + stirling n k)" by simp
   1.155 +  also have "\<dots> = n * (\<Sum>k\<le>n. stirling n (Suc k)) + (\<Sum>k\<le>n. stirling n k)"
   1.156 +    by (simp add: setsum.distrib setsum_right_distrib)
   1.157 +  also have "\<dots> = n * fact n + fact n"
   1.158 +  proof -
   1.159 +    have "n * (\<Sum>k\<le>n. stirling n (Suc k)) = n * ((\<Sum>k\<le>Suc n. stirling n k) - stirling n 0)"
   1.160 +      by (metis add_diff_cancel_left' setsum_atMost_Suc_shift)
   1.161 +    also have "\<dots> = n * (\<Sum>k\<le>n. stirling n k)" by (cases n) simp+
   1.162 +    also have "\<dots> = n * fact n" using Suc.hyps by simp
   1.163 +    finally have "n * (\<Sum>k\<le>n. stirling n (Suc k)) = n * fact n" .
   1.164 +    moreover have "(\<Sum>k\<le>n. stirling n k) = fact n" using Suc.hyps .
   1.165 +    ultimately show ?thesis by simp
   1.166 +  qed
   1.167 +  also have "\<dots> = fact (Suc n)" by simp
   1.168 +  finally show ?case .
   1.169 +qed
   1.170 +
   1.171 +lemma stirling_pochhammer:
   1.172 +  "(\<Sum>k\<le>n. of_nat (stirling n k) * x ^ k) = (pochhammer x n :: 'a :: comm_semiring_1)"
   1.173 +proof (induction n)
   1.174 +  case (Suc n)
   1.175 +  have "of_nat (n * stirling n 0) = (0 :: 'a)" by (cases n) simp_all
   1.176 +  hence "(\<Sum>k\<le>Suc n. of_nat (stirling (Suc n) k) * x ^ k) =
   1.177 +            (of_nat (n * stirling n 0) * x ^ 0 +
   1.178 +            (\<Sum>i\<le>n. of_nat (n * stirling n (Suc i)) * (x ^ Suc i))) +
   1.179 +            (\<Sum>i\<le>n. of_nat (stirling n i) * (x ^ Suc i))"
   1.180 +    by (subst setsum_atMost_Suc_shift) (simp add: setsum.distrib ring_distribs)
   1.181 +  also have "\<dots> = pochhammer x (Suc n)"
   1.182 +    by (subst setsum_atMost_Suc_shift [symmetric])
   1.183 +       (simp add: algebra_simps setsum.distrib setsum_right_distrib pochhammer_Suc Suc [symmetric])
   1.184 +  finally show ?case .
   1.185 +qed simp_all
   1.186 +
   1.187 +
   1.188 +text \<open>A row of the Stirling number triangle\<close>
   1.189 +
   1.190 +definition stirling_row :: "nat \<Rightarrow> nat list" where
   1.191 +  "stirling_row n = [stirling n k. k \<leftarrow> [0..<Suc n]]"
   1.192 +
   1.193 +lemma nth_stirling_row: "k \<le> n \<Longrightarrow> stirling_row n ! k = stirling n k"
   1.194 +  by (simp add: stirling_row_def del: upt_Suc)
   1.195 +
   1.196 +lemma length_stirling_row [simp]: "length (stirling_row n) = Suc n"
   1.197 +  by (simp add: stirling_row_def)
   1.198 +
   1.199 +lemma stirling_row_nonempty [simp]: "stirling_row n \<noteq> []"
   1.200 +  using length_stirling_row[of n] by (auto simp del: length_stirling_row)
   1.201 +
   1.202 +(* TODO Move *)
   1.203 +lemma list_ext:
   1.204 +  assumes "length xs = length ys"
   1.205 +  assumes "\<And>i. i < length xs \<Longrightarrow> xs ! i = ys ! i"
   1.206 +  shows   "xs = ys"
   1.207 +using assms
   1.208 +proof (induction rule: list_induct2)
   1.209 +  case (Cons x xs y ys)
   1.210 +  from Cons.prems[of 0] have "x = y" by simp
   1.211 +  moreover from Cons.prems[of "Suc i" for i] have "xs = ys" by (intro Cons.IH) simp
   1.212 +  ultimately show ?case by simp
   1.213 +qed simp_all
   1.214 +
   1.215 +
   1.216 +subsubsection \<open>Efficient code\<close>
   1.217 +
   1.218 +text \<open>
   1.219 +  Naively using the defining equations of the Stirling numbers of the first kind to
   1.220 +  compute them leads to exponential run time due to repeated computations.
   1.221 +  We can use memoisation to compute them row by row without repeating computations, at
   1.222 +  the cost of computing a few unneeded values.
   1.223 +
   1.224 +  As a bonus, this is very efficient for applications where an entire row of
   1.225 +  Stirling numbers is needed..
   1.226 +\<close>
   1.227 +
   1.228 +definition zip_with_prev :: "('a \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'b list" where
   1.229 +  "zip_with_prev f x xs = map (\<lambda>(x,y). f x y) (zip (x # xs) xs)"
   1.230 +
   1.231 +lemma zip_with_prev_altdef:
   1.232 +  "zip_with_prev f x xs =
   1.233 +     (if xs = [] then [] else f x (hd xs) # [f (xs!i) (xs!(i+1)). i \<leftarrow> [0..<length xs - 1]])"
   1.234 +proof (cases xs)
   1.235 +  case (Cons y ys)
   1.236 +  hence "zip_with_prev f x xs = f x (hd xs) # zip_with_prev f y ys"
   1.237 +    by (simp add: zip_with_prev_def)
   1.238 +  also have "zip_with_prev f y ys = map (\<lambda>i. f (xs ! i) (xs ! (i + 1))) [0..<length xs - 1]"
   1.239 +    unfolding Cons
   1.240 +    by (induction ys arbitrary: y)
   1.241 +       (simp_all add: zip_with_prev_def upt_conv_Cons map_Suc_upt [symmetric] del: upt_Suc)
   1.242 +  finally show ?thesis using Cons by simp
   1.243 +qed (simp add: zip_with_prev_def)
   1.244 +
   1.245 +
   1.246 +primrec stirling_row_aux where
   1.247 +  "stirling_row_aux n y [] = [1]"
   1.248 +| "stirling_row_aux n y (x#xs) = (y + n * x) # stirling_row_aux n x xs"
   1.249 +
   1.250 +lemma stirling_row_aux_correct:
   1.251 +  "stirling_row_aux n y xs = zip_with_prev (\<lambda>a b. a + n * b) y xs @ [1]"
   1.252 +  by (induction xs arbitrary: y) (simp_all add: zip_with_prev_def)
   1.253 +
   1.254 +lemma stirling_row_code [code]:
   1.255 +  "stirling_row 0 = [1]"
   1.256 +  "stirling_row (Suc n) = stirling_row_aux n 0 (stirling_row n)"
   1.257 +proof -
   1.258 +  have "stirling_row (Suc n) =
   1.259 +          0 # [stirling_row n ! i + stirling_row n ! (i+1) * n. i \<leftarrow> [0..<n]] @ [1]"
   1.260 +  proof (rule list_ext, goal_cases length nth)
   1.261 +    case (nth i)
   1.262 +    from nth have "i \<le> Suc n" by simp
   1.263 +    then consider "i = 0" | j where "i > 0" "i \<le> n" | "i = Suc n" by linarith
   1.264 +    thus ?case
   1.265 +    proof cases
   1.266 +      assume i: "i > 0" "i \<le> n"
   1.267 +      from this show ?thesis by (cases i) (simp_all add: nth_append nth_stirling_row)
   1.268 +    qed (simp_all add: nth_stirling_row nth_append)
   1.269 +  qed simp
   1.270 +  also have "0 # [stirling_row n ! i + stirling_row n ! (i+1) * n. i \<leftarrow> [0..<n]] @ [1] =
   1.271 +               zip_with_prev (\<lambda>a b. a + n * b) 0 (stirling_row n) @ [1]"
   1.272 +    by (cases n) (auto simp add: zip_with_prev_altdef stirling_row_def hd_map simp del: upt_Suc)
   1.273 +  also have "\<dots> = stirling_row_aux n 0 (stirling_row n)"
   1.274 +    by (simp add: stirling_row_aux_correct)
   1.275 +  finally show "stirling_row (Suc n) = stirling_row_aux n 0 (stirling_row n)" .
   1.276 +qed (simp add: stirling_row_def)
   1.277 +
   1.278 +lemma stirling_code [code]:
   1.279 +  "stirling n k = (if k = 0 then if n = 0 then 1 else 0
   1.280 +                   else if k > n then 0 else if k = n then 1
   1.281 +                   else stirling_row n ! k)"
   1.282 +  by (simp add: nth_stirling_row)
   1.283 +
   1.284 +end