src/HOL/Int.thy
author wenzelm
Thu May 24 17:25:53 2012 +0200 (2012-05-24)
changeset 47988 e4b69e10b990
parent 47255 30a1692557b0
child 48044 fea6f3060b65
permissions -rw-r--r--
tuned proofs;
wenzelm@41959
     1
(*  Title:      HOL/Int.thy
haftmann@25919
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@41959
     3
    Author:     Tobias Nipkow, Florian Haftmann, TU Muenchen
haftmann@25919
     4
*)
haftmann@25919
     5
haftmann@25919
     6
header {* The Integers as Equivalence Classes over Pairs of Natural Numbers *} 
haftmann@25919
     7
haftmann@25919
     8
theory Int
huffman@47108
     9
imports Equiv_Relations Wellfounded
haftmann@25919
    10
uses
haftmann@31068
    11
  ("Tools/int_arith.ML")
haftmann@25919
    12
begin
haftmann@25919
    13
haftmann@25919
    14
subsection {* The equivalence relation underlying the integers *}
haftmann@25919
    15
haftmann@28661
    16
definition intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set" where
haftmann@37767
    17
  "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
haftmann@25919
    18
wenzelm@45694
    19
definition "Integ = UNIV//intrel"
wenzelm@45694
    20
wenzelm@45694
    21
typedef (open) int = Integ
wenzelm@45694
    22
  morphisms Rep_Integ Abs_Integ
wenzelm@45694
    23
  unfolding Integ_def by (auto simp add: quotient_def)
haftmann@25919
    24
haftmann@25919
    25
instantiation int :: "{zero, one, plus, minus, uminus, times, ord, abs, sgn}"
haftmann@25919
    26
begin
haftmann@25919
    27
haftmann@25919
    28
definition
haftmann@37767
    29
  Zero_int_def: "0 = Abs_Integ (intrel `` {(0, 0)})"
haftmann@25919
    30
haftmann@25919
    31
definition
haftmann@37767
    32
  One_int_def: "1 = Abs_Integ (intrel `` {(1, 0)})"
haftmann@25919
    33
haftmann@25919
    34
definition
haftmann@37767
    35
  add_int_def: "z + w = Abs_Integ
haftmann@25919
    36
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u, v) \<in> Rep_Integ w.
haftmann@25919
    37
      intrel `` {(x + u, y + v)})"
haftmann@25919
    38
haftmann@25919
    39
definition
haftmann@37767
    40
  minus_int_def:
haftmann@25919
    41
    "- z = Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
haftmann@25919
    42
haftmann@25919
    43
definition
haftmann@37767
    44
  diff_int_def:  "z - w = z + (-w \<Colon> int)"
haftmann@25919
    45
haftmann@25919
    46
definition
haftmann@37767
    47
  mult_int_def: "z * w = Abs_Integ
haftmann@25919
    48
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
haftmann@25919
    49
      intrel `` {(x*u + y*v, x*v + y*u)})"
haftmann@25919
    50
haftmann@25919
    51
definition
haftmann@37767
    52
  le_int_def:
haftmann@25919
    53
   "z \<le> w \<longleftrightarrow> (\<exists>x y u v. x+v \<le> u+y \<and> (x, y) \<in> Rep_Integ z \<and> (u, v) \<in> Rep_Integ w)"
haftmann@25919
    54
haftmann@25919
    55
definition
haftmann@37767
    56
  less_int_def: "(z\<Colon>int) < w \<longleftrightarrow> z \<le> w \<and> z \<noteq> w"
haftmann@25919
    57
haftmann@25919
    58
definition
haftmann@25919
    59
  zabs_def: "\<bar>i\<Colon>int\<bar> = (if i < 0 then - i else i)"
haftmann@25919
    60
haftmann@25919
    61
definition
haftmann@25919
    62
  zsgn_def: "sgn (i\<Colon>int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
haftmann@25919
    63
haftmann@25919
    64
instance ..
haftmann@25919
    65
haftmann@25919
    66
end
haftmann@25919
    67
haftmann@25919
    68
haftmann@25919
    69
subsection{*Construction of the Integers*}
haftmann@25919
    70
haftmann@25919
    71
lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
haftmann@25919
    72
by (simp add: intrel_def)
haftmann@25919
    73
haftmann@25919
    74
lemma equiv_intrel: "equiv UNIV intrel"
nipkow@30198
    75
by (simp add: intrel_def equiv_def refl_on_def sym_def trans_def)
haftmann@25919
    76
haftmann@25919
    77
text{*Reduces equality of equivalence classes to the @{term intrel} relation:
haftmann@25919
    78
  @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
haftmann@25919
    79
lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
haftmann@25919
    80
haftmann@25919
    81
text{*All equivalence classes belong to set of representatives*}
haftmann@25919
    82
lemma [simp]: "intrel``{(x,y)} \<in> Integ"
haftmann@25919
    83
by (auto simp add: Integ_def intrel_def quotient_def)
haftmann@25919
    84
haftmann@25919
    85
text{*Reduces equality on abstractions to equality on representatives:
haftmann@25919
    86
  @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
blanchet@35828
    87
declare Abs_Integ_inject [simp,no_atp]  Abs_Integ_inverse [simp,no_atp]
haftmann@25919
    88
haftmann@25919
    89
text{*Case analysis on the representation of an integer as an equivalence
haftmann@25919
    90
      class of pairs of naturals.*}
haftmann@25919
    91
lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
haftmann@25919
    92
     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
haftmann@25919
    93
apply (rule Abs_Integ_cases [of z]) 
haftmann@25919
    94
apply (auto simp add: Integ_def quotient_def) 
haftmann@25919
    95
done
haftmann@25919
    96
haftmann@25919
    97
haftmann@25919
    98
subsection {* Arithmetic Operations *}
haftmann@25919
    99
haftmann@25919
   100
lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
haftmann@25919
   101
proof -
haftmann@25919
   102
  have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
haftmann@40819
   103
    by (auto simp add: congruent_def)
haftmann@25919
   104
  thus ?thesis
haftmann@25919
   105
    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
haftmann@25919
   106
qed
haftmann@25919
   107
haftmann@25919
   108
lemma add:
haftmann@25919
   109
     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
haftmann@25919
   110
      Abs_Integ (intrel``{(x+u, y+v)})"
haftmann@25919
   111
proof -
haftmann@25919
   112
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z) 
haftmann@25919
   113
        respects2 intrel"
haftmann@40819
   114
    by (auto simp add: congruent2_def)
haftmann@25919
   115
  thus ?thesis
haftmann@25919
   116
    by (simp add: add_int_def UN_UN_split_split_eq
haftmann@25919
   117
                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
haftmann@25919
   118
qed
haftmann@25919
   119
haftmann@25919
   120
text{*Congruence property for multiplication*}
haftmann@25919
   121
lemma mult_congruent2:
haftmann@25919
   122
     "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
haftmann@25919
   123
      respects2 intrel"
haftmann@25919
   124
apply (rule equiv_intrel [THEN congruent2_commuteI])
haftmann@25919
   125
 apply (force simp add: mult_ac, clarify) 
haftmann@25919
   126
apply (simp add: congruent_def mult_ac)  
haftmann@25919
   127
apply (rename_tac u v w x y z)
haftmann@25919
   128
apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
haftmann@25919
   129
apply (simp add: mult_ac)
haftmann@25919
   130
apply (simp add: add_mult_distrib [symmetric])
haftmann@25919
   131
done
haftmann@25919
   132
haftmann@25919
   133
lemma mult:
haftmann@25919
   134
     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
haftmann@25919
   135
      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
haftmann@25919
   136
by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
haftmann@25919
   137
              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
haftmann@25919
   138
haftmann@25919
   139
text{*The integers form a @{text comm_ring_1}*}
haftmann@25919
   140
instance int :: comm_ring_1
haftmann@25919
   141
proof
haftmann@25919
   142
  fix i j k :: int
haftmann@25919
   143
  show "(i + j) + k = i + (j + k)"
haftmann@25919
   144
    by (cases i, cases j, cases k) (simp add: add add_assoc)
haftmann@25919
   145
  show "i + j = j + i" 
haftmann@25919
   146
    by (cases i, cases j) (simp add: add_ac add)
haftmann@25919
   147
  show "0 + i = i"
haftmann@25919
   148
    by (cases i) (simp add: Zero_int_def add)
haftmann@25919
   149
  show "- i + i = 0"
haftmann@25919
   150
    by (cases i) (simp add: Zero_int_def minus add)
haftmann@25919
   151
  show "i - j = i + - j"
haftmann@25919
   152
    by (simp add: diff_int_def)
haftmann@25919
   153
  show "(i * j) * k = i * (j * k)"
nipkow@29667
   154
    by (cases i, cases j, cases k) (simp add: mult algebra_simps)
haftmann@25919
   155
  show "i * j = j * i"
nipkow@29667
   156
    by (cases i, cases j) (simp add: mult algebra_simps)
haftmann@25919
   157
  show "1 * i = i"
haftmann@25919
   158
    by (cases i) (simp add: One_int_def mult)
haftmann@25919
   159
  show "(i + j) * k = i * k + j * k"
nipkow@29667
   160
    by (cases i, cases j, cases k) (simp add: add mult algebra_simps)
haftmann@25919
   161
  show "0 \<noteq> (1::int)"
haftmann@25919
   162
    by (simp add: Zero_int_def One_int_def)
haftmann@25919
   163
qed
haftmann@25919
   164
huffman@44709
   165
abbreviation int :: "nat \<Rightarrow> int" where
huffman@44709
   166
  "int \<equiv> of_nat"
huffman@44709
   167
huffman@44709
   168
lemma int_def: "int m = Abs_Integ (intrel `` {(m, 0)})"
wenzelm@42676
   169
by (induct m) (simp_all add: Zero_int_def One_int_def add)
haftmann@25919
   170
haftmann@25919
   171
haftmann@25919
   172
subsection {* The @{text "\<le>"} Ordering *}
haftmann@25919
   173
haftmann@25919
   174
lemma le:
haftmann@25919
   175
  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
haftmann@25919
   176
by (force simp add: le_int_def)
haftmann@25919
   177
haftmann@25919
   178
lemma less:
haftmann@25919
   179
  "(Abs_Integ(intrel``{(x,y)}) < Abs_Integ(intrel``{(u,v)})) = (x+v < u+y)"
haftmann@25919
   180
by (simp add: less_int_def le order_less_le)
haftmann@25919
   181
haftmann@25919
   182
instance int :: linorder
haftmann@25919
   183
proof
haftmann@25919
   184
  fix i j k :: int
haftmann@27682
   185
  show antisym: "i \<le> j \<Longrightarrow> j \<le> i \<Longrightarrow> i = j"
haftmann@27682
   186
    by (cases i, cases j) (simp add: le)
haftmann@27682
   187
  show "(i < j) = (i \<le> j \<and> \<not> j \<le> i)"
haftmann@27682
   188
    by (auto simp add: less_int_def dest: antisym) 
haftmann@25919
   189
  show "i \<le> i"
haftmann@25919
   190
    by (cases i) (simp add: le)
haftmann@25919
   191
  show "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k"
haftmann@25919
   192
    by (cases i, cases j, cases k) (simp add: le)
haftmann@25919
   193
  show "i \<le> j \<or> j \<le> i"
haftmann@25919
   194
    by (cases i, cases j) (simp add: le linorder_linear)
haftmann@25919
   195
qed
haftmann@25919
   196
haftmann@25919
   197
instantiation int :: distrib_lattice
haftmann@25919
   198
begin
haftmann@25919
   199
haftmann@25919
   200
definition
haftmann@25919
   201
  "(inf \<Colon> int \<Rightarrow> int \<Rightarrow> int) = min"
haftmann@25919
   202
haftmann@25919
   203
definition
haftmann@25919
   204
  "(sup \<Colon> int \<Rightarrow> int \<Rightarrow> int) = max"
haftmann@25919
   205
haftmann@25919
   206
instance
haftmann@25919
   207
  by intro_classes
haftmann@25919
   208
    (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
haftmann@25919
   209
haftmann@25919
   210
end
haftmann@25919
   211
haftmann@35028
   212
instance int :: ordered_cancel_ab_semigroup_add
haftmann@25919
   213
proof
haftmann@25919
   214
  fix i j k :: int
haftmann@25919
   215
  show "i \<le> j \<Longrightarrow> k + i \<le> k + j"
haftmann@25919
   216
    by (cases i, cases j, cases k) (simp add: le add)
haftmann@25919
   217
qed
haftmann@25919
   218
haftmann@25961
   219
haftmann@25919
   220
text{*Strict Monotonicity of Multiplication*}
haftmann@25919
   221
haftmann@25919
   222
text{*strict, in 1st argument; proof is by induction on k>0*}
haftmann@25919
   223
lemma zmult_zless_mono2_lemma:
huffman@44709
   224
     "(i::int)<j ==> 0<k ==> int k * i < int k * j"
wenzelm@42676
   225
apply (induct k)
wenzelm@42676
   226
apply simp
haftmann@25919
   227
apply (simp add: left_distrib)
haftmann@25919
   228
apply (case_tac "k=0")
haftmann@25919
   229
apply (simp_all add: add_strict_mono)
haftmann@25919
   230
done
haftmann@25919
   231
huffman@44709
   232
lemma zero_le_imp_eq_int: "(0::int) \<le> k ==> \<exists>n. k = int n"
haftmann@25919
   233
apply (cases k)
haftmann@25919
   234
apply (auto simp add: le add int_def Zero_int_def)
haftmann@25919
   235
apply (rule_tac x="x-y" in exI, simp)
haftmann@25919
   236
done
haftmann@25919
   237
huffman@44709
   238
lemma zero_less_imp_eq_int: "(0::int) < k ==> \<exists>n>0. k = int n"
haftmann@25919
   239
apply (cases k)
haftmann@25919
   240
apply (simp add: less int_def Zero_int_def)
haftmann@25919
   241
apply (rule_tac x="x-y" in exI, simp)
haftmann@25919
   242
done
haftmann@25919
   243
haftmann@25919
   244
lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
haftmann@25919
   245
apply (drule zero_less_imp_eq_int)
haftmann@25919
   246
apply (auto simp add: zmult_zless_mono2_lemma)
haftmann@25919
   247
done
haftmann@25919
   248
haftmann@25919
   249
text{*The integers form an ordered integral domain*}
haftmann@35028
   250
instance int :: linordered_idom
haftmann@25919
   251
proof
haftmann@25919
   252
  fix i j k :: int
haftmann@25919
   253
  show "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
haftmann@25919
   254
    by (rule zmult_zless_mono2)
haftmann@25919
   255
  show "\<bar>i\<bar> = (if i < 0 then -i else i)"
haftmann@25919
   256
    by (simp only: zabs_def)
haftmann@25919
   257
  show "sgn (i\<Colon>int) = (if i=0 then 0 else if 0<i then 1 else - 1)"
haftmann@25919
   258
    by (simp only: zsgn_def)
haftmann@25919
   259
qed
haftmann@25919
   260
haftmann@25919
   261
lemma zless_imp_add1_zle: "w < z \<Longrightarrow> w + (1\<Colon>int) \<le> z"
haftmann@25919
   262
apply (cases w, cases z) 
haftmann@25919
   263
apply (simp add: less le add One_int_def)
haftmann@25919
   264
done
haftmann@25919
   265
haftmann@25919
   266
lemma zless_iff_Suc_zadd:
huffman@44709
   267
  "(w \<Colon> int) < z \<longleftrightarrow> (\<exists>n. z = w + int (Suc n))"
haftmann@25919
   268
apply (cases z, cases w)
haftmann@25919
   269
apply (auto simp add: less add int_def)
haftmann@25919
   270
apply (rename_tac a b c d) 
haftmann@25919
   271
apply (rule_tac x="a+d - Suc(c+b)" in exI) 
haftmann@25919
   272
apply arith
haftmann@25919
   273
done
haftmann@25919
   274
haftmann@25919
   275
lemmas int_distrib =
wenzelm@45607
   276
  left_distrib [of z1 z2 w]
wenzelm@45607
   277
  right_distrib [of w z1 z2]
wenzelm@45607
   278
  left_diff_distrib [of z1 z2 w]
wenzelm@45607
   279
  right_diff_distrib [of w z1 z2]
wenzelm@45607
   280
  for z1 z2 w :: int
haftmann@25919
   281
haftmann@25919
   282
haftmann@25919
   283
subsection {* Embedding of the Integers into any @{text ring_1}: @{text of_int}*}
haftmann@25919
   284
haftmann@25919
   285
context ring_1
haftmann@25919
   286
begin
haftmann@25919
   287
haftmann@31015
   288
definition of_int :: "int \<Rightarrow> 'a" where
haftmann@39910
   289
  "of_int z = the_elem (\<Union>(i, j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
haftmann@25919
   290
haftmann@25919
   291
lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
haftmann@25919
   292
proof -
haftmann@25919
   293
  have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
haftmann@40819
   294
    by (auto simp add: congruent_def) (simp add: algebra_simps of_nat_add [symmetric]
haftmann@25919
   295
            del: of_nat_add) 
haftmann@25919
   296
  thus ?thesis
haftmann@25919
   297
    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
haftmann@25919
   298
qed
haftmann@25919
   299
haftmann@25919
   300
lemma of_int_0 [simp]: "of_int 0 = 0"
nipkow@29667
   301
by (simp add: of_int Zero_int_def)
haftmann@25919
   302
haftmann@25919
   303
lemma of_int_1 [simp]: "of_int 1 = 1"
nipkow@29667
   304
by (simp add: of_int One_int_def)
haftmann@25919
   305
haftmann@25919
   306
lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
wenzelm@42676
   307
by (cases w, cases z) (simp add: algebra_simps of_int add)
haftmann@25919
   308
haftmann@25919
   309
lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
wenzelm@42676
   310
by (cases z) (simp add: algebra_simps of_int minus)
haftmann@25919
   311
haftmann@25919
   312
lemma of_int_diff [simp]: "of_int (w - z) = of_int w - of_int z"
haftmann@35050
   313
by (simp add: diff_minus Groups.diff_minus)
haftmann@25919
   314
haftmann@25919
   315
lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
haftmann@25919
   316
apply (cases w, cases z)
nipkow@29667
   317
apply (simp add: algebra_simps of_int mult of_nat_mult)
haftmann@25919
   318
done
haftmann@25919
   319
haftmann@25919
   320
text{*Collapse nested embeddings*}
huffman@44709
   321
lemma of_int_of_nat_eq [simp]: "of_int (int n) = of_nat n"
nipkow@29667
   322
by (induct n) auto
haftmann@25919
   323
huffman@47108
   324
lemma of_int_numeral [simp, code_post]: "of_int (numeral k) = numeral k"
huffman@47108
   325
  by (simp add: of_nat_numeral [symmetric] of_int_of_nat_eq [symmetric])
huffman@47108
   326
huffman@47108
   327
lemma of_int_neg_numeral [simp, code_post]: "of_int (neg_numeral k) = neg_numeral k"
huffman@47108
   328
  unfolding neg_numeral_def neg_numeral_class.neg_numeral_def
huffman@47108
   329
  by (simp only: of_int_minus of_int_numeral)
huffman@47108
   330
haftmann@31015
   331
lemma of_int_power:
haftmann@31015
   332
  "of_int (z ^ n) = of_int z ^ n"
haftmann@31015
   333
  by (induct n) simp_all
haftmann@31015
   334
haftmann@25919
   335
end
haftmann@25919
   336
huffman@47108
   337
context ring_char_0
haftmann@25919
   338
begin
haftmann@25919
   339
haftmann@25919
   340
lemma of_int_eq_iff [simp]:
haftmann@25919
   341
   "of_int w = of_int z \<longleftrightarrow> w = z"
wenzelm@42676
   342
apply (cases w, cases z)
wenzelm@42676
   343
apply (simp add: of_int)
haftmann@25919
   344
apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq)
haftmann@25919
   345
apply (simp only: of_nat_add [symmetric] of_nat_eq_iff)
haftmann@25919
   346
done
haftmann@25919
   347
haftmann@25919
   348
text{*Special cases where either operand is zero*}
haftmann@36424
   349
lemma of_int_eq_0_iff [simp]:
haftmann@36424
   350
  "of_int z = 0 \<longleftrightarrow> z = 0"
haftmann@36424
   351
  using of_int_eq_iff [of z 0] by simp
haftmann@36424
   352
haftmann@36424
   353
lemma of_int_0_eq_iff [simp]:
haftmann@36424
   354
  "0 = of_int z \<longleftrightarrow> z = 0"
haftmann@36424
   355
  using of_int_eq_iff [of 0 z] by simp
haftmann@25919
   356
haftmann@25919
   357
end
haftmann@25919
   358
haftmann@36424
   359
context linordered_idom
haftmann@36424
   360
begin
haftmann@36424
   361
haftmann@35028
   362
text{*Every @{text linordered_idom} has characteristic zero.*}
haftmann@36424
   363
subclass ring_char_0 ..
haftmann@36424
   364
haftmann@36424
   365
lemma of_int_le_iff [simp]:
haftmann@36424
   366
  "of_int w \<le> of_int z \<longleftrightarrow> w \<le> z"
wenzelm@42676
   367
  by (cases w, cases z)
wenzelm@42676
   368
    (simp add: of_int le minus algebra_simps of_nat_add [symmetric] del: of_nat_add)
haftmann@36424
   369
haftmann@36424
   370
lemma of_int_less_iff [simp]:
haftmann@36424
   371
  "of_int w < of_int z \<longleftrightarrow> w < z"
haftmann@36424
   372
  by (simp add: less_le order_less_le)
haftmann@36424
   373
haftmann@36424
   374
lemma of_int_0_le_iff [simp]:
haftmann@36424
   375
  "0 \<le> of_int z \<longleftrightarrow> 0 \<le> z"
haftmann@36424
   376
  using of_int_le_iff [of 0 z] by simp
haftmann@36424
   377
haftmann@36424
   378
lemma of_int_le_0_iff [simp]:
haftmann@36424
   379
  "of_int z \<le> 0 \<longleftrightarrow> z \<le> 0"
haftmann@36424
   380
  using of_int_le_iff [of z 0] by simp
haftmann@36424
   381
haftmann@36424
   382
lemma of_int_0_less_iff [simp]:
haftmann@36424
   383
  "0 < of_int z \<longleftrightarrow> 0 < z"
haftmann@36424
   384
  using of_int_less_iff [of 0 z] by simp
haftmann@36424
   385
haftmann@36424
   386
lemma of_int_less_0_iff [simp]:
haftmann@36424
   387
  "of_int z < 0 \<longleftrightarrow> z < 0"
haftmann@36424
   388
  using of_int_less_iff [of z 0] by simp
haftmann@36424
   389
haftmann@36424
   390
end
haftmann@25919
   391
haftmann@25919
   392
lemma of_int_eq_id [simp]: "of_int = id"
haftmann@25919
   393
proof
haftmann@25919
   394
  fix z show "of_int z = id z"
haftmann@25919
   395
    by (cases z) (simp add: of_int add minus int_def diff_minus)
haftmann@25919
   396
qed
haftmann@25919
   397
haftmann@25919
   398
haftmann@25919
   399
subsection {* Magnitude of an Integer, as a Natural Number: @{text nat} *}
haftmann@25919
   400
haftmann@37767
   401
definition nat :: "int \<Rightarrow> nat" where
haftmann@39910
   402
  "nat z = the_elem (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
haftmann@25919
   403
haftmann@25919
   404
lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
haftmann@25919
   405
proof -
haftmann@25919
   406
  have "(\<lambda>(x,y). {x-y}) respects intrel"
haftmann@40819
   407
    by (auto simp add: congruent_def)
haftmann@25919
   408
  thus ?thesis
haftmann@25919
   409
    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
haftmann@25919
   410
qed
haftmann@25919
   411
huffman@44709
   412
lemma nat_int [simp]: "nat (int n) = n"
haftmann@25919
   413
by (simp add: nat int_def)
haftmann@25919
   414
huffman@44709
   415
lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
wenzelm@42676
   416
by (cases z) (simp add: nat le int_def Zero_int_def)
haftmann@25919
   417
huffman@44709
   418
corollary nat_0_le: "0 \<le> z ==> int (nat z) = z"
haftmann@25919
   419
by simp
haftmann@25919
   420
haftmann@25919
   421
lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
wenzelm@42676
   422
by (cases z) (simp add: nat le Zero_int_def)
haftmann@25919
   423
haftmann@25919
   424
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
haftmann@25919
   425
apply (cases w, cases z) 
haftmann@25919
   426
apply (simp add: nat le linorder_not_le [symmetric] Zero_int_def, arith)
haftmann@25919
   427
done
haftmann@25919
   428
haftmann@25919
   429
text{*An alternative condition is @{term "0 \<le> w"} *}
haftmann@25919
   430
corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
haftmann@25919
   431
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
haftmann@25919
   432
haftmann@25919
   433
corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
haftmann@25919
   434
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
haftmann@25919
   435
haftmann@25919
   436
lemma zless_nat_conj [simp]: "(nat w < nat z) = (0 < z & w < z)"
haftmann@25919
   437
apply (cases w, cases z) 
haftmann@25919
   438
apply (simp add: nat le Zero_int_def linorder_not_le [symmetric], arith)
haftmann@25919
   439
done
haftmann@25919
   440
haftmann@25919
   441
lemma nonneg_eq_int:
haftmann@25919
   442
  fixes z :: int
huffman@44709
   443
  assumes "0 \<le> z" and "\<And>m. z = int m \<Longrightarrow> P"
haftmann@25919
   444
  shows P
haftmann@25919
   445
  using assms by (blast dest: nat_0_le sym)
haftmann@25919
   446
huffman@44709
   447
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
wenzelm@42676
   448
by (cases w) (simp add: nat le int_def Zero_int_def, arith)
haftmann@25919
   449
huffman@44709
   450
corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
haftmann@25919
   451
by (simp only: eq_commute [of m] nat_eq_iff)
haftmann@25919
   452
haftmann@25919
   453
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < of_nat m)"
haftmann@25919
   454
apply (cases w)
nipkow@29700
   455
apply (simp add: nat le int_def Zero_int_def linorder_not_le[symmetric], arith)
haftmann@25919
   456
done
haftmann@25919
   457
huffman@44709
   458
lemma nat_le_iff: "nat x \<le> n \<longleftrightarrow> x \<le> int n"
huffman@44707
   459
  by (cases x, simp add: nat le int_def le_diff_conv)
huffman@44707
   460
huffman@44707
   461
lemma nat_mono: "x \<le> y \<Longrightarrow> nat x \<le> nat y"
huffman@44707
   462
  by (cases x, cases y, simp add: nat le)
huffman@44707
   463
nipkow@29700
   464
lemma nat_0_iff[simp]: "nat(i::int) = 0 \<longleftrightarrow> i\<le>0"
nipkow@29700
   465
by(simp add: nat_eq_iff) arith
nipkow@29700
   466
haftmann@25919
   467
lemma int_eq_iff: "(of_nat m = z) = (m = nat z & 0 \<le> z)"
haftmann@25919
   468
by (auto simp add: nat_eq_iff2)
haftmann@25919
   469
haftmann@25919
   470
lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
haftmann@25919
   471
by (insert zless_nat_conj [of 0], auto)
haftmann@25919
   472
haftmann@25919
   473
lemma nat_add_distrib:
haftmann@25919
   474
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
wenzelm@42676
   475
by (cases z, cases z') (simp add: nat add le Zero_int_def)
haftmann@25919
   476
haftmann@25919
   477
lemma nat_diff_distrib:
haftmann@25919
   478
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
wenzelm@42676
   479
by (cases z, cases z')
wenzelm@42676
   480
  (simp add: nat add minus diff_minus le Zero_int_def)
haftmann@25919
   481
huffman@44709
   482
lemma nat_zminus_int [simp]: "nat (- int n) = 0"
haftmann@25919
   483
by (simp add: int_def minus nat Zero_int_def) 
haftmann@25919
   484
huffman@44709
   485
lemma zless_nat_eq_int_zless: "(m < nat z) = (int m < z)"
wenzelm@42676
   486
by (cases z) (simp add: nat less int_def, arith)
haftmann@25919
   487
haftmann@25919
   488
context ring_1
haftmann@25919
   489
begin
haftmann@25919
   490
haftmann@25919
   491
lemma of_nat_nat: "0 \<le> z \<Longrightarrow> of_nat (nat z) = of_int z"
haftmann@25919
   492
  by (cases z rule: eq_Abs_Integ)
haftmann@25919
   493
   (simp add: nat le of_int Zero_int_def of_nat_diff)
haftmann@25919
   494
haftmann@25919
   495
end
haftmann@25919
   496
krauss@29779
   497
text {* For termination proofs: *}
krauss@29779
   498
lemma measure_function_int[measure_function]: "is_measure (nat o abs)" ..
krauss@29779
   499
haftmann@25919
   500
haftmann@25919
   501
subsection{*Lemmas about the Function @{term of_nat} and Orderings*}
haftmann@25919
   502
huffman@44709
   503
lemma negative_zless_0: "- (int (Suc n)) < (0 \<Colon> int)"
haftmann@25919
   504
by (simp add: order_less_le del: of_nat_Suc)
haftmann@25919
   505
huffman@44709
   506
lemma negative_zless [iff]: "- (int (Suc n)) < int m"
haftmann@25919
   507
by (rule negative_zless_0 [THEN order_less_le_trans], simp)
haftmann@25919
   508
huffman@44709
   509
lemma negative_zle_0: "- int n \<le> 0"
haftmann@25919
   510
by (simp add: minus_le_iff)
haftmann@25919
   511
huffman@44709
   512
lemma negative_zle [iff]: "- int n \<le> int m"
haftmann@25919
   513
by (rule order_trans [OF negative_zle_0 of_nat_0_le_iff])
haftmann@25919
   514
huffman@44709
   515
lemma not_zle_0_negative [simp]: "~ (0 \<le> - (int (Suc n)))"
haftmann@25919
   516
by (subst le_minus_iff, simp del: of_nat_Suc)
haftmann@25919
   517
huffman@44709
   518
lemma int_zle_neg: "(int n \<le> - int m) = (n = 0 & m = 0)"
haftmann@25919
   519
by (simp add: int_def le minus Zero_int_def)
haftmann@25919
   520
huffman@44709
   521
lemma not_int_zless_negative [simp]: "~ (int n < - int m)"
haftmann@25919
   522
by (simp add: linorder_not_less)
haftmann@25919
   523
huffman@44709
   524
lemma negative_eq_positive [simp]: "(- int n = of_nat m) = (n = 0 & m = 0)"
haftmann@25919
   525
by (force simp add: order_eq_iff [of "- of_nat n"] int_zle_neg)
haftmann@25919
   526
huffman@44709
   527
lemma zle_iff_zadd: "w \<le> z \<longleftrightarrow> (\<exists>n. z = w + int n)"
haftmann@25919
   528
proof -
haftmann@25919
   529
  have "(w \<le> z) = (0 \<le> z - w)"
haftmann@25919
   530
    by (simp only: le_diff_eq add_0_left)
haftmann@25919
   531
  also have "\<dots> = (\<exists>n. z - w = of_nat n)"
haftmann@25919
   532
    by (auto elim: zero_le_imp_eq_int)
haftmann@25919
   533
  also have "\<dots> = (\<exists>n. z = w + of_nat n)"
nipkow@29667
   534
    by (simp only: algebra_simps)
haftmann@25919
   535
  finally show ?thesis .
haftmann@25919
   536
qed
haftmann@25919
   537
huffman@44709
   538
lemma zadd_int_left: "int m + (int n + z) = int (m + n) + z"
haftmann@25919
   539
by simp
haftmann@25919
   540
huffman@44709
   541
lemma int_Suc0_eq_1: "int (Suc 0) = 1"
haftmann@25919
   542
by simp
haftmann@25919
   543
haftmann@25919
   544
text{*This version is proved for all ordered rings, not just integers!
haftmann@25919
   545
      It is proved here because attribute @{text arith_split} is not available
haftmann@35050
   546
      in theory @{text Rings}.
haftmann@25919
   547
      But is it really better than just rewriting with @{text abs_if}?*}
blanchet@35828
   548
lemma abs_split [arith_split,no_atp]:
haftmann@35028
   549
     "P(abs(a::'a::linordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
haftmann@25919
   550
by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
haftmann@25919
   551
huffman@44709
   552
lemma negD: "x < 0 \<Longrightarrow> \<exists>n. x = - (int (Suc n))"
haftmann@25919
   553
apply (cases x)
haftmann@25919
   554
apply (auto simp add: le minus Zero_int_def int_def order_less_le)
haftmann@25919
   555
apply (rule_tac x="y - Suc x" in exI, arith)
haftmann@25919
   556
done
haftmann@25919
   557
haftmann@25919
   558
haftmann@25919
   559
subsection {* Cases and induction *}
haftmann@25919
   560
haftmann@25919
   561
text{*Now we replace the case analysis rule by a more conventional one:
haftmann@25919
   562
whether an integer is negative or not.*}
haftmann@25919
   563
wenzelm@42676
   564
theorem int_cases [case_names nonneg neg, cases type: int]:
huffman@44709
   565
  "[|!! n. z = int n ==> P;  !! n. z =  - (int (Suc n)) ==> P |] ==> P"
wenzelm@42676
   566
apply (cases "z < 0")
wenzelm@42676
   567
apply (blast dest!: negD)
haftmann@25919
   568
apply (simp add: linorder_not_less del: of_nat_Suc)
haftmann@25919
   569
apply auto
haftmann@25919
   570
apply (blast dest: nat_0_le [THEN sym])
haftmann@25919
   571
done
haftmann@25919
   572
wenzelm@42676
   573
theorem int_of_nat_induct [case_names nonneg neg, induct type: int]:
huffman@44709
   574
     "[|!! n. P (int n);  !!n. P (- (int (Suc n))) |] ==> P z"
wenzelm@42676
   575
  by (cases z) auto
haftmann@25919
   576
huffman@47207
   577
lemma nonneg_int_cases:
huffman@47207
   578
  assumes "0 \<le> k" obtains n where "k = int n"
huffman@47207
   579
  using assms by (cases k, simp, simp del: of_nat_Suc)
huffman@47207
   580
haftmann@25919
   581
text{*Contributed by Brian Huffman*}
haftmann@25919
   582
theorem int_diff_cases:
huffman@44709
   583
  obtains (diff) m n where "z = int m - int n"
haftmann@25919
   584
apply (cases z rule: eq_Abs_Integ)
haftmann@25919
   585
apply (rule_tac m=x and n=y in diff)
haftmann@37887
   586
apply (simp add: int_def minus add diff_minus)
haftmann@25919
   587
done
haftmann@25919
   588
huffman@47108
   589
lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"
huffman@47108
   590
  -- {* Unfold all @{text let}s involving constants *}
huffman@47108
   591
  unfolding Let_def ..
haftmann@37767
   592
huffman@47108
   593
lemma Let_neg_numeral [simp]: "Let (neg_numeral v) f = f (neg_numeral v)"
haftmann@25919
   594
  -- {* Unfold all @{text let}s involving constants *}
haftmann@25919
   595
  unfolding Let_def ..
haftmann@25919
   596
huffman@47108
   597
text {* Unfold @{text min} and @{text max} on numerals. *}
huffman@28958
   598
huffman@47108
   599
lemmas max_number_of [simp] =
huffman@47108
   600
  max_def [of "numeral u" "numeral v"]
huffman@47108
   601
  max_def [of "numeral u" "neg_numeral v"]
huffman@47108
   602
  max_def [of "neg_numeral u" "numeral v"]
huffman@47108
   603
  max_def [of "neg_numeral u" "neg_numeral v"] for u v
huffman@28958
   604
huffman@47108
   605
lemmas min_number_of [simp] =
huffman@47108
   606
  min_def [of "numeral u" "numeral v"]
huffman@47108
   607
  min_def [of "numeral u" "neg_numeral v"]
huffman@47108
   608
  min_def [of "neg_numeral u" "numeral v"]
huffman@47108
   609
  min_def [of "neg_numeral u" "neg_numeral v"] for u v
huffman@26075
   610
haftmann@25919
   611
huffman@28958
   612
subsubsection {* Binary comparisons *}
huffman@28958
   613
huffman@28958
   614
text {* Preliminaries *}
huffman@28958
   615
huffman@28958
   616
lemma even_less_0_iff:
haftmann@35028
   617
  "a + a < 0 \<longleftrightarrow> a < (0::'a::linordered_idom)"
huffman@28958
   618
proof -
huffman@47108
   619
  have "a + a < 0 \<longleftrightarrow> (1+1)*a < 0" by (simp add: left_distrib del: one_add_one)
huffman@28958
   620
  also have "(1+1)*a < 0 \<longleftrightarrow> a < 0"
huffman@28958
   621
    by (simp add: mult_less_0_iff zero_less_two 
huffman@28958
   622
                  order_less_not_sym [OF zero_less_two])
huffman@28958
   623
  finally show ?thesis .
huffman@28958
   624
qed
huffman@28958
   625
huffman@28958
   626
lemma le_imp_0_less: 
huffman@28958
   627
  assumes le: "0 \<le> z"
huffman@28958
   628
  shows "(0::int) < 1 + z"
huffman@28958
   629
proof -
huffman@28958
   630
  have "0 \<le> z" by fact
huffman@47108
   631
  also have "... < z + 1" by (rule less_add_one)
huffman@28958
   632
  also have "... = 1 + z" by (simp add: add_ac)
huffman@28958
   633
  finally show "0 < 1 + z" .
huffman@28958
   634
qed
huffman@28958
   635
huffman@28958
   636
lemma odd_less_0_iff:
huffman@28958
   637
  "(1 + z + z < 0) = (z < (0::int))"
wenzelm@42676
   638
proof (cases z)
huffman@28958
   639
  case (nonneg n)
huffman@28958
   640
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
huffman@28958
   641
                             le_imp_0_less [THEN order_less_imp_le])  
huffman@28958
   642
next
huffman@28958
   643
  case (neg n)
huffman@30079
   644
  thus ?thesis by (simp del: of_nat_Suc of_nat_add of_nat_1
huffman@30079
   645
    add: algebra_simps of_nat_1 [where 'a=int, symmetric] of_nat_add [symmetric])
huffman@28958
   646
qed
huffman@28958
   647
huffman@28958
   648
subsubsection {* Comparisons, for Ordered Rings *}
haftmann@25919
   649
haftmann@25919
   650
lemmas double_eq_0_iff = double_zero
haftmann@25919
   651
haftmann@25919
   652
lemma odd_nonzero:
haftmann@33296
   653
  "1 + z + z \<noteq> (0::int)"
wenzelm@42676
   654
proof (cases z)
haftmann@25919
   655
  case (nonneg n)
haftmann@25919
   656
  have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
haftmann@25919
   657
  thus ?thesis using  le_imp_0_less [OF le]
haftmann@25919
   658
    by (auto simp add: add_assoc) 
haftmann@25919
   659
next
haftmann@25919
   660
  case (neg n)
haftmann@25919
   661
  show ?thesis
haftmann@25919
   662
  proof
haftmann@25919
   663
    assume eq: "1 + z + z = 0"
huffman@44709
   664
    have "(0::int) < 1 + (int n + int n)"
haftmann@25919
   665
      by (simp add: le_imp_0_less add_increasing) 
haftmann@25919
   666
    also have "... = - (1 + z + z)" 
haftmann@25919
   667
      by (simp add: neg add_assoc [symmetric]) 
haftmann@25919
   668
    also have "... = 0" by (simp add: eq) 
haftmann@25919
   669
    finally have "0<0" ..
haftmann@25919
   670
    thus False by blast
haftmann@25919
   671
  qed
haftmann@25919
   672
qed
haftmann@25919
   673
haftmann@30652
   674
haftmann@25919
   675
subsection {* The Set of Integers *}
haftmann@25919
   676
haftmann@25919
   677
context ring_1
haftmann@25919
   678
begin
haftmann@25919
   679
haftmann@30652
   680
definition Ints  :: "'a set" where
haftmann@37767
   681
  "Ints = range of_int"
haftmann@25919
   682
haftmann@25919
   683
notation (xsymbols)
haftmann@25919
   684
  Ints  ("\<int>")
haftmann@25919
   685
huffman@35634
   686
lemma Ints_of_int [simp]: "of_int z \<in> \<int>"
huffman@35634
   687
  by (simp add: Ints_def)
huffman@35634
   688
huffman@35634
   689
lemma Ints_of_nat [simp]: "of_nat n \<in> \<int>"
huffman@45533
   690
  using Ints_of_int [of "of_nat n"] by simp
huffman@35634
   691
haftmann@25919
   692
lemma Ints_0 [simp]: "0 \<in> \<int>"
huffman@45533
   693
  using Ints_of_int [of "0"] by simp
haftmann@25919
   694
haftmann@25919
   695
lemma Ints_1 [simp]: "1 \<in> \<int>"
huffman@45533
   696
  using Ints_of_int [of "1"] by simp
haftmann@25919
   697
haftmann@25919
   698
lemma Ints_add [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a + b \<in> \<int>"
haftmann@25919
   699
apply (auto simp add: Ints_def)
haftmann@25919
   700
apply (rule range_eqI)
haftmann@25919
   701
apply (rule of_int_add [symmetric])
haftmann@25919
   702
done
haftmann@25919
   703
haftmann@25919
   704
lemma Ints_minus [simp]: "a \<in> \<int> \<Longrightarrow> -a \<in> \<int>"
haftmann@25919
   705
apply (auto simp add: Ints_def)
haftmann@25919
   706
apply (rule range_eqI)
haftmann@25919
   707
apply (rule of_int_minus [symmetric])
haftmann@25919
   708
done
haftmann@25919
   709
huffman@35634
   710
lemma Ints_diff [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a - b \<in> \<int>"
huffman@35634
   711
apply (auto simp add: Ints_def)
huffman@35634
   712
apply (rule range_eqI)
huffman@35634
   713
apply (rule of_int_diff [symmetric])
huffman@35634
   714
done
huffman@35634
   715
haftmann@25919
   716
lemma Ints_mult [simp]: "a \<in> \<int> \<Longrightarrow> b \<in> \<int> \<Longrightarrow> a * b \<in> \<int>"
haftmann@25919
   717
apply (auto simp add: Ints_def)
haftmann@25919
   718
apply (rule range_eqI)
haftmann@25919
   719
apply (rule of_int_mult [symmetric])
haftmann@25919
   720
done
haftmann@25919
   721
huffman@35634
   722
lemma Ints_power [simp]: "a \<in> \<int> \<Longrightarrow> a ^ n \<in> \<int>"
huffman@35634
   723
by (induct n) simp_all
huffman@35634
   724
haftmann@25919
   725
lemma Ints_cases [cases set: Ints]:
haftmann@25919
   726
  assumes "q \<in> \<int>"
haftmann@25919
   727
  obtains (of_int) z where "q = of_int z"
haftmann@25919
   728
  unfolding Ints_def
haftmann@25919
   729
proof -
haftmann@25919
   730
  from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
haftmann@25919
   731
  then obtain z where "q = of_int z" ..
haftmann@25919
   732
  then show thesis ..
haftmann@25919
   733
qed
haftmann@25919
   734
haftmann@25919
   735
lemma Ints_induct [case_names of_int, induct set: Ints]:
haftmann@25919
   736
  "q \<in> \<int> \<Longrightarrow> (\<And>z. P (of_int z)) \<Longrightarrow> P q"
haftmann@25919
   737
  by (rule Ints_cases) auto
haftmann@25919
   738
haftmann@25919
   739
end
haftmann@25919
   740
haftmann@25919
   741
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
haftmann@25919
   742
haftmann@25919
   743
lemma Ints_double_eq_0_iff:
haftmann@25919
   744
  assumes in_Ints: "a \<in> Ints"
haftmann@25919
   745
  shows "(a + a = 0) = (a = (0::'a::ring_char_0))"
haftmann@25919
   746
proof -
haftmann@25919
   747
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
haftmann@25919
   748
  then obtain z where a: "a = of_int z" ..
haftmann@25919
   749
  show ?thesis
haftmann@25919
   750
  proof
haftmann@25919
   751
    assume "a = 0"
haftmann@25919
   752
    thus "a + a = 0" by simp
haftmann@25919
   753
  next
haftmann@25919
   754
    assume eq: "a + a = 0"
haftmann@25919
   755
    hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)
haftmann@25919
   756
    hence "z + z = 0" by (simp only: of_int_eq_iff)
haftmann@25919
   757
    hence "z = 0" by (simp only: double_eq_0_iff)
haftmann@25919
   758
    thus "a = 0" by (simp add: a)
haftmann@25919
   759
  qed
haftmann@25919
   760
qed
haftmann@25919
   761
haftmann@25919
   762
lemma Ints_odd_nonzero:
haftmann@25919
   763
  assumes in_Ints: "a \<in> Ints"
haftmann@25919
   764
  shows "1 + a + a \<noteq> (0::'a::ring_char_0)"
haftmann@25919
   765
proof -
haftmann@25919
   766
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
haftmann@25919
   767
  then obtain z where a: "a = of_int z" ..
haftmann@25919
   768
  show ?thesis
haftmann@25919
   769
  proof
haftmann@25919
   770
    assume eq: "1 + a + a = 0"
haftmann@25919
   771
    hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)
haftmann@25919
   772
    hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
haftmann@25919
   773
    with odd_nonzero show False by blast
haftmann@25919
   774
  qed
haftmann@25919
   775
qed 
haftmann@25919
   776
huffman@47108
   777
lemma Nats_numeral [simp]: "numeral w \<in> Nats"
huffman@47108
   778
  using of_nat_in_Nats [of "numeral w"] by simp
huffman@35634
   779
haftmann@25919
   780
lemma Ints_odd_less_0: 
haftmann@25919
   781
  assumes in_Ints: "a \<in> Ints"
haftmann@35028
   782
  shows "(1 + a + a < 0) = (a < (0::'a::linordered_idom))"
haftmann@25919
   783
proof -
haftmann@25919
   784
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
haftmann@25919
   785
  then obtain z where a: "a = of_int z" ..
haftmann@25919
   786
  hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
haftmann@25919
   787
    by (simp add: a)
huffman@45532
   788
  also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0_iff)
haftmann@25919
   789
  also have "... = (a < 0)" by (simp add: a)
haftmann@25919
   790
  finally show ?thesis .
haftmann@25919
   791
qed
haftmann@25919
   792
haftmann@25919
   793
haftmann@25919
   794
subsection {* @{term setsum} and @{term setprod} *}
haftmann@25919
   795
haftmann@25919
   796
lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))"
haftmann@25919
   797
  apply (cases "finite A")
haftmann@25919
   798
  apply (erule finite_induct, auto)
haftmann@25919
   799
  done
haftmann@25919
   800
haftmann@25919
   801
lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))"
haftmann@25919
   802
  apply (cases "finite A")
haftmann@25919
   803
  apply (erule finite_induct, auto)
haftmann@25919
   804
  done
haftmann@25919
   805
haftmann@25919
   806
lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))"
haftmann@25919
   807
  apply (cases "finite A")
haftmann@25919
   808
  apply (erule finite_induct, auto simp add: of_nat_mult)
haftmann@25919
   809
  done
haftmann@25919
   810
haftmann@25919
   811
lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))"
haftmann@25919
   812
  apply (cases "finite A")
haftmann@25919
   813
  apply (erule finite_induct, auto)
haftmann@25919
   814
  done
haftmann@25919
   815
haftmann@25919
   816
lemmas int_setsum = of_nat_setsum [where 'a=int]
haftmann@25919
   817
lemmas int_setprod = of_nat_setprod [where 'a=int]
haftmann@25919
   818
haftmann@25919
   819
haftmann@25919
   820
text {* Legacy theorems *}
haftmann@25919
   821
haftmann@25919
   822
lemmas zle_int = of_nat_le_iff [where 'a=int]
haftmann@25919
   823
lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
huffman@47108
   824
lemmas numeral_1_eq_1 = numeral_One
haftmann@25919
   825
huffman@30802
   826
subsection {* Setting up simplification procedures *}
huffman@30802
   827
huffman@30802
   828
lemmas int_arith_rules =
huffman@47108
   829
  neg_le_iff_le numeral_One
huffman@30802
   830
  minus_zero diff_minus left_minus right_minus
huffman@45219
   831
  mult_zero_left mult_zero_right mult_1_left mult_1_right
huffman@30802
   832
  mult_minus_left mult_minus_right
huffman@30802
   833
  minus_add_distrib minus_minus mult_assoc
huffman@30802
   834
  of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
huffman@30802
   835
  of_int_0 of_int_1 of_int_add of_int_mult
huffman@30802
   836
haftmann@28952
   837
use "Tools/int_arith.ML"
haftmann@30496
   838
declaration {* K Int_Arith.setup *}
haftmann@25919
   839
huffman@47108
   840
simproc_setup fast_arith ("(m::'a::linordered_idom) < n" |
huffman@47108
   841
  "(m::'a::linordered_idom) <= n" |
huffman@47108
   842
  "(m::'a::linordered_idom) = n") =
wenzelm@43595
   843
  {* fn _ => fn ss => fn ct => Lin_Arith.simproc ss (term_of ct) *}
wenzelm@43595
   844
haftmann@25919
   845
haftmann@25919
   846
subsection{*Lemmas About Small Numerals*}
haftmann@25919
   847
haftmann@25919
   848
lemma abs_power_minus_one [simp]:
huffman@47108
   849
  "abs(-1 ^ n) = (1::'a::linordered_idom)"
haftmann@25919
   850
by (simp add: power_abs)
haftmann@25919
   851
haftmann@25919
   852
haftmann@25919
   853
subsection{*More Inequality Reasoning*}
haftmann@25919
   854
haftmann@25919
   855
lemma zless_add1_eq: "(w < z + (1::int)) = (w<z | w=z)"
haftmann@25919
   856
by arith
haftmann@25919
   857
haftmann@25919
   858
lemma add1_zle_eq: "(w + (1::int) \<le> z) = (w<z)"
haftmann@25919
   859
by arith
haftmann@25919
   860
haftmann@25919
   861
lemma zle_diff1_eq [simp]: "(w \<le> z - (1::int)) = (w<z)"
haftmann@25919
   862
by arith
haftmann@25919
   863
haftmann@25919
   864
lemma zle_add1_eq_le [simp]: "(w < z + (1::int)) = (w\<le>z)"
haftmann@25919
   865
by arith
haftmann@25919
   866
haftmann@25919
   867
lemma int_one_le_iff_zero_less: "((1::int) \<le> z) = (0 < z)"
haftmann@25919
   868
by arith
haftmann@25919
   869
haftmann@25919
   870
huffman@28958
   871
subsection{*The functions @{term nat} and @{term int}*}
haftmann@25919
   872
haftmann@25919
   873
text{*Simplify the terms @{term "int 0"}, @{term "int(Suc 0)"} and
haftmann@25919
   874
  @{term "w + - z"}*}
haftmann@25919
   875
declare Zero_int_def [symmetric, simp]
haftmann@25919
   876
declare One_int_def [symmetric, simp]
haftmann@25919
   877
haftmann@25919
   878
lemmas diff_int_def_symmetric = diff_int_def [symmetric, simp]
haftmann@25919
   879
huffman@44695
   880
lemma nat_0 [simp]: "nat 0 = 0"
haftmann@25919
   881
by (simp add: nat_eq_iff)
haftmann@25919
   882
huffman@47207
   883
lemma nat_1 [simp]: "nat 1 = Suc 0"
haftmann@25919
   884
by (subst nat_eq_iff, simp)
haftmann@25919
   885
haftmann@25919
   886
lemma nat_2: "nat 2 = Suc (Suc 0)"
haftmann@25919
   887
by (subst nat_eq_iff, simp)
haftmann@25919
   888
haftmann@25919
   889
lemma one_less_nat_eq [simp]: "(Suc 0 < nat z) = (1 < z)"
haftmann@25919
   890
apply (insert zless_nat_conj [of 1 z])
huffman@47207
   891
apply auto
haftmann@25919
   892
done
haftmann@25919
   893
haftmann@25919
   894
text{*This simplifies expressions of the form @{term "int n = z"} where
haftmann@25919
   895
      z is an integer literal.*}
huffman@47108
   896
lemmas int_eq_iff_numeral [simp] = int_eq_iff [of _ "numeral v"] for v
haftmann@25919
   897
haftmann@25919
   898
lemma split_nat [arith_split]:
huffman@44709
   899
  "P(nat(i::int)) = ((\<forall>n. i = int n \<longrightarrow> P n) & (i < 0 \<longrightarrow> P 0))"
haftmann@25919
   900
  (is "?P = (?L & ?R)")
haftmann@25919
   901
proof (cases "i < 0")
haftmann@25919
   902
  case True thus ?thesis by auto
haftmann@25919
   903
next
haftmann@25919
   904
  case False
haftmann@25919
   905
  have "?P = ?L"
haftmann@25919
   906
  proof
haftmann@25919
   907
    assume ?P thus ?L using False by clarsimp
haftmann@25919
   908
  next
haftmann@25919
   909
    assume ?L thus ?P using False by simp
haftmann@25919
   910
  qed
haftmann@25919
   911
  with False show ?thesis by simp
haftmann@25919
   912
qed
haftmann@25919
   913
haftmann@25919
   914
context ring_1
haftmann@25919
   915
begin
haftmann@25919
   916
blanchet@33056
   917
lemma of_int_of_nat [nitpick_simp]:
haftmann@25919
   918
  "of_int k = (if k < 0 then - of_nat (nat (- k)) else of_nat (nat k))"
haftmann@25919
   919
proof (cases "k < 0")
haftmann@25919
   920
  case True then have "0 \<le> - k" by simp
haftmann@25919
   921
  then have "of_nat (nat (- k)) = of_int (- k)" by (rule of_nat_nat)
haftmann@25919
   922
  with True show ?thesis by simp
haftmann@25919
   923
next
haftmann@25919
   924
  case False then show ?thesis by (simp add: not_less of_nat_nat)
haftmann@25919
   925
qed
haftmann@25919
   926
haftmann@25919
   927
end
haftmann@25919
   928
haftmann@25919
   929
lemma nat_mult_distrib:
haftmann@25919
   930
  fixes z z' :: int
haftmann@25919
   931
  assumes "0 \<le> z"
haftmann@25919
   932
  shows "nat (z * z') = nat z * nat z'"
haftmann@25919
   933
proof (cases "0 \<le> z'")
haftmann@25919
   934
  case False with assms have "z * z' \<le> 0"
haftmann@25919
   935
    by (simp add: not_le mult_le_0_iff)
haftmann@25919
   936
  then have "nat (z * z') = 0" by simp
haftmann@25919
   937
  moreover from False have "nat z' = 0" by simp
haftmann@25919
   938
  ultimately show ?thesis by simp
haftmann@25919
   939
next
haftmann@25919
   940
  case True with assms have ge_0: "z * z' \<ge> 0" by (simp add: zero_le_mult_iff)
haftmann@25919
   941
  show ?thesis
haftmann@25919
   942
    by (rule injD [of "of_nat :: nat \<Rightarrow> int", OF inj_of_nat])
haftmann@25919
   943
      (simp only: of_nat_mult of_nat_nat [OF True]
haftmann@25919
   944
         of_nat_nat [OF assms] of_nat_nat [OF ge_0], simp)
haftmann@25919
   945
qed
haftmann@25919
   946
haftmann@25919
   947
lemma nat_mult_distrib_neg: "z \<le> (0::int) ==> nat(z*z') = nat(-z) * nat(-z')"
haftmann@25919
   948
apply (rule trans)
haftmann@25919
   949
apply (rule_tac [2] nat_mult_distrib, auto)
haftmann@25919
   950
done
haftmann@25919
   951
haftmann@25919
   952
lemma nat_abs_mult_distrib: "nat (abs (w * z)) = nat (abs w) * nat (abs z)"
haftmann@25919
   953
apply (cases "z=0 | w=0")
haftmann@25919
   954
apply (auto simp add: abs_if nat_mult_distrib [symmetric] 
haftmann@25919
   955
                      nat_mult_distrib_neg [symmetric] mult_less_0_iff)
haftmann@25919
   956
done
haftmann@25919
   957
huffman@47207
   958
lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
huffman@47207
   959
apply (rule sym)
huffman@47207
   960
apply (simp add: nat_eq_iff)
huffman@47207
   961
done
huffman@47207
   962
huffman@47207
   963
lemma diff_nat_eq_if:
huffman@47207
   964
     "nat z - nat z' =  
huffman@47207
   965
        (if z' < 0 then nat z   
huffman@47207
   966
         else let d = z-z' in     
huffman@47207
   967
              if d < 0 then 0 else nat d)"
huffman@47207
   968
by (simp add: Let_def nat_diff_distrib [symmetric])
huffman@47207
   969
huffman@47207
   970
(* nat_diff_distrib has too-strong premises *)
huffman@47207
   971
lemma nat_diff_distrib': "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x - y) = nat x - nat y"
huffman@47207
   972
apply (rule int_int_eq [THEN iffD1], clarsimp)
huffman@47207
   973
apply (subst of_nat_diff)
huffman@47207
   974
apply (rule nat_mono, simp_all)
huffman@47207
   975
done
huffman@47207
   976
huffman@47207
   977
lemma nat_numeral [simp, code_abbrev]:
huffman@47207
   978
  "nat (numeral k) = numeral k"
huffman@47207
   979
  by (simp add: nat_eq_iff)
huffman@47207
   980
huffman@47207
   981
lemma nat_neg_numeral [simp]:
huffman@47207
   982
  "nat (neg_numeral k) = 0"
huffman@47207
   983
  by simp
huffman@47207
   984
huffman@47207
   985
lemma diff_nat_numeral [simp]: 
huffman@47207
   986
  "(numeral v :: nat) - numeral v' = nat (numeral v - numeral v')"
huffman@47207
   987
  by (simp only: nat_diff_distrib' zero_le_numeral nat_numeral)
huffman@47207
   988
huffman@47207
   989
lemma nat_numeral_diff_1 [simp]:
huffman@47207
   990
  "numeral v - (1::nat) = nat (numeral v - 1)"
huffman@47207
   991
  using diff_nat_numeral [of v Num.One] by simp
huffman@47207
   992
huffman@47255
   993
lemmas nat_arith = diff_nat_numeral
huffman@47255
   994
haftmann@25919
   995
haftmann@25919
   996
subsection "Induction principles for int"
haftmann@25919
   997
haftmann@25919
   998
text{*Well-founded segments of the integers*}
haftmann@25919
   999
haftmann@25919
  1000
definition
haftmann@25919
  1001
  int_ge_less_than  ::  "int => (int * int) set"
haftmann@25919
  1002
where
haftmann@25919
  1003
  "int_ge_less_than d = {(z',z). d \<le> z' & z' < z}"
haftmann@25919
  1004
haftmann@25919
  1005
theorem wf_int_ge_less_than: "wf (int_ge_less_than d)"
haftmann@25919
  1006
proof -
haftmann@25919
  1007
  have "int_ge_less_than d \<subseteq> measure (%z. nat (z-d))"
haftmann@25919
  1008
    by (auto simp add: int_ge_less_than_def)
haftmann@25919
  1009
  thus ?thesis 
haftmann@25919
  1010
    by (rule wf_subset [OF wf_measure]) 
haftmann@25919
  1011
qed
haftmann@25919
  1012
haftmann@25919
  1013
text{*This variant looks odd, but is typical of the relations suggested
haftmann@25919
  1014
by RankFinder.*}
haftmann@25919
  1015
haftmann@25919
  1016
definition
haftmann@25919
  1017
  int_ge_less_than2 ::  "int => (int * int) set"
haftmann@25919
  1018
where
haftmann@25919
  1019
  "int_ge_less_than2 d = {(z',z). d \<le> z & z' < z}"
haftmann@25919
  1020
haftmann@25919
  1021
theorem wf_int_ge_less_than2: "wf (int_ge_less_than2 d)"
haftmann@25919
  1022
proof -
haftmann@25919
  1023
  have "int_ge_less_than2 d \<subseteq> measure (%z. nat (1+z-d))" 
haftmann@25919
  1024
    by (auto simp add: int_ge_less_than2_def)
haftmann@25919
  1025
  thus ?thesis 
haftmann@25919
  1026
    by (rule wf_subset [OF wf_measure]) 
haftmann@25919
  1027
qed
haftmann@25919
  1028
haftmann@25919
  1029
(* `set:int': dummy construction *)
haftmann@25919
  1030
theorem int_ge_induct [case_names base step, induct set: int]:
haftmann@25919
  1031
  fixes i :: int
haftmann@25919
  1032
  assumes ge: "k \<le> i" and
haftmann@25919
  1033
    base: "P k" and
haftmann@25919
  1034
    step: "\<And>i. k \<le> i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
haftmann@25919
  1035
  shows "P i"
haftmann@25919
  1036
proof -
wenzelm@42676
  1037
  { fix n
wenzelm@42676
  1038
    have "\<And>i::int. n = nat (i - k) \<Longrightarrow> k \<le> i \<Longrightarrow> P i"
haftmann@25919
  1039
    proof (induct n)
haftmann@25919
  1040
      case 0
haftmann@25919
  1041
      hence "i = k" by arith
haftmann@25919
  1042
      thus "P i" using base by simp
haftmann@25919
  1043
    next
haftmann@25919
  1044
      case (Suc n)
haftmann@25919
  1045
      then have "n = nat((i - 1) - k)" by arith
haftmann@25919
  1046
      moreover
haftmann@25919
  1047
      have ki1: "k \<le> i - 1" using Suc.prems by arith
haftmann@25919
  1048
      ultimately
wenzelm@42676
  1049
      have "P (i - 1)" by (rule Suc.hyps)
wenzelm@42676
  1050
      from step [OF ki1 this] show ?case by simp
haftmann@25919
  1051
    qed
haftmann@25919
  1052
  }
haftmann@25919
  1053
  with ge show ?thesis by fast
haftmann@25919
  1054
qed
haftmann@25919
  1055
haftmann@25928
  1056
(* `set:int': dummy construction *)
haftmann@25928
  1057
theorem int_gr_induct [case_names base step, induct set: int]:
haftmann@25919
  1058
  assumes gr: "k < (i::int)" and
haftmann@25919
  1059
        base: "P(k+1)" and
haftmann@25919
  1060
        step: "\<And>i. \<lbrakk>k < i; P i\<rbrakk> \<Longrightarrow> P(i+1)"
haftmann@25919
  1061
  shows "P i"
haftmann@25919
  1062
apply(rule int_ge_induct[of "k + 1"])
haftmann@25919
  1063
  using gr apply arith
haftmann@25919
  1064
 apply(rule base)
haftmann@25919
  1065
apply (rule step, simp+)
haftmann@25919
  1066
done
haftmann@25919
  1067
wenzelm@42676
  1068
theorem int_le_induct [consumes 1, case_names base step]:
haftmann@25919
  1069
  assumes le: "i \<le> (k::int)" and
haftmann@25919
  1070
        base: "P(k)" and
haftmann@25919
  1071
        step: "\<And>i. \<lbrakk>i \<le> k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
haftmann@25919
  1072
  shows "P i"
haftmann@25919
  1073
proof -
wenzelm@42676
  1074
  { fix n
wenzelm@42676
  1075
    have "\<And>i::int. n = nat(k-i) \<Longrightarrow> i \<le> k \<Longrightarrow> P i"
haftmann@25919
  1076
    proof (induct n)
haftmann@25919
  1077
      case 0
haftmann@25919
  1078
      hence "i = k" by arith
haftmann@25919
  1079
      thus "P i" using base by simp
haftmann@25919
  1080
    next
haftmann@25919
  1081
      case (Suc n)
wenzelm@42676
  1082
      hence "n = nat (k - (i + 1))" by arith
haftmann@25919
  1083
      moreover
haftmann@25919
  1084
      have ki1: "i + 1 \<le> k" using Suc.prems by arith
haftmann@25919
  1085
      ultimately
wenzelm@42676
  1086
      have "P (i + 1)" by(rule Suc.hyps)
haftmann@25919
  1087
      from step[OF ki1 this] show ?case by simp
haftmann@25919
  1088
    qed
haftmann@25919
  1089
  }
haftmann@25919
  1090
  with le show ?thesis by fast
haftmann@25919
  1091
qed
haftmann@25919
  1092
wenzelm@42676
  1093
theorem int_less_induct [consumes 1, case_names base step]:
haftmann@25919
  1094
  assumes less: "(i::int) < k" and
haftmann@25919
  1095
        base: "P(k - 1)" and
haftmann@25919
  1096
        step: "\<And>i. \<lbrakk>i < k; P i\<rbrakk> \<Longrightarrow> P(i - 1)"
haftmann@25919
  1097
  shows "P i"
haftmann@25919
  1098
apply(rule int_le_induct[of _ "k - 1"])
haftmann@25919
  1099
  using less apply arith
haftmann@25919
  1100
 apply(rule base)
haftmann@25919
  1101
apply (rule step, simp+)
haftmann@25919
  1102
done
haftmann@25919
  1103
haftmann@36811
  1104
theorem int_induct [case_names base step1 step2]:
haftmann@36801
  1105
  fixes k :: int
haftmann@36801
  1106
  assumes base: "P k"
haftmann@36801
  1107
    and step1: "\<And>i. k \<le> i \<Longrightarrow> P i \<Longrightarrow> P (i + 1)"
haftmann@36801
  1108
    and step2: "\<And>i. k \<ge> i \<Longrightarrow> P i \<Longrightarrow> P (i - 1)"
haftmann@36801
  1109
  shows "P i"
haftmann@36801
  1110
proof -
haftmann@36801
  1111
  have "i \<le> k \<or> i \<ge> k" by arith
wenzelm@42676
  1112
  then show ?thesis
wenzelm@42676
  1113
  proof
wenzelm@42676
  1114
    assume "i \<ge> k"
wenzelm@42676
  1115
    then show ?thesis using base
haftmann@36801
  1116
      by (rule int_ge_induct) (fact step1)
haftmann@36801
  1117
  next
wenzelm@42676
  1118
    assume "i \<le> k"
wenzelm@42676
  1119
    then show ?thesis using base
haftmann@36801
  1120
      by (rule int_le_induct) (fact step2)
haftmann@36801
  1121
  qed
haftmann@36801
  1122
qed
haftmann@36801
  1123
haftmann@25919
  1124
subsection{*Intermediate value theorems*}
haftmann@25919
  1125
haftmann@25919
  1126
lemma int_val_lemma:
haftmann@25919
  1127
     "(\<forall>i<n::nat. abs(f(i+1) - f i) \<le> 1) -->  
haftmann@25919
  1128
      f 0 \<le> k --> k \<le> f n --> (\<exists>i \<le> n. f i = (k::int))"
huffman@30079
  1129
unfolding One_nat_def
wenzelm@42676
  1130
apply (induct n)
wenzelm@42676
  1131
apply simp
haftmann@25919
  1132
apply (intro strip)
haftmann@25919
  1133
apply (erule impE, simp)
haftmann@25919
  1134
apply (erule_tac x = n in allE, simp)
huffman@30079
  1135
apply (case_tac "k = f (Suc n)")
haftmann@27106
  1136
apply force
haftmann@25919
  1137
apply (erule impE)
haftmann@25919
  1138
 apply (simp add: abs_if split add: split_if_asm)
haftmann@25919
  1139
apply (blast intro: le_SucI)
haftmann@25919
  1140
done
haftmann@25919
  1141
haftmann@25919
  1142
lemmas nat0_intermed_int_val = int_val_lemma [rule_format (no_asm)]
haftmann@25919
  1143
haftmann@25919
  1144
lemma nat_intermed_int_val:
haftmann@25919
  1145
     "[| \<forall>i. m \<le> i & i < n --> abs(f(i + 1::nat) - f i) \<le> 1; m < n;  
haftmann@25919
  1146
         f m \<le> k; k \<le> f n |] ==> ? i. m \<le> i & i \<le> n & f i = (k::int)"
haftmann@25919
  1147
apply (cut_tac n = "n-m" and f = "%i. f (i+m) " and k = k 
haftmann@25919
  1148
       in int_val_lemma)
huffman@30079
  1149
unfolding One_nat_def
haftmann@25919
  1150
apply simp
haftmann@25919
  1151
apply (erule exE)
haftmann@25919
  1152
apply (rule_tac x = "i+m" in exI, arith)
haftmann@25919
  1153
done
haftmann@25919
  1154
haftmann@25919
  1155
haftmann@25919
  1156
subsection{*Products and 1, by T. M. Rasmussen*}
haftmann@25919
  1157
haftmann@25919
  1158
lemma zabs_less_one_iff [simp]: "(\<bar>z\<bar> < 1) = (z = (0::int))"
haftmann@25919
  1159
by arith
haftmann@25919
  1160
paulson@34055
  1161
lemma abs_zmult_eq_1:
paulson@34055
  1162
  assumes mn: "\<bar>m * n\<bar> = 1"
paulson@34055
  1163
  shows "\<bar>m\<bar> = (1::int)"
paulson@34055
  1164
proof -
paulson@34055
  1165
  have 0: "m \<noteq> 0 & n \<noteq> 0" using mn
paulson@34055
  1166
    by auto
paulson@34055
  1167
  have "~ (2 \<le> \<bar>m\<bar>)"
paulson@34055
  1168
  proof
paulson@34055
  1169
    assume "2 \<le> \<bar>m\<bar>"
paulson@34055
  1170
    hence "2*\<bar>n\<bar> \<le> \<bar>m\<bar>*\<bar>n\<bar>"
paulson@34055
  1171
      by (simp add: mult_mono 0) 
paulson@34055
  1172
    also have "... = \<bar>m*n\<bar>" 
paulson@34055
  1173
      by (simp add: abs_mult)
paulson@34055
  1174
    also have "... = 1"
paulson@34055
  1175
      by (simp add: mn)
paulson@34055
  1176
    finally have "2*\<bar>n\<bar> \<le> 1" .
paulson@34055
  1177
    thus "False" using 0
huffman@47108
  1178
      by arith
paulson@34055
  1179
  qed
paulson@34055
  1180
  thus ?thesis using 0
paulson@34055
  1181
    by auto
paulson@34055
  1182
qed
haftmann@25919
  1183
huffman@47108
  1184
ML_val {* @{const_name neg_numeral} *}
huffman@47108
  1185
haftmann@25919
  1186
lemma pos_zmult_eq_1_iff_lemma: "(m * n = 1) ==> m = (1::int) | m = -1"
haftmann@25919
  1187
by (insert abs_zmult_eq_1 [of m n], arith)
haftmann@25919
  1188
boehmes@35815
  1189
lemma pos_zmult_eq_1_iff:
boehmes@35815
  1190
  assumes "0 < (m::int)" shows "(m * n = 1) = (m = 1 & n = 1)"
boehmes@35815
  1191
proof -
boehmes@35815
  1192
  from assms have "m * n = 1 ==> m = 1" by (auto dest: pos_zmult_eq_1_iff_lemma)
boehmes@35815
  1193
  thus ?thesis by (auto dest: pos_zmult_eq_1_iff_lemma)
boehmes@35815
  1194
qed
haftmann@25919
  1195
haftmann@25919
  1196
lemma zmult_eq_1_iff: "(m*n = (1::int)) = ((m = 1 & n = 1) | (m = -1 & n = -1))"
haftmann@25919
  1197
apply (rule iffI) 
haftmann@25919
  1198
 apply (frule pos_zmult_eq_1_iff_lemma)
haftmann@25919
  1199
 apply (simp add: mult_commute [of m]) 
haftmann@25919
  1200
 apply (frule pos_zmult_eq_1_iff_lemma, auto) 
haftmann@25919
  1201
done
haftmann@25919
  1202
haftmann@33296
  1203
lemma infinite_UNIV_int: "\<not> finite (UNIV::int set)"
haftmann@25919
  1204
proof
haftmann@33296
  1205
  assume "finite (UNIV::int set)"
haftmann@33296
  1206
  moreover have "inj (\<lambda>i\<Colon>int. 2 * i)"
haftmann@33296
  1207
    by (rule injI) simp
haftmann@33296
  1208
  ultimately have "surj (\<lambda>i\<Colon>int. 2 * i)"
haftmann@33296
  1209
    by (rule finite_UNIV_inj_surj)
haftmann@33296
  1210
  then obtain i :: int where "1 = 2 * i" by (rule surjE)
haftmann@33296
  1211
  then show False by (simp add: pos_zmult_eq_1_iff)
haftmann@25919
  1212
qed
haftmann@25919
  1213
haftmann@25919
  1214
haftmann@30652
  1215
subsection {* Further theorems on numerals *}
haftmann@30652
  1216
haftmann@30652
  1217
subsubsection{*Special Simplification for Constants*}
haftmann@30652
  1218
haftmann@30652
  1219
text{*These distributive laws move literals inside sums and differences.*}
haftmann@30652
  1220
huffman@47108
  1221
lemmas left_distrib_numeral [simp] = left_distrib [of _ _ "numeral v"] for v
huffman@47108
  1222
lemmas right_distrib_numeral [simp] = right_distrib [of "numeral v"] for v
huffman@47108
  1223
lemmas left_diff_distrib_numeral [simp] = left_diff_distrib [of _ _ "numeral v"] for v
huffman@47108
  1224
lemmas right_diff_distrib_numeral [simp] = right_diff_distrib [of "numeral v"] for v
haftmann@30652
  1225
haftmann@30652
  1226
text{*These are actually for fields, like real: but where else to put them?*}
haftmann@30652
  1227
huffman@47108
  1228
lemmas zero_less_divide_iff_numeral [simp, no_atp] = zero_less_divide_iff [of "numeral w"] for w
huffman@47108
  1229
lemmas divide_less_0_iff_numeral [simp, no_atp] = divide_less_0_iff [of "numeral w"] for w
huffman@47108
  1230
lemmas zero_le_divide_iff_numeral [simp, no_atp] = zero_le_divide_iff [of "numeral w"] for w
huffman@47108
  1231
lemmas divide_le_0_iff_numeral [simp, no_atp] = divide_le_0_iff [of "numeral w"] for w
haftmann@30652
  1232
haftmann@30652
  1233
haftmann@30652
  1234
text {*Replaces @{text "inverse #nn"} by @{text "1/#nn"}.  It looks
haftmann@30652
  1235
  strange, but then other simprocs simplify the quotient.*}
haftmann@30652
  1236
huffman@47108
  1237
lemmas inverse_eq_divide_numeral [simp] =
huffman@47108
  1238
  inverse_eq_divide [of "numeral w"] for w
huffman@47108
  1239
huffman@47108
  1240
lemmas inverse_eq_divide_neg_numeral [simp] =
huffman@47108
  1241
  inverse_eq_divide [of "neg_numeral w"] for w
haftmann@30652
  1242
haftmann@30652
  1243
text {*These laws simplify inequalities, moving unary minus from a term
haftmann@30652
  1244
into the literal.*}
haftmann@30652
  1245
huffman@47108
  1246
lemmas le_minus_iff_numeral [simp, no_atp] =
huffman@47108
  1247
  le_minus_iff [of "numeral v"]
huffman@47108
  1248
  le_minus_iff [of "neg_numeral v"] for v
huffman@47108
  1249
huffman@47108
  1250
lemmas equation_minus_iff_numeral [simp, no_atp] =
huffman@47108
  1251
  equation_minus_iff [of "numeral v"]
huffman@47108
  1252
  equation_minus_iff [of "neg_numeral v"] for v
huffman@47108
  1253
huffman@47108
  1254
lemmas minus_less_iff_numeral [simp, no_atp] =
huffman@47108
  1255
  minus_less_iff [of _ "numeral v"]
huffman@47108
  1256
  minus_less_iff [of _ "neg_numeral v"] for v
huffman@47108
  1257
huffman@47108
  1258
lemmas minus_le_iff_numeral [simp, no_atp] =
huffman@47108
  1259
  minus_le_iff [of _ "numeral v"]
huffman@47108
  1260
  minus_le_iff [of _ "neg_numeral v"] for v
huffman@47108
  1261
huffman@47108
  1262
lemmas minus_equation_iff_numeral [simp, no_atp] =
huffman@47108
  1263
  minus_equation_iff [of _ "numeral v"]
huffman@47108
  1264
  minus_equation_iff [of _ "neg_numeral v"] for v
haftmann@30652
  1265
haftmann@30652
  1266
text{*To Simplify Inequalities Where One Side is the Constant 1*}
haftmann@30652
  1267
blanchet@35828
  1268
lemma less_minus_iff_1 [simp,no_atp]:
huffman@47108
  1269
  fixes b::"'b::linordered_idom"
haftmann@30652
  1270
  shows "(1 < - b) = (b < -1)"
haftmann@30652
  1271
by auto
haftmann@30652
  1272
blanchet@35828
  1273
lemma le_minus_iff_1 [simp,no_atp]:
huffman@47108
  1274
  fixes b::"'b::linordered_idom"
haftmann@30652
  1275
  shows "(1 \<le> - b) = (b \<le> -1)"
haftmann@30652
  1276
by auto
haftmann@30652
  1277
blanchet@35828
  1278
lemma equation_minus_iff_1 [simp,no_atp]:
huffman@47108
  1279
  fixes b::"'b::ring_1"
haftmann@30652
  1280
  shows "(1 = - b) = (b = -1)"
haftmann@30652
  1281
by (subst equation_minus_iff, auto)
haftmann@30652
  1282
blanchet@35828
  1283
lemma minus_less_iff_1 [simp,no_atp]:
huffman@47108
  1284
  fixes a::"'b::linordered_idom"
haftmann@30652
  1285
  shows "(- a < 1) = (-1 < a)"
haftmann@30652
  1286
by auto
haftmann@30652
  1287
blanchet@35828
  1288
lemma minus_le_iff_1 [simp,no_atp]:
huffman@47108
  1289
  fixes a::"'b::linordered_idom"
haftmann@30652
  1290
  shows "(- a \<le> 1) = (-1 \<le> a)"
haftmann@30652
  1291
by auto
haftmann@30652
  1292
blanchet@35828
  1293
lemma minus_equation_iff_1 [simp,no_atp]:
huffman@47108
  1294
  fixes a::"'b::ring_1"
haftmann@30652
  1295
  shows "(- a = 1) = (a = -1)"
haftmann@30652
  1296
by (subst minus_equation_iff, auto)
haftmann@30652
  1297
haftmann@30652
  1298
haftmann@30652
  1299
text {*Cancellation of constant factors in comparisons (@{text "<"} and @{text "\<le>"}) *}
haftmann@30652
  1300
huffman@47108
  1301
lemmas mult_less_cancel_left_numeral [simp, no_atp] = mult_less_cancel_left [of "numeral v"] for v
huffman@47108
  1302
lemmas mult_less_cancel_right_numeral [simp, no_atp] = mult_less_cancel_right [of _ "numeral v"] for v
huffman@47108
  1303
lemmas mult_le_cancel_left_numeral [simp, no_atp] = mult_le_cancel_left [of "numeral v"] for v
huffman@47108
  1304
lemmas mult_le_cancel_right_numeral [simp, no_atp] = mult_le_cancel_right [of _ "numeral v"] for v
haftmann@30652
  1305
haftmann@30652
  1306
haftmann@30652
  1307
text {*Multiplying out constant divisors in comparisons (@{text "<"}, @{text "\<le>"} and @{text "="}) *}
haftmann@30652
  1308
huffman@47108
  1309
lemmas le_divide_eq_numeral1 [simp] =
huffman@47108
  1310
  pos_le_divide_eq [of "numeral w", OF zero_less_numeral]
huffman@47108
  1311
  neg_le_divide_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
huffman@47108
  1312
huffman@47108
  1313
lemmas divide_le_eq_numeral1 [simp] =
huffman@47108
  1314
  pos_divide_le_eq [of "numeral w", OF zero_less_numeral]
huffman@47108
  1315
  neg_divide_le_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
huffman@47108
  1316
huffman@47108
  1317
lemmas less_divide_eq_numeral1 [simp] =
huffman@47108
  1318
  pos_less_divide_eq [of "numeral w", OF zero_less_numeral]
huffman@47108
  1319
  neg_less_divide_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
haftmann@30652
  1320
huffman@47108
  1321
lemmas divide_less_eq_numeral1 [simp] =
huffman@47108
  1322
  pos_divide_less_eq [of "numeral w", OF zero_less_numeral]
huffman@47108
  1323
  neg_divide_less_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
huffman@47108
  1324
huffman@47108
  1325
lemmas eq_divide_eq_numeral1 [simp] =
huffman@47108
  1326
  eq_divide_eq [of _ _ "numeral w"]
huffman@47108
  1327
  eq_divide_eq [of _ _ "neg_numeral w"] for w
huffman@47108
  1328
huffman@47108
  1329
lemmas divide_eq_eq_numeral1 [simp] =
huffman@47108
  1330
  divide_eq_eq [of _ "numeral w"]
huffman@47108
  1331
  divide_eq_eq [of _ "neg_numeral w"] for w
haftmann@30652
  1332
haftmann@30652
  1333
subsubsection{*Optional Simplification Rules Involving Constants*}
haftmann@30652
  1334
haftmann@30652
  1335
text{*Simplify quotients that are compared with a literal constant.*}
haftmann@30652
  1336
huffman@47108
  1337
lemmas le_divide_eq_numeral =
huffman@47108
  1338
  le_divide_eq [of "numeral w"]
huffman@47108
  1339
  le_divide_eq [of "neg_numeral w"] for w
huffman@47108
  1340
huffman@47108
  1341
lemmas divide_le_eq_numeral =
huffman@47108
  1342
  divide_le_eq [of _ _ "numeral w"]
huffman@47108
  1343
  divide_le_eq [of _ _ "neg_numeral w"] for w
huffman@47108
  1344
huffman@47108
  1345
lemmas less_divide_eq_numeral =
huffman@47108
  1346
  less_divide_eq [of "numeral w"]
huffman@47108
  1347
  less_divide_eq [of "neg_numeral w"] for w
huffman@47108
  1348
huffman@47108
  1349
lemmas divide_less_eq_numeral =
huffman@47108
  1350
  divide_less_eq [of _ _ "numeral w"]
huffman@47108
  1351
  divide_less_eq [of _ _ "neg_numeral w"] for w
huffman@47108
  1352
huffman@47108
  1353
lemmas eq_divide_eq_numeral =
huffman@47108
  1354
  eq_divide_eq [of "numeral w"]
huffman@47108
  1355
  eq_divide_eq [of "neg_numeral w"] for w
huffman@47108
  1356
huffman@47108
  1357
lemmas divide_eq_eq_numeral =
huffman@47108
  1358
  divide_eq_eq [of _ _ "numeral w"]
huffman@47108
  1359
  divide_eq_eq [of _ _ "neg_numeral w"] for w
haftmann@30652
  1360
haftmann@30652
  1361
haftmann@30652
  1362
text{*Not good as automatic simprules because they cause case splits.*}
haftmann@30652
  1363
lemmas divide_const_simps =
huffman@47108
  1364
  le_divide_eq_numeral divide_le_eq_numeral less_divide_eq_numeral
huffman@47108
  1365
  divide_less_eq_numeral eq_divide_eq_numeral divide_eq_eq_numeral
haftmann@30652
  1366
  le_divide_eq_1 divide_le_eq_1 less_divide_eq_1 divide_less_eq_1
haftmann@30652
  1367
haftmann@30652
  1368
text{*Division By @{text "-1"}*}
haftmann@30652
  1369
huffman@47108
  1370
lemma divide_minus1 [simp]: "(x::'a::field) / -1 = - x"
huffman@47108
  1371
  unfolding minus_one [symmetric]
huffman@47108
  1372
  unfolding nonzero_minus_divide_right [OF one_neq_zero, symmetric]
huffman@47108
  1373
  by simp
haftmann@30652
  1374
huffman@47108
  1375
lemma minus1_divide [simp]: "-1 / (x::'a::field) = - (1 / x)"
huffman@47108
  1376
  unfolding minus_one [symmetric] by (rule divide_minus_left)
haftmann@30652
  1377
haftmann@30652
  1378
lemma half_gt_zero_iff:
huffman@47108
  1379
     "(0 < r/2) = (0 < (r::'a::linordered_field_inverse_zero))"
haftmann@30652
  1380
by auto
haftmann@30652
  1381
wenzelm@45607
  1382
lemmas half_gt_zero [simp] = half_gt_zero_iff [THEN iffD2]
haftmann@30652
  1383
huffman@47108
  1384
lemma divide_Numeral1: "(x::'a::field) / Numeral1 = x"
haftmann@36719
  1385
  by simp
haftmann@36719
  1386
haftmann@30652
  1387
haftmann@33320
  1388
subsection {* The divides relation *}
haftmann@33320
  1389
nipkow@33657
  1390
lemma zdvd_antisym_nonneg:
nipkow@33657
  1391
    "0 <= m ==> 0 <= n ==> m dvd n ==> n dvd m ==> m = (n::int)"
haftmann@33320
  1392
  apply (simp add: dvd_def, auto)
nipkow@33657
  1393
  apply (auto simp add: mult_assoc zero_le_mult_iff zmult_eq_1_iff)
haftmann@33320
  1394
  done
haftmann@33320
  1395
nipkow@33657
  1396
lemma zdvd_antisym_abs: assumes "(a::int) dvd b" and "b dvd a" 
haftmann@33320
  1397
  shows "\<bar>a\<bar> = \<bar>b\<bar>"
nipkow@33657
  1398
proof cases
nipkow@33657
  1399
  assume "a = 0" with assms show ?thesis by simp
nipkow@33657
  1400
next
nipkow@33657
  1401
  assume "a \<noteq> 0"
haftmann@33320
  1402
  from `a dvd b` obtain k where k:"b = a*k" unfolding dvd_def by blast 
haftmann@33320
  1403
  from `b dvd a` obtain k' where k':"a = b*k'" unfolding dvd_def by blast 
haftmann@33320
  1404
  from k k' have "a = a*k*k'" by simp
haftmann@33320
  1405
  with mult_cancel_left1[where c="a" and b="k*k'"]
haftmann@33320
  1406
  have kk':"k*k' = 1" using `a\<noteq>0` by (simp add: mult_assoc)
haftmann@33320
  1407
  hence "k = 1 \<and> k' = 1 \<or> k = -1 \<and> k' = -1" by (simp add: zmult_eq_1_iff)
haftmann@33320
  1408
  thus ?thesis using k k' by auto
haftmann@33320
  1409
qed
haftmann@33320
  1410
haftmann@33320
  1411
lemma zdvd_zdiffD: "k dvd m - n ==> k dvd n ==> k dvd (m::int)"
haftmann@33320
  1412
  apply (subgoal_tac "m = n + (m - n)")
haftmann@33320
  1413
   apply (erule ssubst)
haftmann@33320
  1414
   apply (blast intro: dvd_add, simp)
haftmann@33320
  1415
  done
haftmann@33320
  1416
haftmann@33320
  1417
lemma zdvd_reduce: "(k dvd n + k * m) = (k dvd (n::int))"
haftmann@33320
  1418
apply (rule iffI)
haftmann@33320
  1419
 apply (erule_tac [2] dvd_add)
haftmann@33320
  1420
 apply (subgoal_tac "n = (n + k * m) - k * m")
haftmann@33320
  1421
  apply (erule ssubst)
haftmann@33320
  1422
  apply (erule dvd_diff)
haftmann@33320
  1423
  apply(simp_all)
haftmann@33320
  1424
done
haftmann@33320
  1425
haftmann@33320
  1426
lemma dvd_imp_le_int:
haftmann@33320
  1427
  fixes d i :: int
haftmann@33320
  1428
  assumes "i \<noteq> 0" and "d dvd i"
haftmann@33320
  1429
  shows "\<bar>d\<bar> \<le> \<bar>i\<bar>"
haftmann@33320
  1430
proof -
haftmann@33320
  1431
  from `d dvd i` obtain k where "i = d * k" ..
haftmann@33320
  1432
  with `i \<noteq> 0` have "k \<noteq> 0" by auto
haftmann@33320
  1433
  then have "1 \<le> \<bar>k\<bar>" and "0 \<le> \<bar>d\<bar>" by auto
haftmann@33320
  1434
  then have "\<bar>d\<bar> * 1 \<le> \<bar>d\<bar> * \<bar>k\<bar>" by (rule mult_left_mono)
haftmann@33320
  1435
  with `i = d * k` show ?thesis by (simp add: abs_mult)
haftmann@33320
  1436
qed
haftmann@33320
  1437
haftmann@33320
  1438
lemma zdvd_not_zless:
haftmann@33320
  1439
  fixes m n :: int
haftmann@33320
  1440
  assumes "0 < m" and "m < n"
haftmann@33320
  1441
  shows "\<not> n dvd m"
haftmann@33320
  1442
proof
haftmann@33320
  1443
  from assms have "0 < n" by auto
haftmann@33320
  1444
  assume "n dvd m" then obtain k where k: "m = n * k" ..
haftmann@33320
  1445
  with `0 < m` have "0 < n * k" by auto
haftmann@33320
  1446
  with `0 < n` have "0 < k" by (simp add: zero_less_mult_iff)
haftmann@33320
  1447
  with k `0 < n` `m < n` have "n * k < n * 1" by simp
haftmann@33320
  1448
  with `0 < n` `0 < k` show False unfolding mult_less_cancel_left by auto
haftmann@33320
  1449
qed
haftmann@33320
  1450
haftmann@33320
  1451
lemma zdvd_mult_cancel: assumes d:"k * m dvd k * n" and kz:"k \<noteq> (0::int)"
haftmann@33320
  1452
  shows "m dvd n"
haftmann@33320
  1453
proof-
haftmann@33320
  1454
  from d obtain h where h: "k*n = k*m * h" unfolding dvd_def by blast
haftmann@33320
  1455
  {assume "n \<noteq> m*h" hence "k* n \<noteq> k* (m*h)" using kz by simp
haftmann@33320
  1456
    with h have False by (simp add: mult_assoc)}
haftmann@33320
  1457
  hence "n = m * h" by blast
haftmann@33320
  1458
  thus ?thesis by simp
haftmann@33320
  1459
qed
haftmann@33320
  1460
haftmann@33320
  1461
theorem zdvd_int: "(x dvd y) = (int x dvd int y)"
haftmann@33320
  1462
proof -
haftmann@33320
  1463
  have "\<And>k. int y = int x * k \<Longrightarrow> x dvd y"
haftmann@33320
  1464
  proof -
haftmann@33320
  1465
    fix k
haftmann@33320
  1466
    assume A: "int y = int x * k"
wenzelm@42676
  1467
    then show "x dvd y"
wenzelm@42676
  1468
    proof (cases k)
wenzelm@42676
  1469
      case (nonneg n)
wenzelm@42676
  1470
      with A have "y = x * n" by (simp add: of_nat_mult [symmetric])
haftmann@33320
  1471
      then show ?thesis ..
haftmann@33320
  1472
    next
wenzelm@42676
  1473
      case (neg n)
wenzelm@42676
  1474
      with A have "int y = int x * (- int (Suc n))" by simp
haftmann@33320
  1475
      also have "\<dots> = - (int x * int (Suc n))" by (simp only: mult_minus_right)
haftmann@33320
  1476
      also have "\<dots> = - int (x * Suc n)" by (simp only: of_nat_mult [symmetric])
haftmann@33320
  1477
      finally have "- int (x * Suc n) = int y" ..
haftmann@33320
  1478
      then show ?thesis by (simp only: negative_eq_positive) auto
haftmann@33320
  1479
    qed
haftmann@33320
  1480
  qed
haftmann@33320
  1481
  then show ?thesis by (auto elim!: dvdE simp only: dvd_triv_left of_nat_mult)
haftmann@33320
  1482
qed
haftmann@33320
  1483
wenzelm@42676
  1484
lemma zdvd1_eq[simp]: "(x::int) dvd 1 = (\<bar>x\<bar> = 1)"
haftmann@33320
  1485
proof
haftmann@33320
  1486
  assume d: "x dvd 1" hence "int (nat \<bar>x\<bar>) dvd int (nat 1)" by simp
haftmann@33320
  1487
  hence "nat \<bar>x\<bar> dvd 1" by (simp add: zdvd_int)
haftmann@33320
  1488
  hence "nat \<bar>x\<bar> = 1"  by simp
wenzelm@42676
  1489
  thus "\<bar>x\<bar> = 1" by (cases "x < 0") auto
haftmann@33320
  1490
next
haftmann@33320
  1491
  assume "\<bar>x\<bar>=1"
haftmann@33320
  1492
  then have "x = 1 \<or> x = -1" by auto
haftmann@33320
  1493
  then show "x dvd 1" by (auto intro: dvdI)
haftmann@33320
  1494
qed
haftmann@33320
  1495
haftmann@33320
  1496
lemma zdvd_mult_cancel1: 
haftmann@33320
  1497
  assumes mp:"m \<noteq>(0::int)" shows "(m * n dvd m) = (\<bar>n\<bar> = 1)"
haftmann@33320
  1498
proof
haftmann@33320
  1499
  assume n1: "\<bar>n\<bar> = 1" thus "m * n dvd m" 
wenzelm@42676
  1500
    by (cases "n >0") (auto simp add: minus_equation_iff)
haftmann@33320
  1501
next
haftmann@33320
  1502
  assume H: "m * n dvd m" hence H2: "m * n dvd m * 1" by simp
haftmann@33320
  1503
  from zdvd_mult_cancel[OF H2 mp] show "\<bar>n\<bar> = 1" by (simp only: zdvd1_eq)
haftmann@33320
  1504
qed
haftmann@33320
  1505
haftmann@33320
  1506
lemma int_dvd_iff: "(int m dvd z) = (m dvd nat (abs z))"
haftmann@33320
  1507
  unfolding zdvd_int by (cases "z \<ge> 0") simp_all
haftmann@33320
  1508
haftmann@33320
  1509
lemma dvd_int_iff: "(z dvd int m) = (nat (abs z) dvd m)"
haftmann@33320
  1510
  unfolding zdvd_int by (cases "z \<ge> 0") simp_all
haftmann@33320
  1511
haftmann@33320
  1512
lemma nat_dvd_iff: "(nat z dvd m) = (if 0 \<le> z then (z dvd int m) else m = 0)"
haftmann@33320
  1513
  by (auto simp add: dvd_int_iff)
haftmann@33320
  1514
haftmann@33341
  1515
lemma eq_nat_nat_iff:
haftmann@33341
  1516
  "0 \<le> z \<Longrightarrow> 0 \<le> z' \<Longrightarrow> nat z = nat z' \<longleftrightarrow> z = z'"
haftmann@33341
  1517
  by (auto elim!: nonneg_eq_int)
haftmann@33341
  1518
haftmann@33341
  1519
lemma nat_power_eq:
haftmann@33341
  1520
  "0 \<le> z \<Longrightarrow> nat (z ^ n) = nat z ^ n"
haftmann@33341
  1521
  by (induct n) (simp_all add: nat_mult_distrib)
haftmann@33341
  1522
haftmann@33320
  1523
lemma zdvd_imp_le: "[| z dvd n; 0 < n |] ==> z \<le> (n::int)"
wenzelm@42676
  1524
  apply (cases n)
haftmann@33320
  1525
  apply (auto simp add: dvd_int_iff)
wenzelm@42676
  1526
  apply (cases z)
haftmann@33320
  1527
  apply (auto simp add: dvd_imp_le)
haftmann@33320
  1528
  done
haftmann@33320
  1529
haftmann@36749
  1530
lemma zdvd_period:
haftmann@36749
  1531
  fixes a d :: int
haftmann@36749
  1532
  assumes "a dvd d"
haftmann@36749
  1533
  shows "a dvd (x + t) \<longleftrightarrow> a dvd ((x + c * d) + t)"
haftmann@36749
  1534
proof -
haftmann@36749
  1535
  from assms obtain k where "d = a * k" by (rule dvdE)
wenzelm@42676
  1536
  show ?thesis
wenzelm@42676
  1537
  proof
haftmann@36749
  1538
    assume "a dvd (x + t)"
haftmann@36749
  1539
    then obtain l where "x + t = a * l" by (rule dvdE)
haftmann@36749
  1540
    then have "x = a * l - t" by simp
haftmann@36749
  1541
    with `d = a * k` show "a dvd x + c * d + t" by simp
haftmann@36749
  1542
  next
haftmann@36749
  1543
    assume "a dvd x + c * d + t"
haftmann@36749
  1544
    then obtain l where "x + c * d + t = a * l" by (rule dvdE)
haftmann@36749
  1545
    then have "x = a * l - c * d - t" by simp
haftmann@36749
  1546
    with `d = a * k` show "a dvd (x + t)" by simp
haftmann@36749
  1547
  qed
haftmann@36749
  1548
qed
haftmann@36749
  1549
haftmann@33320
  1550
bulwahn@46756
  1551
subsection {* Finiteness of intervals *}
bulwahn@46756
  1552
bulwahn@46756
  1553
lemma finite_interval_int1 [iff]: "finite {i :: int. a <= i & i <= b}"
bulwahn@46756
  1554
proof (cases "a <= b")
bulwahn@46756
  1555
  case True
bulwahn@46756
  1556
  from this show ?thesis
bulwahn@46756
  1557
  proof (induct b rule: int_ge_induct)
bulwahn@46756
  1558
    case base
bulwahn@46756
  1559
    have "{i. a <= i & i <= a} = {a}" by auto
bulwahn@46756
  1560
    from this show ?case by simp
bulwahn@46756
  1561
  next
bulwahn@46756
  1562
    case (step b)
bulwahn@46756
  1563
    from this have "{i. a <= i & i <= b + 1} = {i. a <= i & i <= b} \<union> {b + 1}" by auto
bulwahn@46756
  1564
    from this step show ?case by simp
bulwahn@46756
  1565
  qed
bulwahn@46756
  1566
next
bulwahn@46756
  1567
  case False from this show ?thesis
bulwahn@46756
  1568
    by (metis (lifting, no_types) Collect_empty_eq finite.emptyI order_trans)
bulwahn@46756
  1569
qed
bulwahn@46756
  1570
bulwahn@46756
  1571
lemma finite_interval_int2 [iff]: "finite {i :: int. a <= i & i < b}"
bulwahn@46756
  1572
by (rule rev_finite_subset[OF finite_interval_int1[of "a" "b"]]) auto
bulwahn@46756
  1573
bulwahn@46756
  1574
lemma finite_interval_int3 [iff]: "finite {i :: int. a < i & i <= b}"
bulwahn@46756
  1575
by (rule rev_finite_subset[OF finite_interval_int1[of "a" "b"]]) auto
bulwahn@46756
  1576
bulwahn@46756
  1577
lemma finite_interval_int4 [iff]: "finite {i :: int. a < i & i < b}"
bulwahn@46756
  1578
by (rule rev_finite_subset[OF finite_interval_int1[of "a" "b"]]) auto
bulwahn@46756
  1579
bulwahn@46756
  1580
haftmann@25919
  1581
subsection {* Configuration of the code generator *}
haftmann@25919
  1582
huffman@47108
  1583
text {* Constructors *}
huffman@47108
  1584
huffman@47108
  1585
definition Pos :: "num \<Rightarrow> int" where
huffman@47108
  1586
  [simp, code_abbrev]: "Pos = numeral"
huffman@47108
  1587
huffman@47108
  1588
definition Neg :: "num \<Rightarrow> int" where
huffman@47108
  1589
  [simp, code_abbrev]: "Neg = neg_numeral"
huffman@47108
  1590
huffman@47108
  1591
code_datatype "0::int" Pos Neg
huffman@47108
  1592
huffman@47108
  1593
huffman@47108
  1594
text {* Auxiliary operations *}
huffman@47108
  1595
huffman@47108
  1596
definition dup :: "int \<Rightarrow> int" where
huffman@47108
  1597
  [simp]: "dup k = k + k"
haftmann@26507
  1598
huffman@47108
  1599
lemma dup_code [code]:
huffman@47108
  1600
  "dup 0 = 0"
huffman@47108
  1601
  "dup (Pos n) = Pos (Num.Bit0 n)"
huffman@47108
  1602
  "dup (Neg n) = Neg (Num.Bit0 n)"
huffman@47108
  1603
  unfolding Pos_def Neg_def neg_numeral_def
huffman@47108
  1604
  by (simp_all add: numeral_Bit0)
huffman@47108
  1605
huffman@47108
  1606
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
huffman@47108
  1607
  [simp]: "sub m n = numeral m - numeral n"
haftmann@26507
  1608
huffman@47108
  1609
lemma sub_code [code]:
huffman@47108
  1610
  "sub Num.One Num.One = 0"
huffman@47108
  1611
  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
huffman@47108
  1612
  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
huffman@47108
  1613
  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
huffman@47108
  1614
  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
huffman@47108
  1615
  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
huffman@47108
  1616
  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
huffman@47108
  1617
  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
huffman@47108
  1618
  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
huffman@47108
  1619
  unfolding sub_def dup_def numeral.simps Pos_def Neg_def
huffman@47108
  1620
    neg_numeral_def numeral_BitM
huffman@47108
  1621
  by (simp_all only: algebra_simps)
haftmann@26507
  1622
huffman@47108
  1623
huffman@47108
  1624
text {* Implementations *}
huffman@47108
  1625
huffman@47108
  1626
lemma one_int_code [code, code_unfold]:
huffman@47108
  1627
  "1 = Pos Num.One"
huffman@47108
  1628
  by simp
huffman@47108
  1629
huffman@47108
  1630
lemma plus_int_code [code]:
huffman@47108
  1631
  "k + 0 = (k::int)"
huffman@47108
  1632
  "0 + l = (l::int)"
huffman@47108
  1633
  "Pos m + Pos n = Pos (m + n)"
huffman@47108
  1634
  "Pos m + Neg n = sub m n"
huffman@47108
  1635
  "Neg m + Pos n = sub n m"
huffman@47108
  1636
  "Neg m + Neg n = Neg (m + n)"
huffman@47108
  1637
  by simp_all
haftmann@26507
  1638
huffman@47108
  1639
lemma uminus_int_code [code]:
huffman@47108
  1640
  "uminus 0 = (0::int)"
huffman@47108
  1641
  "uminus (Pos m) = Neg m"
huffman@47108
  1642
  "uminus (Neg m) = Pos m"
huffman@47108
  1643
  by simp_all
huffman@47108
  1644
huffman@47108
  1645
lemma minus_int_code [code]:
huffman@47108
  1646
  "k - 0 = (k::int)"
huffman@47108
  1647
  "0 - l = uminus (l::int)"
huffman@47108
  1648
  "Pos m - Pos n = sub m n"
huffman@47108
  1649
  "Pos m - Neg n = Pos (m + n)"
huffman@47108
  1650
  "Neg m - Pos n = Neg (m + n)"
huffman@47108
  1651
  "Neg m - Neg n = sub n m"
huffman@47108
  1652
  by simp_all
huffman@47108
  1653
huffman@47108
  1654
lemma times_int_code [code]:
huffman@47108
  1655
  "k * 0 = (0::int)"
huffman@47108
  1656
  "0 * l = (0::int)"
huffman@47108
  1657
  "Pos m * Pos n = Pos (m * n)"
huffman@47108
  1658
  "Pos m * Neg n = Neg (m * n)"
huffman@47108
  1659
  "Neg m * Pos n = Neg (m * n)"
huffman@47108
  1660
  "Neg m * Neg n = Pos (m * n)"
huffman@47108
  1661
  by simp_all
haftmann@26507
  1662
haftmann@38857
  1663
instantiation int :: equal
haftmann@26507
  1664
begin
haftmann@26507
  1665
haftmann@37767
  1666
definition
huffman@47108
  1667
  "HOL.equal k l \<longleftrightarrow> k = (l::int)"
haftmann@38857
  1668
huffman@47108
  1669
instance by default (rule equal_int_def)
haftmann@26507
  1670
haftmann@26507
  1671
end
haftmann@26507
  1672
huffman@47108
  1673
lemma equal_int_code [code]:
huffman@47108
  1674
  "HOL.equal 0 (0::int) \<longleftrightarrow> True"
huffman@47108
  1675
  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
huffman@47108
  1676
  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
huffman@47108
  1677
  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
huffman@47108
  1678
  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
huffman@47108
  1679
  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
huffman@47108
  1680
  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
huffman@47108
  1681
  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
huffman@47108
  1682
  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
huffman@47108
  1683
  by (auto simp add: equal)
haftmann@26507
  1684
huffman@47108
  1685
lemma equal_int_refl [code nbe]:
haftmann@38857
  1686
  "HOL.equal (k::int) k \<longleftrightarrow> True"
huffman@47108
  1687
  by (fact equal_refl)
haftmann@26507
  1688
haftmann@28562
  1689
lemma less_eq_int_code [code]:
huffman@47108
  1690
  "0 \<le> (0::int) \<longleftrightarrow> True"
huffman@47108
  1691
  "0 \<le> Pos l \<longleftrightarrow> True"
huffman@47108
  1692
  "0 \<le> Neg l \<longleftrightarrow> False"
huffman@47108
  1693
  "Pos k \<le> 0 \<longleftrightarrow> False"
huffman@47108
  1694
  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
huffman@47108
  1695
  "Pos k \<le> Neg l \<longleftrightarrow> False"
huffman@47108
  1696
  "Neg k \<le> 0 \<longleftrightarrow> True"
huffman@47108
  1697
  "Neg k \<le> Pos l \<longleftrightarrow> True"
huffman@47108
  1698
  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
huffman@28958
  1699
  by simp_all
haftmann@26507
  1700
haftmann@28562
  1701
lemma less_int_code [code]:
huffman@47108
  1702
  "0 < (0::int) \<longleftrightarrow> False"
huffman@47108
  1703
  "0 < Pos l \<longleftrightarrow> True"
huffman@47108
  1704
  "0 < Neg l \<longleftrightarrow> False"
huffman@47108
  1705
  "Pos k < 0 \<longleftrightarrow> False"
huffman@47108
  1706
  "Pos k < Pos l \<longleftrightarrow> k < l"
huffman@47108
  1707
  "Pos k < Neg l \<longleftrightarrow> False"
huffman@47108
  1708
  "Neg k < 0 \<longleftrightarrow> True"
huffman@47108
  1709
  "Neg k < Pos l \<longleftrightarrow> True"
huffman@47108
  1710
  "Neg k < Neg l \<longleftrightarrow> l < k"
huffman@28958
  1711
  by simp_all
haftmann@25919
  1712
huffman@47108
  1713
lemma nat_code [code]:
huffman@47108
  1714
  "nat (Int.Neg k) = 0"
huffman@47108
  1715
  "nat 0 = 0"
huffman@47108
  1716
  "nat (Int.Pos k) = nat_of_num k"
huffman@47108
  1717
  by (simp_all add: nat_of_num_numeral nat_numeral)
haftmann@25928
  1718
huffman@47108
  1719
lemma (in ring_1) of_int_code [code]:
huffman@47108
  1720
  "of_int (Int.Neg k) = neg_numeral k"
huffman@47108
  1721
  "of_int 0 = 0"
huffman@47108
  1722
  "of_int (Int.Pos k) = numeral k"
huffman@47108
  1723
  by simp_all
haftmann@25919
  1724
huffman@47108
  1725
huffman@47108
  1726
text {* Serializer setup *}
haftmann@25919
  1727
haftmann@25919
  1728
code_modulename SML
haftmann@33364
  1729
  Int Arith
haftmann@25919
  1730
haftmann@25919
  1731
code_modulename OCaml
haftmann@33364
  1732
  Int Arith
haftmann@25919
  1733
haftmann@25919
  1734
code_modulename Haskell
haftmann@33364
  1735
  Int Arith
haftmann@25919
  1736
haftmann@25919
  1737
quickcheck_params [default_type = int]
haftmann@25919
  1738
huffman@47108
  1739
hide_const (open) Pos Neg sub dup
haftmann@25919
  1740
haftmann@25919
  1741
haftmann@25919
  1742
subsection {* Legacy theorems *}
haftmann@25919
  1743
haftmann@25919
  1744
lemmas inj_int = inj_of_nat [where 'a=int]
haftmann@25919
  1745
lemmas zadd_int = of_nat_add [where 'a=int, symmetric]
haftmann@25919
  1746
lemmas int_mult = of_nat_mult [where 'a=int]
haftmann@25919
  1747
lemmas zmult_int = of_nat_mult [where 'a=int, symmetric]
wenzelm@45607
  1748
lemmas int_eq_0_conv = of_nat_eq_0_iff [where 'a=int and m="n"] for n
haftmann@25919
  1749
lemmas zless_int = of_nat_less_iff [where 'a=int]
wenzelm@45607
  1750
lemmas int_less_0_conv = of_nat_less_0_iff [where 'a=int and m="k"] for k
haftmann@25919
  1751
lemmas zero_less_int_conv = of_nat_0_less_iff [where 'a=int]
haftmann@25919
  1752
lemmas zero_zle_int = of_nat_0_le_iff [where 'a=int]
wenzelm@45607
  1753
lemmas int_le_0_conv = of_nat_le_0_iff [where 'a=int and m="n"] for n
haftmann@25919
  1754
lemmas int_0 = of_nat_0 [where 'a=int]
haftmann@25919
  1755
lemmas int_1 = of_nat_1 [where 'a=int]
haftmann@25919
  1756
lemmas int_Suc = of_nat_Suc [where 'a=int]
huffman@47207
  1757
lemmas int_numeral = of_nat_numeral [where 'a=int]
wenzelm@45607
  1758
lemmas abs_int_eq = abs_of_nat [where 'a=int and n="m"] for m
haftmann@25919
  1759
lemmas of_int_int_eq = of_int_of_nat_eq [where 'a=int]
haftmann@25919
  1760
lemmas zdiff_int = of_nat_diff [where 'a=int, symmetric]
huffman@47255
  1761
lemmas zpower_numeral_even = power_numeral_even [where 'a=int]
huffman@47255
  1762
lemmas zpower_numeral_odd = power_numeral_odd [where 'a=int]
haftmann@30960
  1763
haftmann@31015
  1764
lemma zpower_zpower:
haftmann@31015
  1765
  "(x ^ y) ^ z = (x ^ (y * z)::int)"
haftmann@31015
  1766
  by (rule power_mult [symmetric])
haftmann@31015
  1767
haftmann@31015
  1768
lemma int_power:
haftmann@31015
  1769
  "int (m ^ n) = int m ^ n"
haftmann@31015
  1770
  by (rule of_nat_power)
haftmann@31015
  1771
haftmann@31015
  1772
lemmas zpower_int = int_power [symmetric]
haftmann@31015
  1773
haftmann@25919
  1774
end