library theories for debugging and parallel computing using code generation towards Isabelle/ML
2 Author : Jacques D. Fleuriot
3 Copyright : 1998 University of Cambridge
4 Conversion to Isar and new proofs by Lawrence C Paulson, 2004
5 The integer version of factorial and other additions by Jeremy Avigad.
8 header{*Factorial Function*}
15 fixes fact :: "'a \<Rightarrow> 'a"
17 instantiation nat :: fact
21 fact_nat :: "nat \<Rightarrow> nat"
23 fact_0_nat: "fact_nat 0 = Suc 0"
24 | fact_Suc: "fact_nat (Suc x) = Suc x * fact x"
30 (* definitions for the integers *)
32 instantiation int :: fact
37 fact_int :: "int \<Rightarrow> int"
39 "fact_int x = (if x >= 0 then int (fact (nat x)) else 0)"
46 subsection {* Set up Transfer *}
48 lemma transfer_nat_int_factorial:
49 "(x::int) >= 0 \<Longrightarrow> fact (nat x) = nat (fact x)"
50 unfolding fact_int_def
54 lemma transfer_nat_int_factorial_closure:
55 "x >= (0::int) \<Longrightarrow> fact x >= 0"
56 by (auto simp add: fact_int_def)
58 declare transfer_morphism_nat_int[transfer add return:
59 transfer_nat_int_factorial transfer_nat_int_factorial_closure]
61 lemma transfer_int_nat_factorial:
62 "fact (int x) = int (fact x)"
63 unfolding fact_int_def by auto
65 lemma transfer_int_nat_factorial_closure:
66 "is_nat x \<Longrightarrow> fact x >= 0"
67 by (auto simp add: fact_int_def)
69 declare transfer_morphism_int_nat[transfer add return:
70 transfer_int_nat_factorial transfer_int_nat_factorial_closure]
73 subsection {* Factorial *}
75 lemma fact_0_int [simp]: "fact (0::int) = 1"
76 by (simp add: fact_int_def)
78 lemma fact_1_nat [simp]: "fact (1::nat) = 1"
81 lemma fact_Suc_0_nat [simp]: "fact (Suc 0) = Suc 0"
84 lemma fact_1_int [simp]: "fact (1::int) = 1"
85 by (simp add: fact_int_def)
87 lemma fact_plus_one_nat: "fact ((n::nat) + 1) = (n + 1) * fact n"
90 lemma fact_plus_one_int:
92 shows "fact ((n::int) + 1) = (n + 1) * fact n"
93 using assms unfolding fact_int_def
94 by (simp add: nat_add_distrib algebra_simps int_mult)
96 lemma fact_reduce_nat: "(n::nat) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
97 apply (subgoal_tac "n = Suc (n - 1)")
99 apply (subst fact_Suc)
103 lemma fact_reduce_int: "(n::int) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
104 apply (subgoal_tac "n = (n - 1) + 1")
106 apply (subst fact_plus_one_int)
110 lemma fact_nonzero_nat [simp]: "fact (n::nat) \<noteq> 0"
112 apply (auto simp add: fact_plus_one_nat)
115 lemma fact_nonzero_int [simp]: "n >= 0 \<Longrightarrow> fact (n::int) ~= 0"
116 by (simp add: fact_int_def)
118 lemma fact_gt_zero_nat [simp]: "fact (n :: nat) > 0"
119 by (insert fact_nonzero_nat [of n], arith)
121 lemma fact_gt_zero_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) > 0"
122 by (auto simp add: fact_int_def)
124 lemma fact_ge_one_nat [simp]: "fact (n :: nat) >= 1"
125 by (insert fact_nonzero_nat [of n], arith)
127 lemma fact_ge_Suc_0_nat [simp]: "fact (n :: nat) >= Suc 0"
128 by (insert fact_nonzero_nat [of n], arith)
130 lemma fact_ge_one_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) >= 1"
131 apply (auto simp add: fact_int_def)
132 apply (subgoal_tac "1 = int 1")
134 apply (subst zle_int)
138 lemma dvd_fact_nat [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::nat)"
141 apply (auto simp only: fact_Suc)
142 apply (subgoal_tac "m = Suc n")
144 apply (rule dvd_triv_left)
148 lemma dvd_fact_int [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::int)"
149 apply (case_tac "1 <= n")
150 apply (induct n rule: int_ge_induct)
151 apply (auto simp add: fact_plus_one_int)
152 apply (subgoal_tac "m = i + 1")
156 lemma interval_plus_one_nat: "(i::nat) <= j + 1 \<Longrightarrow>
157 {i..j+1} = {i..j} Un {j+1}"
160 lemma interval_Suc: "i <= Suc j \<Longrightarrow> {i..Suc j} = {i..j} Un {Suc j}"
163 lemma interval_plus_one_int: "(i::int) <= j + 1 \<Longrightarrow> {i..j+1} = {i..j} Un {j+1}"
166 lemma fact_altdef_nat: "fact (n::nat) = (PROD i:{1..n}. i)"
169 apply (subst fact_Suc)
170 apply (subst interval_Suc)
174 lemma fact_altdef_int: "n >= 0 \<Longrightarrow> fact (n::int) = (PROD i:{1..n}. i)"
175 apply (induct n rule: int_ge_induct)
177 apply (subst fact_plus_one_int, assumption)
178 apply (subst interval_plus_one_int)
182 lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd fact (m::nat)"
183 by (auto simp add: fact_altdef_nat intro!: setprod_dvd_setprod_subset)
185 lemma fact_mod: "m \<le> (n::nat) \<Longrightarrow> fact n mod fact m = 0"
186 by (auto simp add: dvd_imp_mod_0 fact_dvd)
189 assumes "m \<ge> (n :: nat)"
190 shows "(fact m) div (fact n) = \<Prod>{n + 1..m}"
192 obtain d where "d = m - n" by auto
193 from assms this have "m = n + d" by auto
194 have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
200 have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
202 also from Suc.hyps have "... = Suc (n + d') * \<Prod>{n + 1..n + d'}"
203 unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
204 also have "... = \<Prod>{n + 1..n + Suc d'}"
205 by (simp add: atLeastAtMostSuc_conv setprod_insert)
208 from this `m = n + d` show ?thesis by simp
211 lemma fact_mono_nat: "(m::nat) \<le> n \<Longrightarrow> fact m \<le> fact n"
212 apply (drule le_imp_less_or_eq)
213 apply (auto dest!: less_imp_Suc_add)
214 apply (induct_tac k, auto)
217 lemma fact_neg_int [simp]: "m < (0::int) \<Longrightarrow> fact m = 0"
218 unfolding fact_int_def by auto
220 lemma fact_ge_zero_int [simp]: "fact m >= (0::int)"
221 apply (case_tac "m >= 0")
223 apply (frule fact_gt_zero_int)
227 lemma fact_mono_int_aux [rule_format]: "k >= (0::int) \<Longrightarrow>
228 fact (m + k) >= fact m"
229 apply (case_tac "m < 0")
231 apply (induct k rule: int_ge_induct)
233 apply (subst add_assoc [symmetric])
234 apply (subst fact_plus_one_int)
236 apply (erule order_trans)
237 apply (subst mult_le_cancel_right1)
238 apply (subgoal_tac "fact (m + i) >= 0")
243 lemma fact_mono_int: "(m::int) <= n \<Longrightarrow> fact m <= fact n"
244 apply (insert fact_mono_int_aux [of "n - m" "m"])
248 text{*Note that @{term "fact 0 = fact 1"}*}
249 lemma fact_less_mono_nat: "[| (0::nat) < m; m < n |] ==> fact m < fact n"
250 apply (drule_tac m = m in less_imp_Suc_add, auto)
251 apply (induct_tac k, auto)
254 lemma fact_less_mono_int_aux: "k >= 0 \<Longrightarrow> (0::int) < m \<Longrightarrow>
255 fact m < fact ((m + 1) + k)"
256 apply (induct k rule: int_ge_induct)
257 apply (simp add: fact_plus_one_int)
258 apply (subst (2) fact_reduce_int)
259 apply (auto simp add: add_ac)
260 apply (erule order_less_le_trans)
261 apply (subst mult_le_cancel_right1)
263 apply (subgoal_tac "fact (i + (1 + m)) >= 0")
265 apply (rule fact_ge_zero_int)
268 lemma fact_less_mono_int: "(0::int) < m \<Longrightarrow> m < n \<Longrightarrow> fact m < fact n"
269 apply (insert fact_less_mono_int_aux [of "n - (m + 1)" "m"])
273 lemma fact_num_eq_if_nat: "fact (m::nat) =
274 (if m=0 then 1 else m * fact (m - 1))"
277 lemma fact_add_num_eq_if_nat:
278 "fact ((m::nat) + n) = (if m + n = 0 then 1 else (m + n) * fact (m + n - 1))"
279 by (cases "m + n") auto
281 lemma fact_add_num_eq_if2_nat:
282 "fact ((m::nat) + n) =
283 (if m = 0 then fact n else (m + n) * fact ((m - 1) + n))"
286 lemma fact_le_power: "fact n \<le> n^n"
289 then have "fact n \<le> Suc n ^ n" by (rule le_trans) (simp add: power_mono)
290 then show ?case by (simp add: add_le_mono)
293 subsection {* @{term fact} and @{term of_nat} *}
295 lemma of_nat_fact_not_zero [simp]: "of_nat (fact n) \<noteq> (0::'a::semiring_char_0)"
298 lemma of_nat_fact_gt_zero [simp]: "(0::'a::{linordered_semidom}) < of_nat(fact n)" by auto
300 lemma of_nat_fact_ge_zero [simp]: "(0::'a::linordered_semidom) \<le> of_nat(fact n)"
303 lemma inv_of_nat_fact_gt_zero [simp]: "(0::'a::linordered_field) < inverse (of_nat (fact n))"
304 by (auto simp add: positive_imp_inverse_positive)
306 lemma inv_of_nat_fact_ge_zero [simp]: "(0::'a::linordered_field) \<le> inverse (of_nat (fact n))"
307 by (auto intro: order_less_imp_le)