src/HOL/IntDef.thy
author haftmann
Thu Aug 09 15:52:47 2007 +0200 (2007-08-09)
changeset 24196 f1dbfd7e3223
parent 23950 f54c0e339061
child 24286 7619080e49f0
permissions -rw-r--r--
localized of_nat
wenzelm@23164
     1
(*  Title:      IntDef.thy
wenzelm@23164
     2
    ID:         $Id$
wenzelm@23164
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@23164
     4
    Copyright   1996  University of Cambridge
wenzelm@23164
     5
wenzelm@23164
     6
*)
wenzelm@23164
     7
wenzelm@23164
     8
header{*The Integers as Equivalence Classes over Pairs of Natural Numbers*} 
wenzelm@23164
     9
wenzelm@23164
    10
theory IntDef
wenzelm@23164
    11
imports Equiv_Relations Nat
wenzelm@23164
    12
begin
wenzelm@23164
    13
haftmann@23705
    14
wenzelm@23164
    15
text {* the equivalence relation underlying the integers *}
wenzelm@23164
    16
wenzelm@23164
    17
definition
wenzelm@23164
    18
  intrel :: "((nat \<times> nat) \<times> (nat \<times> nat)) set"
wenzelm@23164
    19
where
wenzelm@23164
    20
  "intrel = {((x, y), (u, v)) | x y u v. x + v = u +y }"
wenzelm@23164
    21
wenzelm@23164
    22
typedef (Integ)
wenzelm@23164
    23
  int = "UNIV//intrel"
wenzelm@23164
    24
  by (auto simp add: quotient_def)
wenzelm@23164
    25
wenzelm@23164
    26
instance int :: zero
huffman@23299
    27
  Zero_int_def: "0 \<equiv> Abs_Integ (intrel `` {(0, 0)})" ..
wenzelm@23164
    28
wenzelm@23164
    29
instance int :: one
huffman@23299
    30
  One_int_def: "1 \<equiv> Abs_Integ (intrel `` {(1, 0)})" ..
wenzelm@23164
    31
wenzelm@23164
    32
instance int :: plus
wenzelm@23164
    33
  add_int_def: "z + w \<equiv> Abs_Integ
wenzelm@23164
    34
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u, v) \<in> Rep_Integ w.
wenzelm@23164
    35
      intrel `` {(x + u, y + v)})" ..
wenzelm@23164
    36
wenzelm@23164
    37
instance int :: minus
wenzelm@23164
    38
  minus_int_def:
wenzelm@23164
    39
    "- z \<equiv> Abs_Integ (\<Union>(x, y) \<in> Rep_Integ z. intrel `` {(y, x)})"
wenzelm@23164
    40
  diff_int_def:  "z - w \<equiv> z + (-w)" ..
wenzelm@23164
    41
wenzelm@23164
    42
instance int :: times
wenzelm@23164
    43
  mult_int_def: "z * w \<equiv>  Abs_Integ
wenzelm@23164
    44
    (\<Union>(x, y) \<in> Rep_Integ z. \<Union>(u,v ) \<in> Rep_Integ w.
wenzelm@23164
    45
      intrel `` {(x*u + y*v, x*v + y*u)})" ..
wenzelm@23164
    46
wenzelm@23164
    47
instance int :: ord
wenzelm@23164
    48
  le_int_def:
wenzelm@23164
    49
   "z \<le> w \<equiv> \<exists>x y u v. x+v \<le> u+y \<and> (x, y) \<in> Rep_Integ z \<and> (u, v) \<in> Rep_Integ w"
wenzelm@23164
    50
  less_int_def: "z < w \<equiv> z \<le> w \<and> z \<noteq> w" ..
wenzelm@23164
    51
wenzelm@23164
    52
lemmas [code func del] = Zero_int_def One_int_def add_int_def
wenzelm@23164
    53
  minus_int_def mult_int_def le_int_def less_int_def
wenzelm@23164
    54
wenzelm@23164
    55
wenzelm@23164
    56
subsection{*Construction of the Integers*}
wenzelm@23164
    57
wenzelm@23164
    58
lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
wenzelm@23164
    59
by (simp add: intrel_def)
wenzelm@23164
    60
wenzelm@23164
    61
lemma equiv_intrel: "equiv UNIV intrel"
wenzelm@23164
    62
by (simp add: intrel_def equiv_def refl_def sym_def trans_def)
wenzelm@23164
    63
wenzelm@23164
    64
text{*Reduces equality of equivalence classes to the @{term intrel} relation:
wenzelm@23164
    65
  @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
wenzelm@23164
    66
lemmas equiv_intrel_iff [simp] = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
wenzelm@23164
    67
wenzelm@23164
    68
text{*All equivalence classes belong to set of representatives*}
wenzelm@23164
    69
lemma [simp]: "intrel``{(x,y)} \<in> Integ"
wenzelm@23164
    70
by (auto simp add: Integ_def intrel_def quotient_def)
wenzelm@23164
    71
wenzelm@23164
    72
text{*Reduces equality on abstractions to equality on representatives:
wenzelm@23164
    73
  @{prop "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
wenzelm@23164
    74
declare Abs_Integ_inject [simp]  Abs_Integ_inverse [simp]
wenzelm@23164
    75
wenzelm@23164
    76
text{*Case analysis on the representation of an integer as an equivalence
wenzelm@23164
    77
      class of pairs of naturals.*}
wenzelm@23164
    78
lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
wenzelm@23164
    79
     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
wenzelm@23164
    80
apply (rule Abs_Integ_cases [of z]) 
wenzelm@23164
    81
apply (auto simp add: Integ_def quotient_def) 
wenzelm@23164
    82
done
wenzelm@23164
    83
wenzelm@23164
    84
huffman@23372
    85
subsection{*Arithmetic Operations*}
wenzelm@23164
    86
wenzelm@23164
    87
lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
wenzelm@23164
    88
proof -
wenzelm@23164
    89
  have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
wenzelm@23164
    90
    by (simp add: congruent_def) 
wenzelm@23164
    91
  thus ?thesis
wenzelm@23164
    92
    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
wenzelm@23164
    93
qed
wenzelm@23164
    94
wenzelm@23164
    95
lemma add:
wenzelm@23164
    96
     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
wenzelm@23164
    97
      Abs_Integ (intrel``{(x+u, y+v)})"
wenzelm@23164
    98
proof -
wenzelm@23164
    99
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z) 
wenzelm@23164
   100
        respects2 intrel"
wenzelm@23164
   101
    by (simp add: congruent2_def)
wenzelm@23164
   102
  thus ?thesis
wenzelm@23164
   103
    by (simp add: add_int_def UN_UN_split_split_eq
wenzelm@23164
   104
                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
wenzelm@23164
   105
qed
wenzelm@23164
   106
wenzelm@23164
   107
text{*Congruence property for multiplication*}
wenzelm@23164
   108
lemma mult_congruent2:
wenzelm@23164
   109
     "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
wenzelm@23164
   110
      respects2 intrel"
wenzelm@23164
   111
apply (rule equiv_intrel [THEN congruent2_commuteI])
wenzelm@23164
   112
 apply (force simp add: mult_ac, clarify) 
wenzelm@23164
   113
apply (simp add: congruent_def mult_ac)  
wenzelm@23164
   114
apply (rename_tac u v w x y z)
wenzelm@23164
   115
apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
wenzelm@23164
   116
apply (simp add: mult_ac)
wenzelm@23164
   117
apply (simp add: add_mult_distrib [symmetric])
wenzelm@23164
   118
done
wenzelm@23164
   119
wenzelm@23164
   120
lemma mult:
wenzelm@23164
   121
     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
wenzelm@23164
   122
      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
wenzelm@23164
   123
by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
wenzelm@23164
   124
              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
wenzelm@23164
   125
wenzelm@23164
   126
text{*The integers form a @{text comm_ring_1}*}
wenzelm@23164
   127
instance int :: comm_ring_1
wenzelm@23164
   128
proof
wenzelm@23164
   129
  fix i j k :: int
huffman@23372
   130
  show "(i + j) + k = i + (j + k)"
huffman@23372
   131
    by (cases i, cases j, cases k) (simp add: add add_assoc)
huffman@23372
   132
  show "i + j = j + i" 
huffman@23372
   133
    by (cases i, cases j) (simp add: add_ac add)
huffman@23372
   134
  show "0 + i = i"
huffman@23372
   135
    by (cases i) (simp add: Zero_int_def add)
huffman@23372
   136
  show "- i + i = 0"
huffman@23372
   137
    by (cases i) (simp add: Zero_int_def minus add)
huffman@23372
   138
  show "i - j = i + - j"
huffman@23372
   139
    by (simp add: diff_int_def)
huffman@23372
   140
  show "(i * j) * k = i * (j * k)"
nipkow@23477
   141
    by (cases i, cases j, cases k) (simp add: mult ring_simps)
huffman@23372
   142
  show "i * j = j * i"
nipkow@23477
   143
    by (cases i, cases j) (simp add: mult ring_simps)
huffman@23372
   144
  show "1 * i = i"
huffman@23372
   145
    by (cases i) (simp add: One_int_def mult)
huffman@23372
   146
  show "(i + j) * k = i * k + j * k"
nipkow@23477
   147
    by (cases i, cases j, cases k) (simp add: add mult ring_simps)
huffman@23372
   148
  show "0 \<noteq> (1::int)"
huffman@23372
   149
    by (simp add: Zero_int_def One_int_def)
wenzelm@23164
   150
qed
wenzelm@23164
   151
haftmann@24196
   152
lemma int_def: "of_nat m = Abs_Integ (intrel `` {(m, 0)})"
huffman@23365
   153
by (induct m, simp_all add: Zero_int_def One_int_def add)
huffman@23303
   154
wenzelm@23164
   155
wenzelm@23164
   156
subsection{*The @{text "\<le>"} Ordering*}
wenzelm@23164
   157
wenzelm@23164
   158
lemma le:
wenzelm@23164
   159
  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
wenzelm@23164
   160
by (force simp add: le_int_def)
wenzelm@23164
   161
huffman@23299
   162
lemma less:
huffman@23299
   163
  "(Abs_Integ(intrel``{(x,y)}) < Abs_Integ(intrel``{(u,v)})) = (x+v < u+y)"
huffman@23299
   164
by (simp add: less_int_def le order_less_le)
huffman@23299
   165
wenzelm@23164
   166
instance int :: linorder
huffman@23372
   167
proof
huffman@23372
   168
  fix i j k :: int
huffman@23372
   169
  show "(i < j) = (i \<le> j \<and> i \<noteq> j)"
huffman@23372
   170
    by (simp add: less_int_def)
huffman@23372
   171
  show "i \<le> i"
huffman@23372
   172
    by (cases i) (simp add: le)
huffman@23372
   173
  show "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> i \<le> k"
huffman@23372
   174
    by (cases i, cases j, cases k) (simp add: le)
huffman@23372
   175
  show "i \<le> j \<Longrightarrow> j \<le> i \<Longrightarrow> i = j"
huffman@23372
   176
    by (cases i, cases j) (simp add: le)
huffman@23372
   177
  show "i \<le> j \<or> j \<le> i"
huffman@23372
   178
    by (cases i, cases j) (simp add: le linorder_linear)
huffman@23372
   179
qed
wenzelm@23164
   180
huffman@23299
   181
instance int :: pordered_cancel_ab_semigroup_add
huffman@23299
   182
proof
huffman@23372
   183
  fix i j k :: int
huffman@23372
   184
  show "i \<le> j \<Longrightarrow> k + i \<le> k + j"
huffman@23372
   185
    by (cases i, cases j, cases k) (simp add: le add)
huffman@23299
   186
qed
huffman@23299
   187
huffman@23372
   188
text{*Strict Monotonicity of Multiplication*}
wenzelm@23164
   189
wenzelm@23164
   190
text{*strict, in 1st argument; proof is by induction on k>0*}
wenzelm@23164
   191
lemma zmult_zless_mono2_lemma:
haftmann@24196
   192
     "(i::int)<j ==> 0<k ==> of_nat k * i < of_nat k * j"
wenzelm@23164
   193
apply (induct "k", simp)
huffman@23299
   194
apply (simp add: left_distrib)
wenzelm@23164
   195
apply (case_tac "k=0")
huffman@23299
   196
apply (simp_all add: add_strict_mono)
wenzelm@23164
   197
done
wenzelm@23164
   198
haftmann@24196
   199
lemma zero_le_imp_eq_int: "(0::int) \<le> k ==> \<exists>n. k = of_nat n"
wenzelm@23164
   200
apply (cases k)
huffman@23365
   201
apply (auto simp add: le add int_def Zero_int_def)
huffman@23299
   202
apply (rule_tac x="x-y" in exI, simp)
huffman@23299
   203
done
huffman@23299
   204
haftmann@24196
   205
lemma zero_less_imp_eq_int: "(0::int) < k ==> \<exists>n>0. k = of_nat n"
huffman@23299
   206
apply (cases k)
huffman@23365
   207
apply (simp add: less int_def Zero_int_def)
wenzelm@23164
   208
apply (rule_tac x="x-y" in exI, simp)
wenzelm@23164
   209
done
wenzelm@23164
   210
wenzelm@23164
   211
lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
huffman@23299
   212
apply (drule zero_less_imp_eq_int)
wenzelm@23164
   213
apply (auto simp add: zmult_zless_mono2_lemma)
wenzelm@23164
   214
done
wenzelm@23164
   215
haftmann@23879
   216
instance int :: abs
wenzelm@23164
   217
  zabs_def: "\<bar>i\<Colon>int\<bar> \<equiv> if i < 0 then - i else i" ..
wenzelm@23164
   218
wenzelm@23164
   219
instance int :: distrib_lattice
wenzelm@23164
   220
  "inf \<equiv> min"
wenzelm@23164
   221
  "sup \<equiv> max"
wenzelm@23164
   222
  by intro_classes
wenzelm@23164
   223
    (auto simp add: inf_int_def sup_int_def min_max.sup_inf_distrib1)
wenzelm@23164
   224
huffman@23299
   225
text{*The integers form an ordered integral domain*}
wenzelm@23164
   226
instance int :: ordered_idom
wenzelm@23164
   227
proof
wenzelm@23164
   228
  fix i j k :: int
huffman@23372
   229
  show "i < j \<Longrightarrow> 0 < k \<Longrightarrow> k * i < k * j"
huffman@23372
   230
    by (rule zmult_zless_mono2)
huffman@23372
   231
  show "\<bar>i\<bar> = (if i < 0 then -i else i)"
huffman@23372
   232
    by (simp only: zabs_def)
wenzelm@23164
   233
qed
wenzelm@23164
   234
wenzelm@23164
   235
lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
wenzelm@23164
   236
apply (cases w, cases z) 
huffman@23299
   237
apply (simp add: less le add One_int_def)
wenzelm@23164
   238
done
wenzelm@23164
   239
huffman@23299
   240
huffman@23299
   241
subsection{*Magnitude of an Integer, as a Natural Number: @{term nat}*}
wenzelm@23164
   242
wenzelm@23164
   243
definition
wenzelm@23164
   244
  nat :: "int \<Rightarrow> nat"
wenzelm@23164
   245
where
wenzelm@23164
   246
  [code func del]: "nat z = contents (\<Union>(x, y) \<in> Rep_Integ z. {x-y})"
wenzelm@23164
   247
wenzelm@23164
   248
lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
wenzelm@23164
   249
proof -
wenzelm@23164
   250
  have "(\<lambda>(x,y). {x-y}) respects intrel"
wenzelm@23164
   251
    by (simp add: congruent_def) arith
wenzelm@23164
   252
  thus ?thesis
wenzelm@23164
   253
    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
wenzelm@23164
   254
qed
wenzelm@23164
   255
haftmann@24196
   256
lemma nat_int [simp]: "nat (of_nat n) = n"
huffman@23365
   257
by (simp add: nat int_def)
wenzelm@23164
   258
wenzelm@23164
   259
lemma nat_zero [simp]: "nat 0 = 0"
huffman@23303
   260
by (simp add: Zero_int_def nat)
wenzelm@23164
   261
haftmann@24196
   262
lemma int_nat_eq [simp]: "of_nat (nat z) = (if 0 \<le> z then z else 0)"
huffman@23365
   263
by (cases z, simp add: nat le int_def Zero_int_def)
wenzelm@23164
   264
haftmann@24196
   265
corollary nat_0_le: "0 \<le> z ==> of_nat (nat z) = z"
wenzelm@23164
   266
by simp
wenzelm@23164
   267
wenzelm@23164
   268
lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
huffman@23303
   269
by (cases z, simp add: nat le Zero_int_def)
wenzelm@23164
   270
wenzelm@23164
   271
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
wenzelm@23164
   272
apply (cases w, cases z) 
huffman@23303
   273
apply (simp add: nat le linorder_not_le [symmetric] Zero_int_def, arith)
wenzelm@23164
   274
done
wenzelm@23164
   275
wenzelm@23164
   276
text{*An alternative condition is @{term "0 \<le> w"} *}
wenzelm@23164
   277
corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
wenzelm@23164
   278
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
wenzelm@23164
   279
wenzelm@23164
   280
corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
wenzelm@23164
   281
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
wenzelm@23164
   282
huffman@23365
   283
lemma zless_nat_conj [simp]: "(nat w < nat z) = (0 < z & w < z)"
wenzelm@23164
   284
apply (cases w, cases z) 
huffman@23303
   285
apply (simp add: nat le Zero_int_def linorder_not_le [symmetric], arith)
wenzelm@23164
   286
done
wenzelm@23164
   287
haftmann@24196
   288
lemma nonneg_eq_int:
haftmann@24196
   289
  fixes z :: int
haftmann@24196
   290
  assumes "0 \<le> z" and "\<And>m. z = of_nat m \<Longrightarrow> P"
haftmann@24196
   291
  shows P
haftmann@24196
   292
  using assms by (blast dest: nat_0_le sym)
wenzelm@23164
   293
haftmann@24196
   294
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = of_nat m else m=0)"
huffman@23365
   295
by (cases w, simp add: nat le int_def Zero_int_def, arith)
wenzelm@23164
   296
haftmann@24196
   297
corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = of_nat m else m=0)"
huffman@23365
   298
by (simp only: eq_commute [of m] nat_eq_iff)
wenzelm@23164
   299
haftmann@24196
   300
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < of_nat m)"
wenzelm@23164
   301
apply (cases w)
huffman@23365
   302
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
wenzelm@23164
   303
done
wenzelm@23164
   304
haftmann@24196
   305
lemma int_eq_iff: "(of_nat m = z) = (m = nat z & 0 \<le> z)"
huffman@23365
   306
by (auto simp add: nat_eq_iff2)
wenzelm@23164
   307
wenzelm@23164
   308
lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
wenzelm@23164
   309
by (insert zless_nat_conj [of 0], auto)
wenzelm@23164
   310
wenzelm@23164
   311
lemma nat_add_distrib:
wenzelm@23164
   312
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
huffman@23303
   313
by (cases z, cases z', simp add: nat add le Zero_int_def)
wenzelm@23164
   314
wenzelm@23164
   315
lemma nat_diff_distrib:
wenzelm@23164
   316
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
wenzelm@23164
   317
by (cases z, cases z', 
huffman@23303
   318
    simp add: nat add minus diff_minus le Zero_int_def)
wenzelm@23164
   319
haftmann@24196
   320
lemma nat_zminus_int [simp]: "nat (- (of_nat n)) = 0"
huffman@23365
   321
by (simp add: int_def minus nat Zero_int_def) 
wenzelm@23164
   322
haftmann@24196
   323
lemma zless_nat_eq_int_zless: "(m < nat z) = (of_nat m < z)"
huffman@23365
   324
by (cases z, simp add: nat less int_def, arith)
wenzelm@23164
   325
wenzelm@23164
   326
haftmann@24196
   327
subsection{*Lemmas about the Function @{term of_nat} and Orderings*}
wenzelm@23164
   328
haftmann@24196
   329
lemma negative_zless_0: "- (of_nat (Suc n)) < (0 \<Colon> int)"
huffman@23303
   330
by (simp add: order_less_le del: of_nat_Suc)
wenzelm@23164
   331
haftmann@24196
   332
lemma negative_zless [iff]: "- (of_nat (Suc n)) < (of_nat m \<Colon> int)"
huffman@23365
   333
by (rule negative_zless_0 [THEN order_less_le_trans], simp)
wenzelm@23164
   334
haftmann@24196
   335
lemma negative_zle_0: "- of_nat n \<le> (0 \<Colon> int)"
wenzelm@23164
   336
by (simp add: minus_le_iff)
wenzelm@23164
   337
haftmann@24196
   338
lemma negative_zle [iff]: "- of_nat n \<le> (of_nat m \<Colon> int)"
huffman@23365
   339
by (rule order_trans [OF negative_zle_0 of_nat_0_le_iff])
wenzelm@23164
   340
haftmann@24196
   341
lemma not_zle_0_negative [simp]: "~ (0 \<le> - (of_nat (Suc n) \<Colon> int))"
huffman@23303
   342
by (subst le_minus_iff, simp del: of_nat_Suc)
wenzelm@23164
   343
haftmann@24196
   344
lemma int_zle_neg: "((of_nat n \<Colon> int) \<le> - of_nat m) = (n = 0 & m = 0)"
huffman@23365
   345
by (simp add: int_def le minus Zero_int_def)
wenzelm@23164
   346
haftmann@24196
   347
lemma not_int_zless_negative [simp]: "~ ((of_nat n \<Colon> int) < - of_nat m)"
wenzelm@23164
   348
by (simp add: linorder_not_less)
wenzelm@23164
   349
haftmann@24196
   350
lemma negative_eq_positive [simp]: "((- of_nat n \<Colon> int) = of_nat m) = (n = 0 & m = 0)"
haftmann@24196
   351
by (force simp add: order_eq_iff [of "- of_nat n"] int_zle_neg)
wenzelm@23164
   352
haftmann@24196
   353
lemma zle_iff_zadd: "(w\<Colon>int) \<le> z \<longleftrightarrow> (\<exists>n. z = w + of_nat n)"
huffman@23372
   354
proof -
huffman@23372
   355
  have "(w \<le> z) = (0 \<le> z - w)"
huffman@23372
   356
    by (simp only: le_diff_eq add_0_left)
haftmann@24196
   357
  also have "\<dots> = (\<exists>n. z - w = of_nat n)"
huffman@23372
   358
    by (auto elim: zero_le_imp_eq_int)
haftmann@24196
   359
  also have "\<dots> = (\<exists>n. z = w + of_nat n)"
nipkow@23477
   360
    by (simp only: group_simps)
huffman@23372
   361
  finally show ?thesis .
wenzelm@23164
   362
qed
wenzelm@23164
   363
haftmann@24196
   364
lemma zadd_int_left: "of_nat m + (of_nat n + z) = of_nat (m + n) + (z\<Colon>int)"
huffman@23372
   365
by simp
huffman@23372
   366
haftmann@24196
   367
lemma int_Suc0_eq_1: "of_nat (Suc 0) = (1\<Colon>int)"
huffman@23372
   368
by simp
huffman@23372
   369
wenzelm@23164
   370
text{*This version is proved for all ordered rings, not just integers!
wenzelm@23164
   371
      It is proved here because attribute @{text arith_split} is not available
wenzelm@23164
   372
      in theory @{text Ring_and_Field}.
wenzelm@23164
   373
      But is it really better than just rewriting with @{text abs_if}?*}
wenzelm@23164
   374
lemma abs_split [arith_split]:
wenzelm@23164
   375
     "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
wenzelm@23164
   376
by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
wenzelm@23164
   377
wenzelm@23164
   378
wenzelm@23164
   379
subsection {* Constants @{term neg} and @{term iszero} *}
wenzelm@23164
   380
wenzelm@23164
   381
definition
wenzelm@23164
   382
  neg  :: "'a\<Colon>ordered_idom \<Rightarrow> bool"
wenzelm@23164
   383
where
wenzelm@23164
   384
  [code inline]: "neg Z \<longleftrightarrow> Z < 0"
wenzelm@23164
   385
wenzelm@23164
   386
definition (*for simplifying equalities*)
huffman@23276
   387
  iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool"
wenzelm@23164
   388
where
wenzelm@23164
   389
  "iszero z \<longleftrightarrow> z = 0"
wenzelm@23164
   390
haftmann@24196
   391
lemma not_neg_int [simp]: "~ neg (of_nat n)"
wenzelm@23164
   392
by (simp add: neg_def)
wenzelm@23164
   393
haftmann@24196
   394
lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
huffman@23303
   395
by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
wenzelm@23164
   396
wenzelm@23164
   397
lemmas neg_eq_less_0 = neg_def
wenzelm@23164
   398
wenzelm@23164
   399
lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
wenzelm@23164
   400
by (simp add: neg_def linorder_not_less)
wenzelm@23164
   401
wenzelm@23164
   402
huffman@23372
   403
text{*To simplify inequalities when Numeral1 can get simplified to 1*}
wenzelm@23164
   404
wenzelm@23164
   405
lemma not_neg_0: "~ neg 0"
wenzelm@23164
   406
by (simp add: One_int_def neg_def)
wenzelm@23164
   407
wenzelm@23164
   408
lemma not_neg_1: "~ neg 1"
wenzelm@23164
   409
by (simp add: neg_def linorder_not_less zero_le_one)
wenzelm@23164
   410
wenzelm@23164
   411
lemma iszero_0: "iszero 0"
wenzelm@23164
   412
by (simp add: iszero_def)
wenzelm@23164
   413
wenzelm@23164
   414
lemma not_iszero_1: "~ iszero 1"
wenzelm@23164
   415
by (simp add: iszero_def eq_commute)
wenzelm@23164
   416
wenzelm@23164
   417
lemma neg_nat: "neg z ==> nat z = 0"
wenzelm@23164
   418
by (simp add: neg_def order_less_imp_le) 
wenzelm@23164
   419
haftmann@24196
   420
lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
wenzelm@23164
   421
by (simp add: linorder_not_less neg_def)
wenzelm@23164
   422
wenzelm@23164
   423
haftmann@23852
   424
subsection{*Embedding of the Integers into any @{text ring_1}: @{term of_int}*}
wenzelm@23164
   425
haftmann@23950
   426
definition
haftmann@23950
   427
  of_int :: "int \<Rightarrow> 'a\<Colon>ring_1"
haftmann@23950
   428
where
haftmann@23950
   429
  "of_int z = contents (\<Union>(i, j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
haftmann@23852
   430
lemmas [code func del] = of_int_def
wenzelm@23164
   431
wenzelm@23164
   432
lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
wenzelm@23164
   433
proof -
wenzelm@23164
   434
  have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
wenzelm@23164
   435
    by (simp add: congruent_def compare_rls of_nat_add [symmetric]
wenzelm@23164
   436
            del: of_nat_add) 
wenzelm@23164
   437
  thus ?thesis
wenzelm@23164
   438
    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
wenzelm@23164
   439
qed
wenzelm@23164
   440
wenzelm@23164
   441
lemma of_int_0 [simp]: "of_int 0 = 0"
huffman@23303
   442
by (simp add: of_int Zero_int_def)
wenzelm@23164
   443
wenzelm@23164
   444
lemma of_int_1 [simp]: "of_int 1 = 1"
huffman@23303
   445
by (simp add: of_int One_int_def)
wenzelm@23164
   446
wenzelm@23164
   447
lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
wenzelm@23164
   448
by (cases w, cases z, simp add: compare_rls of_int add)
wenzelm@23164
   449
wenzelm@23164
   450
lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
wenzelm@23164
   451
by (cases z, simp add: compare_rls of_int minus)
wenzelm@23164
   452
wenzelm@23164
   453
lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z"
wenzelm@23164
   454
by (simp add: diff_minus)
wenzelm@23164
   455
wenzelm@23164
   456
lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
wenzelm@23164
   457
apply (cases w, cases z)
wenzelm@23164
   458
apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
huffman@23431
   459
                 mult add_ac of_nat_mult)
wenzelm@23164
   460
done
wenzelm@23164
   461
wenzelm@23164
   462
lemma of_int_le_iff [simp]:
wenzelm@23164
   463
     "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
wenzelm@23164
   464
apply (cases w)
wenzelm@23164
   465
apply (cases z)
wenzelm@23164
   466
apply (simp add: compare_rls of_int le diff_int_def add minus
wenzelm@23164
   467
                 of_nat_add [symmetric]   del: of_nat_add)
wenzelm@23164
   468
done
wenzelm@23164
   469
wenzelm@23164
   470
text{*Special cases where either operand is zero*}
wenzelm@23164
   471
lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified]
wenzelm@23164
   472
lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified]
wenzelm@23164
   473
wenzelm@23164
   474
wenzelm@23164
   475
lemma of_int_less_iff [simp]:
wenzelm@23164
   476
     "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
wenzelm@23164
   477
by (simp add: linorder_not_le [symmetric])
wenzelm@23164
   478
wenzelm@23164
   479
text{*Special cases where either operand is zero*}
wenzelm@23164
   480
lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified]
wenzelm@23164
   481
lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified]
wenzelm@23164
   482
wenzelm@23164
   483
text{*Class for unital rings with characteristic zero.
wenzelm@23164
   484
 Includes non-ordered rings like the complex numbers.*}
haftmann@23950
   485
class ring_char_0 = ring_1 + semiring_char_0
wenzelm@23164
   486
wenzelm@23164
   487
lemma of_int_eq_iff [simp]:
haftmann@24196
   488
   "of_int w = (of_int z \<Colon> 'a\<Colon>ring_char_0) \<longleftrightarrow> w = z"
huffman@23282
   489
apply (cases w, cases z, simp add: of_int)
huffman@23282
   490
apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq)
huffman@23282
   491
apply (simp only: of_nat_add [symmetric] of_nat_eq_iff)
huffman@23282
   492
done
wenzelm@23164
   493
wenzelm@23164
   494
text{*Every @{text ordered_idom} has characteristic zero.*}
huffman@23282
   495
instance ordered_idom < ring_char_0 ..
wenzelm@23164
   496
wenzelm@23164
   497
text{*Special cases where either operand is zero*}
wenzelm@23164
   498
lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified]
wenzelm@23164
   499
lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified]
wenzelm@23164
   500
wenzelm@23164
   501
lemma of_int_eq_id [simp]: "of_int = (id :: int => int)"
wenzelm@23164
   502
proof
wenzelm@23164
   503
  fix z
huffman@23299
   504
  show "of_int z = id z"
wenzelm@23164
   505
    by (cases z)
huffman@23365
   506
      (simp add: of_int add minus int_def diff_minus)
wenzelm@23164
   507
qed
wenzelm@23164
   508
huffman@23372
   509
lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z"
huffman@23438
   510
by (cases z rule: eq_Abs_Integ)
huffman@23438
   511
   (simp add: nat le of_int Zero_int_def of_nat_diff)
huffman@23372
   512
wenzelm@23164
   513
wenzelm@23164
   514
subsection{*The Set of Integers*}
wenzelm@23164
   515
wenzelm@23164
   516
constdefs
wenzelm@23164
   517
  Ints  :: "'a::ring_1 set"
wenzelm@23164
   518
  "Ints == range of_int"
wenzelm@23164
   519
wenzelm@23164
   520
notation (xsymbols)
wenzelm@23164
   521
  Ints  ("\<int>")
wenzelm@23164
   522
wenzelm@23164
   523
lemma Ints_0 [simp]: "0 \<in> Ints"
wenzelm@23164
   524
apply (simp add: Ints_def)
wenzelm@23164
   525
apply (rule range_eqI)
wenzelm@23164
   526
apply (rule of_int_0 [symmetric])
wenzelm@23164
   527
done
wenzelm@23164
   528
wenzelm@23164
   529
lemma Ints_1 [simp]: "1 \<in> Ints"
wenzelm@23164
   530
apply (simp add: Ints_def)
wenzelm@23164
   531
apply (rule range_eqI)
wenzelm@23164
   532
apply (rule of_int_1 [symmetric])
wenzelm@23164
   533
done
wenzelm@23164
   534
wenzelm@23164
   535
lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints"
wenzelm@23164
   536
apply (auto simp add: Ints_def)
wenzelm@23164
   537
apply (rule range_eqI)
wenzelm@23164
   538
apply (rule of_int_add [symmetric])
wenzelm@23164
   539
done
wenzelm@23164
   540
wenzelm@23164
   541
lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints"
wenzelm@23164
   542
apply (auto simp add: Ints_def)
wenzelm@23164
   543
apply (rule range_eqI)
wenzelm@23164
   544
apply (rule of_int_minus [symmetric])
wenzelm@23164
   545
done
wenzelm@23164
   546
wenzelm@23164
   547
lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints"
wenzelm@23164
   548
apply (auto simp add: Ints_def)
wenzelm@23164
   549
apply (rule range_eqI)
wenzelm@23164
   550
apply (rule of_int_diff [symmetric])
wenzelm@23164
   551
done
wenzelm@23164
   552
wenzelm@23164
   553
lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints"
wenzelm@23164
   554
apply (auto simp add: Ints_def)
wenzelm@23164
   555
apply (rule range_eqI)
wenzelm@23164
   556
apply (rule of_int_mult [symmetric])
wenzelm@23164
   557
done
wenzelm@23164
   558
wenzelm@23164
   559
text{*Collapse nested embeddings*}
wenzelm@23164
   560
lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
wenzelm@23164
   561
by (induct n, auto)
wenzelm@23164
   562
wenzelm@23164
   563
lemma Ints_cases [cases set: Ints]:
wenzelm@23164
   564
  assumes "q \<in> \<int>"
wenzelm@23164
   565
  obtains (of_int) z where "q = of_int z"
wenzelm@23164
   566
  unfolding Ints_def
wenzelm@23164
   567
proof -
wenzelm@23164
   568
  from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def .
wenzelm@23164
   569
  then obtain z where "q = of_int z" ..
wenzelm@23164
   570
  then show thesis ..
wenzelm@23164
   571
qed
wenzelm@23164
   572
wenzelm@23164
   573
lemma Ints_induct [case_names of_int, induct set: Ints]:
wenzelm@23164
   574
  "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q"
wenzelm@23164
   575
  by (rule Ints_cases) auto
wenzelm@23164
   576
wenzelm@23164
   577
wenzelm@23164
   578
subsection {* Further properties *}
wenzelm@23164
   579
wenzelm@23164
   580
text{*Now we replace the case analysis rule by a more conventional one:
wenzelm@23164
   581
whether an integer is negative or not.*}
wenzelm@23164
   582
huffman@23365
   583
lemma zless_iff_Suc_zadd:
haftmann@24196
   584
  "(w \<Colon> int) < z \<longleftrightarrow> (\<exists>n. z = w + of_nat (Suc n))"
huffman@23303
   585
apply (cases z, cases w)
huffman@23372
   586
apply (auto simp add: less add int_def)
huffman@23303
   587
apply (rename_tac a b c d) 
huffman@23303
   588
apply (rule_tac x="a+d - Suc(c+b)" in exI) 
huffman@23303
   589
apply arith
huffman@23303
   590
done
huffman@23303
   591
haftmann@24196
   592
lemma negD: "(x \<Colon> int) < 0 \<Longrightarrow> \<exists>n. x = - (of_nat (Suc n))"
huffman@23303
   593
apply (cases x)
huffman@23365
   594
apply (auto simp add: le minus Zero_int_def int_def order_less_le)
huffman@23303
   595
apply (rule_tac x="y - Suc x" in exI, arith)
huffman@23303
   596
done
huffman@23303
   597
huffman@23365
   598
theorem int_cases [cases type: int, case_names nonneg neg]:
haftmann@24196
   599
  "[|!! n. (z \<Colon> int) = of_nat n ==> P;  !! n. z =  - (of_nat (Suc n)) ==> P |] ==> P"
huffman@23365
   600
apply (cases "z < 0", blast dest!: negD)
huffman@23303
   601
apply (simp add: linorder_not_less del: of_nat_Suc)
huffman@23365
   602
apply (blast dest: nat_0_le [THEN sym])
huffman@23303
   603
done
huffman@23303
   604
huffman@23372
   605
theorem int_induct [induct type: int, case_names nonneg neg]:
haftmann@24196
   606
     "[|!! n. P (of_nat n \<Colon> int);  !!n. P (- (of_nat (Suc n))) |] ==> P z"
huffman@23365
   607
  by (cases z rule: int_cases) auto
huffman@23303
   608
huffman@23303
   609
text{*Contributed by Brian Huffman*}
huffman@23365
   610
theorem int_diff_cases [case_names diff]:
haftmann@24196
   611
assumes prem: "!!m n. (z\<Colon>int) = of_nat m - of_nat n ==> P" shows "P"
huffman@23303
   612
apply (cases z rule: eq_Abs_Integ)
huffman@23303
   613
apply (rule_tac m=x and n=y in prem)
huffman@23365
   614
apply (simp add: int_def diff_def minus add)
huffman@23303
   615
done
huffman@23303
   616
huffman@23303
   617
huffman@23365
   618
subsection {* Legacy theorems *}
huffman@23303
   619
huffman@23431
   620
lemmas zminus_zminus = minus_minus [of "?z::int"]
huffman@23372
   621
lemmas zminus_0 = minus_zero [where 'a=int]
huffman@23431
   622
lemmas zminus_zadd_distrib = minus_add_distrib [of "?z::int" "?w"]
huffman@23431
   623
lemmas zadd_commute = add_commute [of "?z::int" "?w"]
huffman@23431
   624
lemmas zadd_assoc = add_assoc [of "?z1.0::int" "?z2.0" "?z3.0"]
huffman@23431
   625
lemmas zadd_left_commute = add_left_commute [of "?x::int" "?y" "?z"]
huffman@23372
   626
lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
huffman@23372
   627
lemmas zmult_ac = OrderedGroup.mult_ac
huffman@23431
   628
lemmas zadd_0 = OrderedGroup.add_0_left [of "?z::int"]
huffman@23431
   629
lemmas zadd_0_right = OrderedGroup.add_0_left [of "?z::int"]
huffman@23431
   630
lemmas zadd_zminus_inverse2 = left_minus [of "?z::int"]
huffman@23431
   631
lemmas zmult_zminus = mult_minus_left [of "?z::int" "?w"]
huffman@23431
   632
lemmas zmult_commute = mult_commute [of "?z::int" "?w"]
huffman@23431
   633
lemmas zmult_assoc = mult_assoc [of "?z1.0::int" "?z2.0" "?z3.0"]
huffman@23431
   634
lemmas zadd_zmult_distrib = left_distrib [of "?z1.0::int" "?z2.0" "?w"]
huffman@23431
   635
lemmas zadd_zmult_distrib2 = right_distrib [of "?w::int" "?z1.0" "?z2.0"]
huffman@23431
   636
lemmas zdiff_zmult_distrib = left_diff_distrib [of "?z1.0::int" "?z2.0" "?w"]
huffman@23431
   637
lemmas zdiff_zmult_distrib2 = right_diff_distrib [of "?w::int" "?z1.0" "?z2.0"]
huffman@23303
   638
huffman@23372
   639
lemmas int_distrib =
huffman@23372
   640
  zadd_zmult_distrib zadd_zmult_distrib2
huffman@23372
   641
  zdiff_zmult_distrib zdiff_zmult_distrib2
huffman@23372
   642
huffman@23431
   643
lemmas zmult_1 = mult_1_left [of "?z::int"]
huffman@23431
   644
lemmas zmult_1_right = mult_1_right [of "?z::int"]
huffman@23303
   645
huffman@23431
   646
lemmas zle_refl = order_refl [of "?w::int"]
nipkow@23402
   647
lemmas zle_trans = order_trans [where 'a=int and x="?i" and y="?j" and z="?k"]
huffman@23431
   648
lemmas zle_anti_sym = order_antisym [of "?z::int" "?w"]
huffman@23431
   649
lemmas zle_linear = linorder_linear [of "?z::int" "?w"]
huffman@23372
   650
lemmas zless_linear = linorder_less_linear [where 'a = int]
huffman@23372
   651
huffman@23431
   652
lemmas zadd_left_mono = add_left_mono [of "?i::int" "?j" "?k"]
huffman@23431
   653
lemmas zadd_strict_right_mono = add_strict_right_mono [of "?i::int" "?j" "?k"]
huffman@23431
   654
lemmas zadd_zless_mono = add_less_le_mono [of "?w'::int" "?w" "?z'" "?z"]
huffman@23372
   655
huffman@23372
   656
lemmas int_0_less_1 = zero_less_one [where 'a=int]
huffman@23372
   657
lemmas int_0_neq_1 = zero_neq_one [where 'a=int]
huffman@23303
   658
huffman@23365
   659
lemmas inj_int = inj_of_nat [where 'a=int]
huffman@23365
   660
lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
huffman@23365
   661
lemmas zadd_int = of_nat_add [where 'a=int, symmetric]
huffman@23365
   662
lemmas int_mult = of_nat_mult [where 'a=int]
huffman@23365
   663
lemmas zmult_int = of_nat_mult [where 'a=int, symmetric]
huffman@23431
   664
lemmas int_eq_0_conv = of_nat_eq_0_iff [where 'a=int and m="?n"]
huffman@23365
   665
lemmas zless_int = of_nat_less_iff [where 'a=int]
huffman@23431
   666
lemmas int_less_0_conv = of_nat_less_0_iff [where 'a=int and m="?k"]
huffman@23365
   667
lemmas zero_less_int_conv = of_nat_0_less_iff [where 'a=int]
huffman@23365
   668
lemmas zle_int = of_nat_le_iff [where 'a=int]
huffman@23365
   669
lemmas zero_zle_int = of_nat_0_le_iff [where 'a=int]
huffman@23431
   670
lemmas int_le_0_conv = of_nat_le_0_iff [where 'a=int and m="?n"]
haftmann@24196
   671
lemmas int_0 = of_nat_0 [where 'a=int]
huffman@23365
   672
lemmas int_1 = of_nat_1 [where 'a=int]
haftmann@24196
   673
lemmas int_Suc = of_nat_Suc [where 'a=int]
huffman@23431
   674
lemmas abs_int_eq = abs_of_nat [where 'a=int and n="?m"]
huffman@23365
   675
lemmas of_int_int_eq = of_int_of_nat_eq [where 'a=int]
huffman@23365
   676
lemmas zdiff_int = of_nat_diff [where 'a=int, symmetric]
huffman@23365
   677
lemmas zless_le = less_int_def [THEN meta_eq_to_obj_eq]
huffman@23365
   678
lemmas int_eq_of_nat = TrueI
wenzelm@23164
   679
huffman@23365
   680
abbreviation
haftmann@24196
   681
  int :: "nat \<Rightarrow> int"
haftmann@24196
   682
where
haftmann@24196
   683
  "int \<equiv> of_nat"
haftmann@24196
   684
haftmann@24196
   685
abbreviation
huffman@23365
   686
  int_of_nat :: "nat \<Rightarrow> int"
huffman@23365
   687
where
huffman@23365
   688
  "int_of_nat \<equiv> of_nat"
wenzelm@23164
   689
wenzelm@23164
   690
end