src/HOL/Integ/IntArith.thy
author paulson
Mon Jan 12 16:51:45 2004 +0100 (2004-01-12)
changeset 14353 79f9fbef9106
parent 14295 7f115e5c5de4
child 14365 3d4df8c166ae
permissions -rw-r--r--
Added lemmas to Ring_and_Field with slightly modified simplification rules

Deleted some little-used integer theorems, replacing them by the generic ones
in Ring_and_Field

Consolidated integer powers
paulson@14259
     1
(*  Title:      HOL/Integ/IntArith.thy
paulson@14259
     2
    ID:         $Id$
paulson@14259
     3
    Authors:    Larry Paulson and Tobias Nipkow
paulson@14259
     4
*)
wenzelm@12023
     5
wenzelm@12023
     6
header {* Integer arithmetic *}
wenzelm@12023
     7
wenzelm@9436
     8
theory IntArith = Bin
paulson@14259
     9
files ("int_arith1.ML"):
wenzelm@9436
    10
paulson@14353
    11
paulson@14272
    12
subsection{*Inequality Reasoning for the Arithmetic Simproc*}
paulson@14272
    13
paulson@14272
    14
lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
paulson@14272
    15
  proof (auto simp add: zle_def zless_iff_Suc_zadd) 
paulson@14272
    16
  fix m n
paulson@14272
    17
  assume "w + 1 = w + (1 + int m) + (1 + int n)"
paulson@14272
    18
  hence "(w + 1) + (1 + int (m + n)) = (w + 1) + 0" 
paulson@14272
    19
    by (simp add: add_ac zadd_int [symmetric])
paulson@14272
    20
  hence "int (Suc(m+n)) = 0" 
paulson@14272
    21
    by (simp only: Ring_and_Field.add_left_cancel int_Suc)
paulson@14272
    22
  thus False by (simp only: int_eq_0_conv)
paulson@14272
    23
  qed
paulson@14272
    24
wenzelm@12023
    25
use "int_arith1.ML"
wenzelm@12023
    26
setup int_arith_setup
paulson@14259
    27
paulson@14353
    28
paulson@14272
    29
subsection{*More inequality reasoning*}
paulson@14272
    30
paulson@14272
    31
lemma zless_add1_eq: "(w < z + (1::int)) = (w<z | w=z)"
paulson@14259
    32
by arith
paulson@14259
    33
paulson@14272
    34
lemma add1_zle_eq: "(w + (1::int) \<le> z) = (w<z)"
paulson@14272
    35
by arith
paulson@14272
    36
paulson@14272
    37
lemma zle_diff1_eq [simp]: "(w \<le> z - (1::int)) = (w<(z::int))"
paulson@14272
    38
by arith
paulson@14272
    39
paulson@14272
    40
lemma zle_add1_eq_le [simp]: "(w < z + 1) = (w\<le>(z::int))"
paulson@14259
    41
by arith
paulson@14259
    42
paulson@14259
    43
lemma zadd_left_cancel0 [simp]: "(z = z + w) = (w = (0::int))"
paulson@14259
    44
by arith
paulson@14259
    45
paulson@14353
    46
paulson@14353
    47
subsection{*The Functions @{term nat} and @{term int}*}
paulson@14259
    48
paulson@14272
    49
lemma nonneg_eq_int: "[| 0 \<le> z;  !!m. z = int m ==> P |] ==> P"
paulson@14259
    50
by (blast dest: nat_0_le sym)
paulson@14259
    51
paulson@14272
    52
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
paulson@14259
    53
by auto
paulson@14259
    54
paulson@14272
    55
lemma nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
paulson@14259
    56
by auto
paulson@14259
    57
paulson@14272
    58
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)"
paulson@14259
    59
apply (rule iffI)
paulson@14259
    60
apply (erule nat_0_le [THEN subst])
paulson@14259
    61
apply (simp_all del: zless_int add: zless_int [symmetric]) 
paulson@14259
    62
done
paulson@14259
    63
paulson@14272
    64
lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)"
paulson@14259
    65
by (auto simp add: nat_eq_iff2)
paulson@14259
    66
paulson@14259
    67
paulson@14353
    68
text{*Simplify the terms @{term "int 0"}, @{term "int(Suc 0)"} and
paulson@14353
    69
  @{term "w + - z"}*}
paulson@14259
    70
declare Zero_int_def [symmetric, simp]
paulson@14259
    71
declare One_int_def [symmetric, simp]
paulson@14259
    72
paulson@14259
    73
text{*cooper.ML refers to this theorem*}
paulson@14259
    74
lemmas zdiff_def_symmetric = zdiff_def [symmetric, simp]
paulson@14259
    75
paulson@14259
    76
lemma nat_0: "nat 0 = 0"
paulson@14259
    77
by (simp add: nat_eq_iff)
paulson@14259
    78
paulson@14259
    79
lemma nat_1: "nat 1 = Suc 0"
paulson@14259
    80
by (subst nat_eq_iff, simp)
paulson@14259
    81
paulson@14259
    82
lemma nat_2: "nat 2 = Suc (Suc 0)"
paulson@14259
    83
by (subst nat_eq_iff, simp)
paulson@14259
    84
paulson@14272
    85
lemma nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
paulson@14259
    86
apply (case_tac "neg z")
paulson@14259
    87
apply (auto simp add: nat_less_iff)
paulson@14259
    88
apply (auto intro: zless_trans simp add: neg_eq_less_0 zle_def)
paulson@14259
    89
done
paulson@14259
    90
paulson@14272
    91
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
paulson@14259
    92
by (auto simp add: linorder_not_less [symmetric] zless_nat_conj)
paulson@14259
    93
paulson@14259
    94
paulson@14259
    95
text{*This simplifies expressions of the form @{term "int n = z"} where
paulson@14259
    96
      z is an integer literal.*}
paulson@14259
    97
declare int_eq_iff [of _ "number_of v", standard, simp]
paulson@13837
    98
paulson@13849
    99
lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
paulson@13849
   100
  by simp
paulson@13849
   101
paulson@14295
   102
lemma split_nat [arith_split]:
paulson@14259
   103
  "P(nat(i::int)) = ((\<forall>n. i = int n \<longrightarrow> P n) & (i < 0 \<longrightarrow> P 0))"
nipkow@13575
   104
  (is "?P = (?L & ?R)")
nipkow@13575
   105
proof (cases "i < 0")
nipkow@13575
   106
  case True thus ?thesis by simp
nipkow@13575
   107
next
nipkow@13575
   108
  case False
nipkow@13575
   109
  have "?P = ?L"
nipkow@13575
   110
  proof
nipkow@13575
   111
    assume ?P thus ?L using False by clarsimp
nipkow@13575
   112
  next
nipkow@13575
   113
    assume ?L thus ?P using False by simp
nipkow@13575
   114
  qed
nipkow@13575
   115
  with False show ?thesis by simp
nipkow@13575
   116
qed
nipkow@13575
   117
nipkow@13685
   118
subsubsection "Induction principles for int"
nipkow@13685
   119
nipkow@13685
   120
                     (* `set:int': dummy construction *)
nipkow@13685
   121
theorem int_ge_induct[case_names base step,induct set:int]:
nipkow@13685
   122
  assumes ge: "k \<le> (i::int)" and
nipkow@13685
   123
        base: "P(k)" and
nipkow@13685
   124
        step: "\<And>i. \<lbrakk>k \<le> i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
nipkow@13685
   125
  shows "P i"
nipkow@13685
   126
proof -
paulson@14272
   127
  { fix n have "\<And>i::int. n = nat(i-k) \<Longrightarrow> k \<le> i \<Longrightarrow> P i"
nipkow@13685
   128
    proof (induct n)
nipkow@13685
   129
      case 0
nipkow@13685
   130
      hence "i = k" by arith
nipkow@13685
   131
      thus "P i" using base by simp
nipkow@13685
   132
    next
nipkow@13685
   133
      case (Suc n)
nipkow@13685
   134
      hence "n = nat((i - 1) - k)" by arith
nipkow@13685
   135
      moreover
nipkow@13685
   136
      have ki1: "k \<le> i - 1" using Suc.prems by arith
nipkow@13685
   137
      ultimately
nipkow@13685
   138
      have "P(i - 1)" by(rule Suc.hyps)
nipkow@13685
   139
      from step[OF ki1 this] show ?case by simp
nipkow@13685
   140
    qed
nipkow@13685
   141
  }
nipkow@13685
   142
  from this ge show ?thesis by fast
nipkow@13685
   143
qed
nipkow@13685
   144
nipkow@13685
   145
                     (* `set:int': dummy construction *)
nipkow@13685
   146
theorem int_gr_induct[case_names base step,induct set:int]:
nipkow@13685
   147
  assumes gr: "k < (i::int)" and
nipkow@13685
   148
        base: "P(k+1)" and
nipkow@13685
   149
        step: "\<And>i. \<lbrakk>k < i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
nipkow@13685
   150
  shows "P i"
nipkow@13685
   151
apply(rule int_ge_induct[of "k + 1"])
nipkow@13685
   152
  using gr apply arith
nipkow@13685
   153
 apply(rule base)
paulson@14259
   154
apply (rule step, simp+)
nipkow@13685
   155
done
nipkow@13685
   156
nipkow@13685
   157
theorem int_le_induct[consumes 1,case_names base step]:
nipkow@13685
   158
  assumes le: "i \<le> (k::int)" and
nipkow@13685
   159
        base: "P(k)" and
nipkow@13685
   160
        step: "\<And>i. \<lbrakk>i \<le> k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
nipkow@13685
   161
  shows "P i"
nipkow@13685
   162
proof -
paulson@14272
   163
  { fix n have "\<And>i::int. n = nat(k-i) \<Longrightarrow> i \<le> k \<Longrightarrow> P i"
nipkow@13685
   164
    proof (induct n)
nipkow@13685
   165
      case 0
nipkow@13685
   166
      hence "i = k" by arith
nipkow@13685
   167
      thus "P i" using base by simp
nipkow@13685
   168
    next
nipkow@13685
   169
      case (Suc n)
nipkow@13685
   170
      hence "n = nat(k - (i+1))" by arith
nipkow@13685
   171
      moreover
nipkow@13685
   172
      have ki1: "i + 1 \<le> k" using Suc.prems by arith
nipkow@13685
   173
      ultimately
nipkow@13685
   174
      have "P(i+1)" by(rule Suc.hyps)
nipkow@13685
   175
      from step[OF ki1 this] show ?case by simp
nipkow@13685
   176
    qed
nipkow@13685
   177
  }
nipkow@13685
   178
  from this le show ?thesis by fast
nipkow@13685
   179
qed
nipkow@13685
   180
nipkow@13685
   181
theorem int_less_induct[consumes 1,case_names base step]:
nipkow@13685
   182
  assumes less: "(i::int) < k" and
nipkow@13685
   183
        base: "P(k - 1)" and
nipkow@13685
   184
        step: "\<And>i. \<lbrakk>i < k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
nipkow@13685
   185
  shows "P i"
nipkow@13685
   186
apply(rule int_le_induct[of _ "k - 1"])
nipkow@13685
   187
  using less apply arith
nipkow@13685
   188
 apply(rule base)
paulson@14259
   189
apply (rule step, simp+)
paulson@14259
   190
done
paulson@14259
   191
paulson@14259
   192
subsection{*Intermediate value theorems*}
paulson@14259
   193
paulson@14259
   194
lemma int_val_lemma:
paulson@14259
   195
     "(\<forall>i<n::nat. abs(f(i+1) - f i) \<le> 1) -->  
paulson@14259
   196
      f 0 \<le> k --> k \<le> f n --> (\<exists>i \<le> n. f i = (k::int))"
paulson@14271
   197
apply (induct_tac "n", simp)
paulson@14259
   198
apply (intro strip)
paulson@14259
   199
apply (erule impE, simp)
paulson@14259
   200
apply (erule_tac x = n in allE, simp)
paulson@14259
   201
apply (case_tac "k = f (n+1) ")
paulson@14259
   202
 apply force
paulson@14259
   203
apply (erule impE)
paulson@14259
   204
 apply (simp add: zabs_def split add: split_if_asm)
paulson@14259
   205
apply (blast intro: le_SucI)
paulson@14259
   206
done
paulson@14259
   207
paulson@14259
   208
lemmas nat0_intermed_int_val = int_val_lemma [rule_format (no_asm)]
paulson@14259
   209
paulson@14259
   210
lemma nat_intermed_int_val:
paulson@14259
   211
     "[| \<forall>i. m \<le> i & i < n --> abs(f(i + 1::nat) - f i) \<le> 1; m < n;  
paulson@14259
   212
         f m \<le> k; k \<le> f n |] ==> ? i. m \<le> i & i \<le> n & f i = (k::int)"
paulson@14259
   213
apply (cut_tac n = "n-m" and f = "%i. f (i+m) " and k = k 
paulson@14259
   214
       in int_val_lemma)
paulson@14259
   215
apply simp
paulson@14259
   216
apply (erule impE)
paulson@14259
   217
 apply (intro strip)
paulson@14259
   218
 apply (erule_tac x = "i+m" in allE, arith)
paulson@14259
   219
apply (erule exE)
paulson@14259
   220
apply (rule_tac x = "i+m" in exI, arith)
paulson@14259
   221
done
paulson@14259
   222
paulson@14259
   223
paulson@14259
   224
subsection{*Products and 1, by T. M. Rasmussen*}
paulson@14259
   225
paulson@14259
   226
lemma zmult_eq_self_iff: "(m = m*(n::int)) = (n = 1 | m = 0)"
paulson@14259
   227
apply auto
paulson@14259
   228
apply (subgoal_tac "m*1 = m*n")
paulson@14259
   229
apply (drule zmult_cancel1 [THEN iffD1], auto)
nipkow@13685
   230
done
nipkow@13685
   231
paulson@14259
   232
lemma zless_1_zmult: "[| 1 < m; 1 < n |] ==> 1 < m*(n::int)"
paulson@14259
   233
apply (rule_tac y = "1*n" in order_less_trans)
paulson@14259
   234
apply (rule_tac [2] zmult_zless_mono1)
paulson@14259
   235
apply (simp_all (no_asm_simp))
paulson@14259
   236
done
paulson@14259
   237
paulson@14259
   238
lemma pos_zmult_eq_1_iff: "0 < (m::int) ==> (m * n = 1) = (m = 1 & n = 1)"
paulson@14259
   239
apply auto
paulson@14259
   240
apply (case_tac "m=1")
paulson@14259
   241
apply (case_tac [2] "n=1")
paulson@14259
   242
apply (case_tac [4] "m=1")
paulson@14259
   243
apply (case_tac [5] "n=1", auto)
paulson@14259
   244
apply (tactic"distinct_subgoals_tac")
paulson@14259
   245
apply (subgoal_tac "1<m*n", simp)
paulson@14259
   246
apply (rule zless_1_zmult, arith)
paulson@14259
   247
apply (subgoal_tac "0<n", arith)
paulson@14259
   248
apply (subgoal_tac "0<m*n")
paulson@14353
   249
apply (drule zero_less_mult_iff [THEN iffD1], auto)
paulson@14259
   250
done
paulson@14259
   251
paulson@14259
   252
lemma zmult_eq_1_iff: "(m*n = (1::int)) = ((m = 1 & n = 1) | (m = -1 & n = -1))"
paulson@14259
   253
apply (case_tac "0<m")
paulson@14271
   254
apply (simp add: pos_zmult_eq_1_iff)
paulson@14259
   255
apply (case_tac "m=0")
paulson@14271
   256
apply (simp del: number_of_reorient)
paulson@14259
   257
apply (subgoal_tac "0 < -m")
paulson@14259
   258
apply (drule_tac n = "-n" in pos_zmult_eq_1_iff, auto)
paulson@14259
   259
done
paulson@14259
   260
paulson@14259
   261
paulson@14259
   262
subsection{*More about nat*}
paulson@14259
   263
paulson@14271
   264
(*Analogous to zadd_int*)
paulson@14271
   265
lemma zdiff_int: "n \<le> m ==> int m - int n = int (m-n)"
paulson@14271
   266
by (induct m n rule: diff_induct, simp_all)
paulson@14271
   267
paulson@14259
   268
lemma nat_add_distrib:
paulson@14259
   269
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
paulson@14259
   270
apply (rule inj_int [THEN injD])
paulson@14271
   271
apply (simp add: zadd_int [symmetric])
paulson@14259
   272
done
paulson@14259
   273
paulson@14259
   274
lemma nat_diff_distrib:
paulson@14259
   275
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
paulson@14259
   276
apply (rule inj_int [THEN injD])
paulson@14271
   277
apply (simp add: zdiff_int [symmetric] nat_le_eq_zle)
paulson@14259
   278
done
paulson@14259
   279
paulson@14259
   280
lemma nat_mult_distrib: "(0::int) \<le> z ==> nat (z*z') = nat z * nat z'"
paulson@14259
   281
apply (case_tac "0 \<le> z'")
paulson@14259
   282
apply (rule inj_int [THEN injD])
paulson@14353
   283
apply (simp add: zmult_int [symmetric] zero_le_mult_iff)
paulson@14353
   284
apply (simp add: mult_le_0_iff)
paulson@14259
   285
done
paulson@14259
   286
paulson@14259
   287
lemma nat_mult_distrib_neg: "z \<le> (0::int) ==> nat(z*z') = nat(-z) * nat(-z')"
paulson@14259
   288
apply (rule trans)
paulson@14259
   289
apply (rule_tac [2] nat_mult_distrib, auto)
paulson@14259
   290
done
paulson@14259
   291
paulson@14259
   292
lemma nat_abs_mult_distrib: "nat (abs (w * z)) = nat (abs w) * nat (abs z)"
paulson@14259
   293
apply (case_tac "z=0 | w=0")
paulson@14259
   294
apply (auto simp add: zabs_def nat_mult_distrib [symmetric] 
paulson@14353
   295
                      nat_mult_distrib_neg [symmetric] mult_less_0_iff)
paulson@14259
   296
done
paulson@14259
   297
paulson@14353
   298
paulson@14259
   299
ML
paulson@14259
   300
{*
paulson@14259
   301
val zle_diff1_eq = thm "zle_diff1_eq";
paulson@14259
   302
val zle_add1_eq_le = thm "zle_add1_eq_le";
paulson@14259
   303
val nonneg_eq_int = thm "nonneg_eq_int";
paulson@14259
   304
val nat_eq_iff = thm "nat_eq_iff";
paulson@14259
   305
val nat_eq_iff2 = thm "nat_eq_iff2";
paulson@14259
   306
val nat_less_iff = thm "nat_less_iff";
paulson@14259
   307
val int_eq_iff = thm "int_eq_iff";
paulson@14259
   308
val nat_0 = thm "nat_0";
paulson@14259
   309
val nat_1 = thm "nat_1";
paulson@14259
   310
val nat_2 = thm "nat_2";
paulson@14259
   311
val nat_less_eq_zless = thm "nat_less_eq_zless";
paulson@14259
   312
val nat_le_eq_zle = thm "nat_le_eq_zle";
paulson@14259
   313
paulson@14259
   314
val nat_intermed_int_val = thm "nat_intermed_int_val";
paulson@14259
   315
val zmult_eq_self_iff = thm "zmult_eq_self_iff";
paulson@14259
   316
val zless_1_zmult = thm "zless_1_zmult";
paulson@14259
   317
val pos_zmult_eq_1_iff = thm "pos_zmult_eq_1_iff";
paulson@14259
   318
val zmult_eq_1_iff = thm "zmult_eq_1_iff";
paulson@14259
   319
val nat_add_distrib = thm "nat_add_distrib";
paulson@14259
   320
val nat_diff_distrib = thm "nat_diff_distrib";
paulson@14259
   321
val nat_mult_distrib = thm "nat_mult_distrib";
paulson@14259
   322
val nat_mult_distrib_neg = thm "nat_mult_distrib_neg";
paulson@14259
   323
val nat_abs_mult_distrib = thm "nat_abs_mult_distrib";
paulson@14259
   324
*}
paulson@14259
   325
wenzelm@7707
   326
end