src/HOL/Fact.thy
author bulwahn
Tue Oct 19 12:26:38 2010 +0200 (2010-10-19)
changeset 40033 84200d970bf0
parent 35644 d20cf282342e
child 41550 efa734d9b221
permissions -rw-r--r--
added some facts about factorial and dvd, div and mod
paulson@15094
     1
(*  Title       : Fact.thy
paulson@12196
     2
    Author      : Jacques D. Fleuriot
paulson@12196
     3
    Copyright   : 1998  University of Cambridge
paulson@15094
     4
    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
avigad@32036
     5
    The integer version of factorial and other additions by Jeremy Avigad.
paulson@12196
     6
*)
paulson@12196
     7
paulson@15094
     8
header{*Factorial Function*}
paulson@15094
     9
nipkow@15131
    10
theory Fact
haftmann@33319
    11
imports Main
nipkow@15131
    12
begin
paulson@15094
    13
avigad@32036
    14
class fact =
avigad@32036
    15
avigad@32036
    16
fixes 
avigad@32036
    17
  fact :: "'a \<Rightarrow> 'a"
avigad@32036
    18
avigad@32036
    19
instantiation nat :: fact
avigad@32036
    20
avigad@32036
    21
begin 
avigad@32036
    22
avigad@32036
    23
fun
avigad@32036
    24
  fact_nat :: "nat \<Rightarrow> nat"
avigad@32036
    25
where
avigad@32036
    26
  fact_0_nat: "fact_nat 0 = Suc 0"
avigad@32047
    27
| fact_Suc: "fact_nat (Suc x) = Suc x * fact x"
avigad@32036
    28
avigad@32036
    29
instance proof qed
avigad@32036
    30
avigad@32036
    31
end
avigad@32036
    32
avigad@32036
    33
(* definitions for the integers *)
avigad@32036
    34
avigad@32036
    35
instantiation int :: fact
avigad@32036
    36
avigad@32036
    37
begin 
avigad@32036
    38
avigad@32036
    39
definition
avigad@32036
    40
  fact_int :: "int \<Rightarrow> int"
avigad@32036
    41
where  
avigad@32036
    42
  "fact_int x = (if x >= 0 then int (fact (nat x)) else 0)"
avigad@32036
    43
avigad@32036
    44
instance proof qed
avigad@32036
    45
avigad@32036
    46
end
avigad@32036
    47
avigad@32036
    48
avigad@32036
    49
subsection {* Set up Transfer *}
avigad@32036
    50
avigad@32036
    51
lemma transfer_nat_int_factorial:
avigad@32036
    52
  "(x::int) >= 0 \<Longrightarrow> fact (nat x) = nat (fact x)"
avigad@32036
    53
  unfolding fact_int_def
avigad@32036
    54
  by auto
avigad@32036
    55
avigad@32036
    56
avigad@32036
    57
lemma transfer_nat_int_factorial_closure:
avigad@32036
    58
  "x >= (0::int) \<Longrightarrow> fact x >= 0"
avigad@32036
    59
  by (auto simp add: fact_int_def)
avigad@32036
    60
haftmann@35644
    61
declare transfer_morphism_nat_int[transfer add return: 
avigad@32036
    62
    transfer_nat_int_factorial transfer_nat_int_factorial_closure]
avigad@32036
    63
avigad@32036
    64
lemma transfer_int_nat_factorial:
avigad@32036
    65
  "fact (int x) = int (fact x)"
avigad@32036
    66
  unfolding fact_int_def by auto
avigad@32036
    67
avigad@32036
    68
lemma transfer_int_nat_factorial_closure:
avigad@32036
    69
  "is_nat x \<Longrightarrow> fact x >= 0"
avigad@32036
    70
  by (auto simp add: fact_int_def)
avigad@32036
    71
haftmann@35644
    72
declare transfer_morphism_int_nat[transfer add return: 
avigad@32036
    73
    transfer_int_nat_factorial transfer_int_nat_factorial_closure]
paulson@15094
    74
paulson@15094
    75
avigad@32036
    76
subsection {* Factorial *}
avigad@32036
    77
avigad@32036
    78
lemma fact_0_int [simp]: "fact (0::int) = 1"
avigad@32036
    79
  by (simp add: fact_int_def)
avigad@32036
    80
avigad@32036
    81
lemma fact_1_nat [simp]: "fact (1::nat) = 1"
avigad@32036
    82
  by simp
avigad@32036
    83
avigad@32036
    84
lemma fact_Suc_0_nat [simp]: "fact (Suc 0) = Suc 0"
avigad@32036
    85
  by simp
avigad@32036
    86
avigad@32036
    87
lemma fact_1_int [simp]: "fact (1::int) = 1"
avigad@32036
    88
  by (simp add: fact_int_def)
avigad@32036
    89
avigad@32036
    90
lemma fact_plus_one_nat: "fact ((n::nat) + 1) = (n + 1) * fact n"
avigad@32036
    91
  by simp
avigad@32036
    92
avigad@32036
    93
lemma fact_plus_one_int: 
avigad@32036
    94
  assumes "n >= 0"
avigad@32036
    95
  shows "fact ((n::int) + 1) = (n + 1) * fact n"
avigad@32036
    96
avigad@32036
    97
  using prems unfolding fact_int_def 
avigad@32036
    98
  by (simp add: nat_add_distrib algebra_simps int_mult)
avigad@32036
    99
avigad@32036
   100
lemma fact_reduce_nat: "(n::nat) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
avigad@32036
   101
  apply (subgoal_tac "n = Suc (n - 1)")
avigad@32036
   102
  apply (erule ssubst)
avigad@32047
   103
  apply (subst fact_Suc)
avigad@32036
   104
  apply simp_all
avigad@32036
   105
done
avigad@32036
   106
avigad@32036
   107
lemma fact_reduce_int: "(n::int) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
avigad@32036
   108
  apply (subgoal_tac "n = (n - 1) + 1")
avigad@32036
   109
  apply (erule ssubst)
avigad@32036
   110
  apply (subst fact_plus_one_int)
avigad@32036
   111
  apply simp_all
avigad@32036
   112
done
avigad@32036
   113
avigad@32036
   114
lemma fact_nonzero_nat [simp]: "fact (n::nat) \<noteq> 0"
avigad@32036
   115
  apply (induct n)
avigad@32036
   116
  apply (auto simp add: fact_plus_one_nat)
avigad@32036
   117
done
avigad@32036
   118
avigad@32036
   119
lemma fact_nonzero_int [simp]: "n >= 0 \<Longrightarrow> fact (n::int) ~= 0"
avigad@32036
   120
  by (simp add: fact_int_def)
avigad@32036
   121
avigad@32036
   122
lemma fact_gt_zero_nat [simp]: "fact (n :: nat) > 0"
avigad@32036
   123
  by (insert fact_nonzero_nat [of n], arith)
avigad@32036
   124
avigad@32036
   125
lemma fact_gt_zero_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) > 0"
avigad@32036
   126
  by (auto simp add: fact_int_def)
avigad@32036
   127
avigad@32036
   128
lemma fact_ge_one_nat [simp]: "fact (n :: nat) >= 1"
avigad@32036
   129
  by (insert fact_nonzero_nat [of n], arith)
avigad@32036
   130
avigad@32036
   131
lemma fact_ge_Suc_0_nat [simp]: "fact (n :: nat) >= Suc 0"
avigad@32036
   132
  by (insert fact_nonzero_nat [of n], arith)
avigad@32036
   133
avigad@32036
   134
lemma fact_ge_one_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) >= 1"
avigad@32036
   135
  apply (auto simp add: fact_int_def)
avigad@32036
   136
  apply (subgoal_tac "1 = int 1")
avigad@32036
   137
  apply (erule ssubst)
avigad@32036
   138
  apply (subst zle_int)
avigad@32036
   139
  apply auto
avigad@32036
   140
done
avigad@32036
   141
avigad@32036
   142
lemma dvd_fact_nat [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::nat)"
avigad@32036
   143
  apply (induct n)
avigad@32036
   144
  apply force
avigad@32047
   145
  apply (auto simp only: fact_Suc)
avigad@32036
   146
  apply (subgoal_tac "m = Suc n")
avigad@32036
   147
  apply (erule ssubst)
avigad@32036
   148
  apply (rule dvd_triv_left)
avigad@32036
   149
  apply auto
avigad@32036
   150
done
avigad@32036
   151
avigad@32036
   152
lemma dvd_fact_int [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::int)"
avigad@32036
   153
  apply (case_tac "1 <= n")
avigad@32036
   154
  apply (induct n rule: int_ge_induct)
avigad@32036
   155
  apply (auto simp add: fact_plus_one_int)
avigad@32036
   156
  apply (subgoal_tac "m = i + 1")
avigad@32036
   157
  apply auto
avigad@32036
   158
done
avigad@32036
   159
avigad@32036
   160
lemma interval_plus_one_nat: "(i::nat) <= j + 1 \<Longrightarrow> 
avigad@32036
   161
  {i..j+1} = {i..j} Un {j+1}"
avigad@32036
   162
  by auto
avigad@32036
   163
avigad@32036
   164
lemma interval_Suc: "i <= Suc j \<Longrightarrow> {i..Suc j} = {i..j} Un {Suc j}"
avigad@32036
   165
  by auto
avigad@32036
   166
avigad@32036
   167
lemma interval_plus_one_int: "(i::int) <= j + 1 \<Longrightarrow> {i..j+1} = {i..j} Un {j+1}"
avigad@32036
   168
  by auto
paulson@15094
   169
avigad@32036
   170
lemma fact_altdef_nat: "fact (n::nat) = (PROD i:{1..n}. i)"
avigad@32036
   171
  apply (induct n)
avigad@32036
   172
  apply force
avigad@32047
   173
  apply (subst fact_Suc)
avigad@32036
   174
  apply (subst interval_Suc)
avigad@32036
   175
  apply auto
avigad@32036
   176
done
avigad@32036
   177
avigad@32036
   178
lemma fact_altdef_int: "n >= 0 \<Longrightarrow> fact (n::int) = (PROD i:{1..n}. i)"
avigad@32036
   179
  apply (induct n rule: int_ge_induct)
avigad@32036
   180
  apply force
avigad@32036
   181
  apply (subst fact_plus_one_int, assumption)
avigad@32036
   182
  apply (subst interval_plus_one_int)
avigad@32036
   183
  apply auto
avigad@32036
   184
done
avigad@32036
   185
bulwahn@40033
   186
lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd fact (m::nat)"
bulwahn@40033
   187
  by (auto simp add: fact_altdef_nat intro!: setprod_dvd_setprod_subset)
bulwahn@40033
   188
bulwahn@40033
   189
lemma fact_mod: "m \<le> (n::nat) \<Longrightarrow> fact n mod fact m = 0"
bulwahn@40033
   190
  by (auto simp add: dvd_imp_mod_0 fact_dvd)
bulwahn@40033
   191
bulwahn@40033
   192
lemma fact_div_fact:
bulwahn@40033
   193
  assumes "m \<ge> (n :: nat)"
bulwahn@40033
   194
  shows "(fact m) div (fact n) = \<Prod>{n + 1..m}"
bulwahn@40033
   195
proof -
bulwahn@40033
   196
  obtain d where "d = m - n" by auto
bulwahn@40033
   197
  from assms this have "m = n + d" by auto
bulwahn@40033
   198
  have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
bulwahn@40033
   199
  proof (induct d)
bulwahn@40033
   200
    case 0
bulwahn@40033
   201
    show ?case by simp
bulwahn@40033
   202
  next
bulwahn@40033
   203
    case (Suc d')
bulwahn@40033
   204
    have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
bulwahn@40033
   205
      by simp
bulwahn@40033
   206
    also from Suc.hyps have "... = Suc (n + d') * \<Prod>{n + 1..n + d'}" 
bulwahn@40033
   207
      unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
bulwahn@40033
   208
    also have "... = \<Prod>{n + 1..n + Suc d'}"
bulwahn@40033
   209
      by (simp add: atLeastAtMostSuc_conv setprod_insert)
bulwahn@40033
   210
    finally show ?case .
bulwahn@40033
   211
  qed
bulwahn@40033
   212
  from this `m = n + d` show ?thesis by simp
bulwahn@40033
   213
qed
bulwahn@40033
   214
avigad@32036
   215
lemma fact_mono_nat: "(m::nat) \<le> n \<Longrightarrow> fact m \<le> fact n"
avigad@32036
   216
apply (drule le_imp_less_or_eq)
avigad@32036
   217
apply (auto dest!: less_imp_Suc_add)
avigad@32036
   218
apply (induct_tac k, auto)
avigad@32036
   219
done
avigad@32036
   220
avigad@32036
   221
lemma fact_neg_int [simp]: "m < (0::int) \<Longrightarrow> fact m = 0"
avigad@32036
   222
  unfolding fact_int_def by auto
avigad@32036
   223
avigad@32036
   224
lemma fact_ge_zero_int [simp]: "fact m >= (0::int)"
avigad@32036
   225
  apply (case_tac "m >= 0")
avigad@32036
   226
  apply auto
avigad@32036
   227
  apply (frule fact_gt_zero_int)
avigad@32036
   228
  apply arith
avigad@32036
   229
done
avigad@32036
   230
avigad@32036
   231
lemma fact_mono_int_aux [rule_format]: "k >= (0::int) \<Longrightarrow> 
avigad@32036
   232
    fact (m + k) >= fact m"
avigad@32036
   233
  apply (case_tac "m < 0")
avigad@32036
   234
  apply auto
avigad@32036
   235
  apply (induct k rule: int_ge_induct)
avigad@32036
   236
  apply auto
avigad@32036
   237
  apply (subst add_assoc [symmetric])
avigad@32036
   238
  apply (subst fact_plus_one_int)
avigad@32036
   239
  apply auto
avigad@32036
   240
  apply (erule order_trans)
avigad@32036
   241
  apply (subst mult_le_cancel_right1)
avigad@32036
   242
  apply (subgoal_tac "fact (m + i) >= 0")
avigad@32036
   243
  apply arith
avigad@32036
   244
  apply auto
avigad@32036
   245
done
avigad@32036
   246
avigad@32036
   247
lemma fact_mono_int: "(m::int) <= n \<Longrightarrow> fact m <= fact n"
avigad@32036
   248
  apply (insert fact_mono_int_aux [of "n - m" "m"])
avigad@32036
   249
  apply auto
avigad@32036
   250
done
avigad@32036
   251
avigad@32036
   252
text{*Note that @{term "fact 0 = fact 1"}*}
avigad@32036
   253
lemma fact_less_mono_nat: "[| (0::nat) < m; m < n |] ==> fact m < fact n"
avigad@32036
   254
apply (drule_tac m = m in less_imp_Suc_add, auto)
avigad@32036
   255
apply (induct_tac k, auto)
avigad@32036
   256
done
avigad@32036
   257
avigad@32036
   258
lemma fact_less_mono_int_aux: "k >= 0 \<Longrightarrow> (0::int) < m \<Longrightarrow>
avigad@32036
   259
    fact m < fact ((m + 1) + k)"
avigad@32036
   260
  apply (induct k rule: int_ge_induct)
avigad@32036
   261
  apply (simp add: fact_plus_one_int)
avigad@32036
   262
  apply (subst mult_less_cancel_right1)
avigad@32036
   263
  apply (insert fact_gt_zero_int [of m], arith)
avigad@32036
   264
  apply (subst (2) fact_reduce_int)
avigad@32036
   265
  apply (auto simp add: add_ac)
avigad@32036
   266
  apply (erule order_less_le_trans)
avigad@32036
   267
  apply (subst mult_le_cancel_right1)
avigad@32036
   268
  apply auto
avigad@32036
   269
  apply (subgoal_tac "fact (i + (1 + m)) >= 0")
avigad@32036
   270
  apply force
avigad@32036
   271
  apply (rule fact_ge_zero_int)
avigad@32036
   272
done
avigad@32036
   273
avigad@32036
   274
lemma fact_less_mono_int: "(0::int) < m \<Longrightarrow> m < n \<Longrightarrow> fact m < fact n"
avigad@32036
   275
  apply (insert fact_less_mono_int_aux [of "n - (m + 1)" "m"])
avigad@32036
   276
  apply auto
avigad@32036
   277
done
avigad@32036
   278
avigad@32036
   279
lemma fact_num_eq_if_nat: "fact (m::nat) = 
avigad@32036
   280
  (if m=0 then 1 else m * fact (m - 1))"
avigad@32036
   281
by (cases m) auto
avigad@32036
   282
avigad@32036
   283
lemma fact_add_num_eq_if_nat:
avigad@32036
   284
  "fact ((m::nat) + n) = (if m + n = 0 then 1 else (m + n) * fact (m + n - 1))"
avigad@32036
   285
by (cases "m + n") auto
avigad@32036
   286
avigad@32036
   287
lemma fact_add_num_eq_if2_nat:
avigad@32036
   288
  "fact ((m::nat) + n) = 
avigad@32036
   289
    (if m = 0 then fact n else (m + n) * fact ((m - 1) + n))"
avigad@32036
   290
by (cases m) auto
avigad@32036
   291
avigad@32036
   292
berghofe@32039
   293
subsection {* @{term fact} and @{term of_nat} *}
paulson@15094
   294
chaieb@29693
   295
lemma of_nat_fact_not_zero [simp]: "of_nat (fact n) \<noteq> (0::'a::semiring_char_0)"
nipkow@25134
   296
by auto
paulson@15094
   297
haftmann@35028
   298
lemma of_nat_fact_gt_zero [simp]: "(0::'a::{linordered_semidom}) < of_nat(fact n)" by auto
chaieb@29693
   299
haftmann@35028
   300
lemma of_nat_fact_ge_zero [simp]: "(0::'a::linordered_semidom) \<le> of_nat(fact n)"
nipkow@25134
   301
by simp
paulson@15094
   302
haftmann@35028
   303
lemma inv_of_nat_fact_gt_zero [simp]: "(0::'a::linordered_field) < inverse (of_nat (fact n))"
nipkow@25134
   304
by (auto simp add: positive_imp_inverse_positive)
paulson@15094
   305
haftmann@35028
   306
lemma inv_of_nat_fact_ge_zero [simp]: "(0::'a::linordered_field) \<le> inverse (of_nat (fact n))"
nipkow@25134
   307
by (auto intro: order_less_imp_le)
paulson@15094
   308
nipkow@15131
   309
end