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