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