src/HOL/Integ/IntDef.thy
author nipkow
Wed Aug 18 11:09:40 2004 +0200 (2004-08-18)
changeset 15140 322485b816ac
parent 15131 c69542757a4d
child 15169 2b5da07a0b89
permissions -rw-r--r--
import -> imports
paulson@5508
     1
(*  Title:      IntDef.thy
paulson@5508
     2
    ID:         $Id$
paulson@5508
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@5508
     4
    Copyright   1996  University of Cambridge
paulson@5508
     5
paulson@5508
     6
*)
paulson@5508
     7
paulson@14271
     8
header{*The Integers as Equivalence Classes over Pairs of Natural Numbers*}
paulson@14271
     9
nipkow@15131
    10
theory IntDef
nipkow@15140
    11
imports Equiv NatArith
nipkow@15131
    12
begin
paulson@14479
    13
paulson@5508
    14
constdefs
paulson@14271
    15
  intrel :: "((nat * nat) * (nat * nat)) set"
paulson@14479
    16
    --{*the equivalence relation underlying the integers*}
paulson@14496
    17
    "intrel == {((x,y),(u,v)) | x y u v. x+v = u+y}"
paulson@5508
    18
paulson@5508
    19
typedef (Integ)
paulson@14259
    20
  int = "UNIV//intrel"
paulson@14479
    21
    by (auto simp add: quotient_def)
paulson@5508
    22
wenzelm@14691
    23
instance int :: "{ord, zero, one, plus, times, minus}" ..
paulson@5508
    24
paulson@5508
    25
constdefs
paulson@14259
    26
  int :: "nat => int"
nipkow@10834
    27
  "int m == Abs_Integ(intrel `` {(m,0)})"
paulson@14479
    28
paulson@14479
    29
paulson@14269
    30
defs (overloaded)
paulson@14271
    31
paulson@14259
    32
  Zero_int_def:  "0 == int 0"
paulson@14271
    33
  One_int_def:   "1 == int 1"
paulson@8937
    34
paulson@14479
    35
  minus_int_def:
paulson@14532
    36
    "- z == Abs_Integ (\<Union>(x,y) \<in> Rep_Integ z. intrel``{(y,x)})"
paulson@14479
    37
paulson@14479
    38
  add_int_def:
paulson@14479
    39
   "z + w ==
paulson@14532
    40
       Abs_Integ (\<Union>(x,y) \<in> Rep_Integ z. \<Union>(u,v) \<in> Rep_Integ w.
paulson@14532
    41
		 intrel``{(x+u, y+v)})"
paulson@14479
    42
paulson@14479
    43
  diff_int_def:  "z - (w::int) == z + (-w)"
paulson@5508
    44
paulson@14479
    45
  mult_int_def:
paulson@14479
    46
   "z * w ==
paulson@14532
    47
       Abs_Integ (\<Union>(x,y) \<in> Rep_Integ z. \<Union>(u,v) \<in> Rep_Integ w.
paulson@14532
    48
		  intrel``{(x*u + y*v, x*v + y*u)})"
paulson@14479
    49
paulson@14479
    50
  le_int_def:
paulson@14479
    51
   "z \<le> (w::int) == 
paulson@14479
    52
    \<exists>x y u v. x+v \<le> u+y & (x,y) \<in> Rep_Integ z & (u,v) \<in> Rep_Integ w"
paulson@5508
    53
paulson@14479
    54
  less_int_def: "(z < (w::int)) == (z \<le> w & z \<noteq> w)"
paulson@14479
    55
paulson@14479
    56
paulson@14479
    57
subsection{*Construction of the Integers*}
paulson@14378
    58
paulson@14479
    59
subsubsection{*Preliminary Lemmas about the Equivalence Relation*}
paulson@14378
    60
paulson@14496
    61
lemma intrel_iff [simp]: "(((x,y),(u,v)) \<in> intrel) = (x+v = u+y)"
paulson@14479
    62
by (simp add: intrel_def)
paulson@14259
    63
paulson@14259
    64
lemma equiv_intrel: "equiv UNIV intrel"
paulson@14479
    65
by (simp add: intrel_def equiv_def refl_def sym_def trans_def)
paulson@14259
    66
paulson@14496
    67
text{*Reduces equality of equivalence classes to the @{term intrel} relation:
paulson@14496
    68
  @{term "(intrel `` {x} = intrel `` {y}) = ((x,y) \<in> intrel)"} *}
paulson@14496
    69
lemmas equiv_intrel_iff = eq_equiv_class_iff [OF equiv_intrel UNIV_I UNIV_I]
paulson@14259
    70
paulson@14496
    71
declare equiv_intrel_iff [simp]
paulson@14496
    72
paulson@14496
    73
paulson@14496
    74
text{*All equivalence classes belong to set of representatives*}
paulson@14532
    75
lemma [simp]: "intrel``{(x,y)} \<in> Integ"
paulson@14496
    76
by (auto simp add: Integ_def intrel_def quotient_def)
paulson@14259
    77
paulson@14259
    78
lemma inj_on_Abs_Integ: "inj_on Abs_Integ Integ"
paulson@14259
    79
apply (rule inj_on_inverseI)
paulson@14259
    80
apply (erule Abs_Integ_inverse)
paulson@14259
    81
done
paulson@14259
    82
paulson@14496
    83
text{*This theorem reduces equality on abstractions to equality on 
paulson@14496
    84
      representatives:
paulson@14496
    85
  @{term "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
paulson@14479
    86
declare inj_on_Abs_Integ [THEN inj_on_iff, simp]
paulson@14496
    87
paulson@14496
    88
declare Abs_Integ_inverse [simp]
paulson@14259
    89
paulson@14479
    90
text{*Case analysis on the representation of an integer as an equivalence
paulson@14485
    91
      class of pairs of naturals.*}
paulson@14479
    92
lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
paulson@14479
    93
     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
paulson@14479
    94
apply (rule Rep_Integ [of z, unfolded Integ_def, THEN quotientE])
paulson@14479
    95
apply (drule arg_cong [where f=Abs_Integ])
paulson@14479
    96
apply (auto simp add: Rep_Integ_inverse)
paulson@14479
    97
done
paulson@14259
    98
paulson@14479
    99
paulson@14479
   100
subsubsection{*@{term int}: Embedding the Naturals into the Integers*}
paulson@14259
   101
paulson@14259
   102
lemma inj_int: "inj int"
paulson@14479
   103
by (simp add: inj_on_def int_def)
paulson@14259
   104
paulson@14341
   105
lemma int_int_eq [iff]: "(int m = int n) = (m = n)"
paulson@14341
   106
by (fast elim!: inj_int [THEN injD])
paulson@14341
   107
paulson@14341
   108
paulson@14479
   109
subsubsection{*Integer Unary Negation*}
paulson@14259
   110
paulson@14479
   111
lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
paulson@14479
   112
proof -
paulson@14532
   113
  have "congruent intrel (\<lambda>(x,y). intrel``{(y,x)})"
paulson@14479
   114
    by (simp add: congruent_def) 
paulson@14479
   115
  thus ?thesis
paulson@14479
   116
    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
paulson@14479
   117
qed
paulson@14259
   118
paulson@14479
   119
lemma zminus_zminus: "- (- z) = (z::int)"
paulson@14479
   120
by (cases z, simp add: minus)
paulson@14259
   121
paulson@14479
   122
lemma zminus_0: "- 0 = (0::int)"
paulson@14479
   123
by (simp add: int_def Zero_int_def minus)
paulson@14259
   124
paulson@14259
   125
paulson@14479
   126
subsection{*Integer Addition*}
paulson@14259
   127
paulson@14479
   128
lemma add:
paulson@14479
   129
     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
paulson@14479
   130
      Abs_Integ (intrel``{(x+u, y+v)})"
paulson@14479
   131
proof -
paulson@14658
   132
  have "congruent2 intrel intrel
paulson@14532
   133
        (\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z)"
paulson@14479
   134
    by (simp add: congruent2_def)
paulson@14479
   135
  thus ?thesis
paulson@14479
   136
    by (simp add: add_int_def UN_UN_split_split_eq
paulson@14658
   137
                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
paulson@14479
   138
qed
paulson@14259
   139
paulson@14479
   140
lemma zminus_zadd_distrib: "- (z + w) = (- z) + (- w::int)"
paulson@14479
   141
by (cases z, cases w, simp add: minus add)
paulson@14259
   142
paulson@14259
   143
lemma zadd_commute: "(z::int) + w = w + z"
paulson@14479
   144
by (cases z, cases w, simp add: add_ac add)
paulson@14259
   145
paulson@14259
   146
lemma zadd_assoc: "((z1::int) + z2) + z3 = z1 + (z2 + z3)"
paulson@14479
   147
by (cases z1, cases z2, cases z3, simp add: add add_assoc)
paulson@14259
   148
paulson@14259
   149
(*For AC rewriting*)
paulson@14271
   150
lemma zadd_left_commute: "x + (y + z) = y + ((x + z)  ::int)"
paulson@14259
   151
  apply (rule mk_left_commute [of "op +"])
paulson@14259
   152
  apply (rule zadd_assoc)
paulson@14259
   153
  apply (rule zadd_commute)
paulson@14259
   154
  done
paulson@14259
   155
paulson@14259
   156
lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
paulson@14259
   157
obua@14738
   158
lemmas zmult_ac = OrderedGroup.mult_ac
paulson@14271
   159
paulson@14259
   160
lemma zadd_int: "(int m) + (int n) = int (m + n)"
paulson@14479
   161
by (simp add: int_def add)
paulson@14259
   162
paulson@14259
   163
lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
paulson@14259
   164
by (simp add: zadd_int zadd_assoc [symmetric])
paulson@14259
   165
paulson@14259
   166
lemma int_Suc: "int (Suc m) = 1 + (int m)"
paulson@14259
   167
by (simp add: One_int_def zadd_int)
paulson@14259
   168
obua@14738
   169
(*also for the instance declaration int :: comm_monoid_add*)
paulson@14479
   170
lemma zadd_0: "(0::int) + z = z"
paulson@14479
   171
apply (simp add: Zero_int_def int_def)
paulson@14479
   172
apply (cases z, simp add: add)
paulson@14259
   173
done
paulson@14259
   174
paulson@14479
   175
lemma zadd_0_right: "z + (0::int) = z"
paulson@14479
   176
by (rule trans [OF zadd_commute zadd_0])
paulson@14259
   177
paulson@14479
   178
lemma zadd_zminus_inverse2: "(- z) + z = (0::int)"
paulson@14479
   179
by (cases z, simp add: int_def Zero_int_def minus add)
paulson@14259
   180
paulson@14259
   181
paulson@14479
   182
subsection{*Integer Multiplication*}
paulson@14259
   183
paulson@14378
   184
text{*Congruence property for multiplication*}
paulson@14479
   185
lemma mult_congruent2:
paulson@14658
   186
     "congruent2 intrel intrel
paulson@14479
   187
        (%p1 p2. (%(x,y). (%(u,v).
paulson@14532
   188
                    intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)"
paulson@14259
   189
apply (rule equiv_intrel [THEN congruent2_commuteI])
paulson@14532
   190
 apply (force simp add: mult_ac, clarify) 
paulson@14532
   191
apply (simp add: congruent_def mult_ac)  
paulson@14479
   192
apply (rename_tac u v w x y z)
paulson@14479
   193
apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
paulson@14479
   194
apply (simp add: mult_ac, arith)
paulson@14259
   195
apply (simp add: add_mult_distrib [symmetric])
paulson@14259
   196
done
paulson@14259
   197
paulson@14532
   198
paulson@14479
   199
lemma mult:
paulson@14479
   200
     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
paulson@14479
   201
      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
paulson@14479
   202
by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
paulson@14658
   203
              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
paulson@14259
   204
paulson@14259
   205
lemma zmult_zminus: "(- z) * w = - (z * (w::int))"
paulson@14479
   206
by (cases z, cases w, simp add: minus mult add_ac)
paulson@14259
   207
paulson@14259
   208
lemma zmult_commute: "(z::int) * w = w * z"
paulson@14479
   209
by (cases z, cases w, simp add: mult add_ac mult_ac)
paulson@14259
   210
paulson@14259
   211
lemma zmult_assoc: "((z1::int) * z2) * z3 = z1 * (z2 * z3)"
paulson@14479
   212
by (cases z1, cases z2, cases z3, simp add: mult add_mult_distrib2 mult_ac)
paulson@14259
   213
paulson@14259
   214
lemma zadd_zmult_distrib: "((z1::int) + z2) * w = (z1 * w) + (z2 * w)"
paulson@14479
   215
by (cases z1, cases z2, cases w, simp add: add mult add_mult_distrib2 mult_ac)
paulson@14259
   216
paulson@14259
   217
lemma zadd_zmult_distrib2: "(w::int) * (z1 + z2) = (w * z1) + (w * z2)"
paulson@14259
   218
by (simp add: zmult_commute [of w] zadd_zmult_distrib)
paulson@14259
   219
paulson@14259
   220
lemma zdiff_zmult_distrib: "((z1::int) - z2) * w = (z1 * w) - (z2 * w)"
paulson@14496
   221
by (simp add: diff_int_def zadd_zmult_distrib zmult_zminus)
paulson@14259
   222
paulson@14259
   223
lemma zdiff_zmult_distrib2: "(w::int) * (z1 - z2) = (w * z1) - (w * z2)"
paulson@14259
   224
by (simp add: zmult_commute [of w] zdiff_zmult_distrib)
paulson@14259
   225
paulson@14259
   226
lemmas int_distrib =
paulson@14479
   227
  zadd_zmult_distrib zadd_zmult_distrib2
paulson@14259
   228
  zdiff_zmult_distrib zdiff_zmult_distrib2
paulson@14259
   229
paulson@14259
   230
lemma zmult_int: "(int m) * (int n) = int (m * n)"
paulson@14479
   231
by (simp add: int_def mult)
paulson@14259
   232
paulson@14479
   233
lemma zmult_1: "(1::int) * z = z"
paulson@14479
   234
by (cases z, simp add: One_int_def int_def mult)
paulson@14259
   235
paulson@14479
   236
lemma zmult_1_right: "z * (1::int) = z"
paulson@14259
   237
by (rule trans [OF zmult_commute zmult_1])
paulson@14259
   238
paulson@14259
   239
nipkow@14740
   240
text{*The integers form a @{text comm_ring_1}*}
obua@14738
   241
instance int :: comm_ring_1
paulson@14341
   242
proof
paulson@14341
   243
  fix i j k :: int
paulson@14341
   244
  show "(i + j) + k = i + (j + k)" by (simp add: zadd_assoc)
paulson@14341
   245
  show "i + j = j + i" by (simp add: zadd_commute)
paulson@14479
   246
  show "0 + i = i" by (rule zadd_0)
paulson@14479
   247
  show "- i + i = 0" by (rule zadd_zminus_inverse2)
paulson@14479
   248
  show "i - j = i + (-j)" by (simp add: diff_int_def)
paulson@14341
   249
  show "(i * j) * k = i * (j * k)" by (rule zmult_assoc)
paulson@14341
   250
  show "i * j = j * i" by (rule zmult_commute)
paulson@14479
   251
  show "1 * i = i" by (rule zmult_1) 
paulson@14341
   252
  show "(i + j) * k = i * k + j * k" by (simp add: int_distrib)
paulson@14479
   253
  show "0 \<noteq> (1::int)"
paulson@14348
   254
    by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
paulson@14341
   255
qed
paulson@14341
   256
paulson@14341
   257
paulson@14378
   258
subsection{*The @{text "\<le>"} Ordering*}
paulson@14378
   259
paulson@14479
   260
lemma le:
paulson@14479
   261
  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
paulson@14479
   262
by (force simp add: le_int_def)
paulson@14378
   263
paulson@14378
   264
lemma zle_refl: "w \<le> (w::int)"
paulson@14479
   265
by (cases w, simp add: le)
paulson@14378
   266
paulson@14378
   267
lemma zle_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::int)"
paulson@14479
   268
by (cases i, cases j, cases k, simp add: le)
paulson@14378
   269
paulson@14378
   270
lemma zle_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::int)"
paulson@14479
   271
by (cases w, cases z, simp add: le)
paulson@14378
   272
paulson@14378
   273
(* Axiom 'order_less_le' of class 'order': *)
paulson@14378
   274
lemma zless_le: "((w::int) < z) = (w \<le> z & w \<noteq> z)"
paulson@14479
   275
by (simp add: less_int_def)
paulson@14378
   276
paulson@14378
   277
instance int :: order
wenzelm@14691
   278
  by intro_classes
wenzelm@14691
   279
    (assumption |
wenzelm@14691
   280
      rule zle_refl zle_trans zle_anti_sym zless_le)+
paulson@14378
   281
paulson@14378
   282
(* Axiom 'linorder_linear' of class 'linorder': *)
paulson@14378
   283
lemma zle_linear: "(z::int) \<le> w | w \<le> z"
wenzelm@14691
   284
by (cases z, cases w) (simp add: le linorder_linear)
paulson@14378
   285
paulson@14378
   286
instance int :: linorder
wenzelm@14691
   287
  by intro_classes (rule zle_linear)
paulson@14378
   288
paulson@14378
   289
paulson@14378
   290
lemmas zless_linear = linorder_less_linear [where 'a = int]
paulson@14378
   291
paulson@14378
   292
paulson@14378
   293
lemma int_eq_0_conv [simp]: "(int n = 0) = (n = 0)"
paulson@14378
   294
by (simp add: Zero_int_def)
paulson@14259
   295
paulson@14259
   296
lemma zless_int [simp]: "(int m < int n) = (m<n)"
paulson@14479
   297
by (simp add: le add int_def linorder_not_le [symmetric]) 
paulson@14259
   298
paulson@14259
   299
lemma int_less_0_conv [simp]: "~ (int k < 0)"
paulson@14259
   300
by (simp add: Zero_int_def)
paulson@14259
   301
paulson@14259
   302
lemma zero_less_int_conv [simp]: "(0 < int n) = (0 < n)"
paulson@14259
   303
by (simp add: Zero_int_def)
paulson@14259
   304
paulson@14341
   305
lemma int_0_less_1: "0 < (1::int)"
paulson@14341
   306
by (simp only: Zero_int_def One_int_def One_nat_def zless_int)
paulson@14259
   307
paulson@14341
   308
lemma int_0_neq_1 [simp]: "0 \<noteq> (1::int)"
paulson@14341
   309
by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
paulson@14341
   310
paulson@14378
   311
lemma zle_int [simp]: "(int m \<le> int n) = (m\<le>n)"
paulson@14378
   312
by (simp add: linorder_not_less [symmetric])
paulson@14259
   313
paulson@14378
   314
lemma zero_zle_int [simp]: "(0 \<le> int n)"
paulson@14378
   315
by (simp add: Zero_int_def)
paulson@14259
   316
paulson@14378
   317
lemma int_le_0_conv [simp]: "(int n \<le> 0) = (n = 0)"
paulson@14378
   318
by (simp add: Zero_int_def)
paulson@14378
   319
paulson@14378
   320
lemma int_0 [simp]: "int 0 = (0::int)"
paulson@14259
   321
by (simp add: Zero_int_def)
paulson@14259
   322
paulson@14378
   323
lemma int_1 [simp]: "int 1 = 1"
paulson@14378
   324
by (simp add: One_int_def)
paulson@14378
   325
paulson@14378
   326
lemma int_Suc0_eq_1: "int (Suc 0) = 1"
paulson@14378
   327
by (simp add: One_int_def One_nat_def)
paulson@14378
   328
paulson@14479
   329
paulson@14378
   330
subsection{*Monotonicity results*}
paulson@14378
   331
paulson@14479
   332
lemma zadd_left_mono: "i \<le> j ==> k + i \<le> k + (j::int)"
paulson@14479
   333
by (cases i, cases j, cases k, simp add: le add)
paulson@14378
   334
paulson@14479
   335
lemma zadd_strict_right_mono: "i < j ==> i + k < j + (k::int)"
paulson@14479
   336
apply (cases i, cases j, cases k)
paulson@14479
   337
apply (simp add: linorder_not_le [where 'a = int, symmetric]
paulson@14479
   338
                 linorder_not_le [where 'a = nat]  le add)
paulson@14378
   339
done
paulson@14378
   340
paulson@14378
   341
lemma zadd_zless_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::int)"
paulson@14479
   342
by (rule order_less_le_trans [OF zadd_strict_right_mono zadd_left_mono])
paulson@14378
   343
paulson@14378
   344
paulson@14378
   345
subsection{*Strict Monotonicity of Multiplication*}
paulson@14378
   346
paulson@14378
   347
text{*strict, in 1st argument; proof is by induction on k>0*}
paulson@14378
   348
lemma zmult_zless_mono2_lemma [rule_format]:
paulson@14378
   349
     "i<j ==> 0<k --> int k * i < int k * j"
paulson@14479
   350
apply (induct_tac "k", simp)
paulson@14378
   351
apply (simp add: int_Suc)
paulson@14378
   352
apply (case_tac "n=0")
paulson@14378
   353
apply (simp_all add: zadd_zmult_distrib int_Suc0_eq_1 order_le_less)
paulson@14378
   354
apply (simp add: zadd_zless_mono int_Suc0_eq_1 order_le_less)
paulson@14378
   355
done
paulson@14259
   356
paulson@14378
   357
lemma zero_le_imp_eq_int: "0 \<le> k ==> \<exists>n. k = int n"
paulson@14479
   358
apply (cases k)
paulson@14479
   359
apply (auto simp add: le add int_def Zero_int_def)
paulson@14479
   360
apply (rule_tac x="x-y" in exI, simp)
paulson@14378
   361
done
paulson@14378
   362
paulson@14378
   363
lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
paulson@14479
   364
apply (frule order_less_imp_le [THEN zero_le_imp_eq_int])
paulson@14479
   365
apply (auto simp add: zmult_zless_mono2_lemma)
paulson@14378
   366
done
paulson@14378
   367
paulson@14378
   368
paulson@14378
   369
defs (overloaded)
paulson@14378
   370
    zabs_def:  "abs(i::int) == if i < 0 then -i else i"
paulson@14378
   371
paulson@14378
   372
nipkow@14740
   373
text{*The integers form an ordered @{text comm_ring_1}*}
obua@14738
   374
instance int :: ordered_idom
paulson@14378
   375
proof
paulson@14378
   376
  fix i j k :: int
paulson@14378
   377
  show "i \<le> j ==> k + i \<le> k + j" by (rule zadd_left_mono)
paulson@14378
   378
  show "i < j ==> 0 < k ==> k * i < k * j" by (rule zmult_zless_mono2)
paulson@14378
   379
  show "\<bar>i\<bar> = (if i < 0 then -i else i)" by (simp only: zabs_def)
paulson@14378
   380
qed
paulson@14378
   381
paulson@14378
   382
paulson@14479
   383
lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
paulson@14479
   384
apply (cases w, cases z) 
paulson@14479
   385
apply (simp add: linorder_not_le [symmetric] le int_def add One_int_def)
paulson@14479
   386
done
paulson@14479
   387
paulson@14378
   388
subsection{*Magnitide of an Integer, as a Natural Number: @{term nat}*}
paulson@14378
   389
paulson@14378
   390
constdefs
paulson@14378
   391
   nat  :: "int => nat"
paulson@14532
   392
    "nat z == contents (\<Union>(x,y) \<in> Rep_Integ z. { x-y })"
paulson@14479
   393
paulson@14479
   394
lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
paulson@14479
   395
proof -
paulson@14479
   396
  have "congruent intrel (\<lambda>(x,y). {x-y})"
paulson@14479
   397
    by (simp add: congruent_def, arith) 
paulson@14479
   398
  thus ?thesis
paulson@14479
   399
    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
paulson@14479
   400
qed
paulson@14378
   401
paulson@14378
   402
lemma nat_int [simp]: "nat(int n) = n"
paulson@14479
   403
by (simp add: nat int_def) 
paulson@14378
   404
paulson@14378
   405
lemma nat_zero [simp]: "nat 0 = 0"
paulson@14479
   406
by (simp only: Zero_int_def nat_int)
paulson@14378
   407
paulson@14479
   408
lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
paulson@14479
   409
by (cases z, simp add: nat le int_def Zero_int_def)
paulson@14479
   410
paulson@14479
   411
corollary nat_0_le: "0 \<le> z ==> int (nat z) = z"
paulson@14479
   412
apply simp 
paulson@14259
   413
done
paulson@14259
   414
paulson@14378
   415
lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
paulson@14479
   416
by (cases z, simp add: nat le int_def Zero_int_def)
paulson@14479
   417
paulson@14479
   418
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
paulson@14479
   419
apply (cases w, cases z) 
paulson@14479
   420
apply (simp add: nat le linorder_not_le [symmetric] int_def Zero_int_def, arith)
paulson@14479
   421
done
paulson@14378
   422
paulson@14378
   423
text{*An alternative condition is @{term "0 \<le> w"} *}
paulson@14479
   424
corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
paulson@14479
   425
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
paulson@14479
   426
paulson@14479
   427
corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
paulson@14479
   428
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
paulson@14479
   429
paulson@14479
   430
lemma zless_nat_conj: "(nat w < nat z) = (0 < z & w < z)"
paulson@14479
   431
apply (cases w, cases z) 
paulson@14479
   432
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
paulson@14378
   433
done
paulson@14378
   434
paulson@14479
   435
lemma nonneg_eq_int: "[| 0 \<le> z;  !!m. z = int m ==> P |] ==> P"
paulson@14479
   436
by (blast dest: nat_0_le sym)
paulson@14479
   437
paulson@14479
   438
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
paulson@14479
   439
by (cases w, simp add: nat le int_def Zero_int_def, arith)
paulson@14479
   440
paulson@14479
   441
corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
paulson@14479
   442
by (simp only: eq_commute [of m] nat_eq_iff) 
paulson@14479
   443
paulson@14479
   444
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)"
paulson@14479
   445
apply (cases w)
paulson@14479
   446
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
paulson@14378
   447
done
paulson@14378
   448
paulson@14479
   449
lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)"
paulson@14479
   450
by (auto simp add: nat_eq_iff2)
paulson@14479
   451
paulson@14479
   452
lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
paulson@14479
   453
by (insert zless_nat_conj [of 0], auto)
paulson@14479
   454
paulson@14479
   455
paulson@14479
   456
lemma nat_add_distrib:
paulson@14479
   457
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
paulson@14479
   458
by (cases z, cases z', simp add: nat add le int_def Zero_int_def)
paulson@14479
   459
paulson@14479
   460
lemma nat_diff_distrib:
paulson@14479
   461
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
paulson@14479
   462
by (cases z, cases z', 
paulson@14479
   463
    simp add: nat add minus diff_minus le int_def Zero_int_def)
paulson@14479
   464
paulson@14479
   465
paulson@14479
   466
lemma nat_zminus_int [simp]: "nat (- (int n)) = 0"
paulson@14479
   467
by (simp add: int_def minus nat Zero_int_def) 
paulson@14479
   468
paulson@14479
   469
lemma zless_nat_eq_int_zless: "(m < nat z) = (int m < z)"
paulson@14479
   470
by (cases z, simp add: nat le int_def  linorder_not_le [symmetric], arith)
paulson@14479
   471
paulson@14378
   472
paulson@14378
   473
subsection{*Lemmas about the Function @{term int} and Orderings*}
paulson@14378
   474
paulson@14378
   475
lemma negative_zless_0: "- (int (Suc n)) < 0"
paulson@14479
   476
by (simp add: order_less_le)
paulson@14378
   477
paulson@14378
   478
lemma negative_zless [iff]: "- (int (Suc n)) < int m"
paulson@14378
   479
by (rule negative_zless_0 [THEN order_less_le_trans], simp)
paulson@14378
   480
paulson@14378
   481
lemma negative_zle_0: "- int n \<le> 0"
paulson@14378
   482
by (simp add: minus_le_iff)
paulson@14378
   483
paulson@14378
   484
lemma negative_zle [iff]: "- int n \<le> int m"
paulson@14378
   485
by (rule order_trans [OF negative_zle_0 zero_zle_int])
paulson@14378
   486
paulson@14378
   487
lemma not_zle_0_negative [simp]: "~ (0 \<le> - (int (Suc n)))"
paulson@14378
   488
by (subst le_minus_iff, simp)
paulson@14378
   489
paulson@14378
   490
lemma int_zle_neg: "(int n \<le> - int m) = (n = 0 & m = 0)"
paulson@14479
   491
by (simp add: int_def le minus Zero_int_def) 
paulson@14259
   492
paulson@14378
   493
lemma not_int_zless_negative [simp]: "~ (int n < - int m)"
paulson@14378
   494
by (simp add: linorder_not_less)
paulson@14378
   495
paulson@14378
   496
lemma negative_eq_positive [simp]: "(- int n = int m) = (n = 0 & m = 0)"
paulson@14378
   497
by (force simp add: order_eq_iff [of "- int n"] int_zle_neg)
paulson@14378
   498
paulson@14378
   499
lemma zle_iff_zadd: "(w \<le> z) = (\<exists>n. z = w + int n)"
paulson@14479
   500
apply (cases w, cases z)
paulson@14479
   501
apply (auto simp add: le add int_def) 
paulson@14479
   502
apply (rename_tac a b c d) 
paulson@14479
   503
apply (rule_tac x="c+b - (a+d)" in exI) 
paulson@14479
   504
apply arith
paulson@14479
   505
done
paulson@14378
   506
paulson@14479
   507
lemma abs_int_eq [simp]: "abs (int m) = int m"
paulson@15003
   508
by (simp add: abs_if)
paulson@14378
   509
paulson@14378
   510
text{*This version is proved for all ordered rings, not just integers!
paulson@14378
   511
      It is proved here because attribute @{text arith_split} is not available
paulson@14378
   512
      in theory @{text Ring_and_Field}.
paulson@14378
   513
      But is it really better than just rewriting with @{text abs_if}?*}
paulson@14378
   514
lemma abs_split [arith_split]:
obua@14738
   515
     "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))"
paulson@14378
   516
by (force dest: order_less_le_trans simp add: abs_if linorder_not_less)
paulson@14378
   517
paulson@14378
   518
paulson@14378
   519
paulson@14378
   520
subsection{*The Constants @{term neg} and @{term iszero}*}
paulson@14378
   521
paulson@14378
   522
constdefs
paulson@14378
   523
obua@14738
   524
  neg   :: "'a::ordered_idom => bool"
paulson@14378
   525
  "neg(Z) == Z < 0"
paulson@14378
   526
paulson@14378
   527
  (*For simplifying equalities*)
obua@14738
   528
  iszero :: "'a::comm_semiring_1_cancel => bool"
paulson@14378
   529
  "iszero z == z = (0)"
paulson@14479
   530
paulson@14378
   531
paulson@14378
   532
lemma not_neg_int [simp]: "~ neg(int n)"
paulson@14378
   533
by (simp add: neg_def)
paulson@14378
   534
paulson@14378
   535
lemma neg_zminus_int [simp]: "neg(- (int (Suc n)))"
paulson@14378
   536
by (simp add: neg_def neg_less_0_iff_less)
paulson@14378
   537
paulson@14378
   538
lemmas neg_eq_less_0 = neg_def
paulson@14378
   539
paulson@14378
   540
lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
paulson@14378
   541
by (simp add: neg_def linorder_not_less)
paulson@14378
   542
paulson@14479
   543
paulson@14378
   544
subsection{*To simplify inequalities when Numeral1 can get simplified to 1*}
paulson@14378
   545
paulson@14378
   546
lemma not_neg_0: "~ neg 0"
paulson@14378
   547
by (simp add: One_int_def neg_def)
paulson@14378
   548
paulson@14378
   549
lemma not_neg_1: "~ neg 1"
paulson@14479
   550
by (simp add: neg_def linorder_not_less zero_le_one)
paulson@14378
   551
paulson@14378
   552
lemma iszero_0: "iszero 0"
paulson@14378
   553
by (simp add: iszero_def)
paulson@14378
   554
paulson@14378
   555
lemma not_iszero_1: "~ iszero 1"
paulson@14479
   556
by (simp add: iszero_def eq_commute)
paulson@14378
   557
paulson@14378
   558
lemma neg_nat: "neg z ==> nat z = 0"
paulson@14479
   559
by (simp add: neg_def order_less_imp_le) 
paulson@14378
   560
paulson@14378
   561
lemma not_neg_nat: "~ neg z ==> int (nat z) = z"
paulson@14378
   562
by (simp add: linorder_not_less neg_def)
paulson@14378
   563
paulson@14378
   564
nipkow@14740
   565
subsection{*Embedding of the Naturals into any @{text
nipkow@14740
   566
comm_semiring_1_cancel}: @{term of_nat}*}
paulson@14378
   567
obua@14738
   568
consts of_nat :: "nat => 'a::comm_semiring_1_cancel"
paulson@14378
   569
paulson@14378
   570
primrec
paulson@14378
   571
  of_nat_0:   "of_nat 0 = 0"
paulson@14378
   572
  of_nat_Suc: "of_nat (Suc m) = of_nat m + 1"
paulson@14378
   573
paulson@14378
   574
lemma of_nat_1 [simp]: "of_nat 1 = 1"
paulson@14378
   575
by simp
paulson@14378
   576
paulson@14378
   577
lemma of_nat_add [simp]: "of_nat (m+n) = of_nat m + of_nat n"
paulson@14378
   578
apply (induct m)
paulson@14479
   579
apply (simp_all add: add_ac)
paulson@14378
   580
done
paulson@14378
   581
paulson@14378
   582
lemma of_nat_mult [simp]: "of_nat (m*n) = of_nat m * of_nat n"
paulson@14479
   583
apply (induct m)
paulson@14479
   584
apply (simp_all add: mult_ac add_ac right_distrib)
paulson@14378
   585
done
paulson@14378
   586
obua@14738
   587
lemma zero_le_imp_of_nat: "0 \<le> (of_nat m::'a::ordered_semidom)"
paulson@14479
   588
apply (induct m, simp_all)
paulson@14479
   589
apply (erule order_trans)
paulson@14479
   590
apply (rule less_add_one [THEN order_less_imp_le])
paulson@14259
   591
done
paulson@14259
   592
paulson@14378
   593
lemma less_imp_of_nat_less:
obua@14738
   594
     "m < n ==> of_nat m < (of_nat n::'a::ordered_semidom)"
paulson@14479
   595
apply (induct m n rule: diff_induct, simp_all)
paulson@14479
   596
apply (insert add_le_less_mono [OF zero_le_imp_of_nat zero_less_one], force)
paulson@14378
   597
done
paulson@14378
   598
paulson@14378
   599
lemma of_nat_less_imp_less:
obua@14738
   600
     "of_nat m < (of_nat n::'a::ordered_semidom) ==> m < n"
paulson@14479
   601
apply (induct m n rule: diff_induct, simp_all)
paulson@14479
   602
apply (insert zero_le_imp_of_nat)
paulson@14479
   603
apply (force simp add: linorder_not_less [symmetric])
paulson@14259
   604
done
paulson@14259
   605
paulson@14378
   606
lemma of_nat_less_iff [simp]:
obua@14738
   607
     "(of_nat m < (of_nat n::'a::ordered_semidom)) = (m<n)"
paulson@14479
   608
by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
paulson@14378
   609
paulson@14378
   610
text{*Special cases where either operand is zero*}
paulson@14378
   611
declare of_nat_less_iff [of 0, simplified, simp]
paulson@14378
   612
declare of_nat_less_iff [of _ 0, simplified, simp]
paulson@14378
   613
paulson@14378
   614
lemma of_nat_le_iff [simp]:
obua@14738
   615
     "(of_nat m \<le> (of_nat n::'a::ordered_semidom)) = (m \<le> n)"
paulson@14479
   616
by (simp add: linorder_not_less [symmetric])
paulson@14378
   617
paulson@14378
   618
text{*Special cases where either operand is zero*}
paulson@14378
   619
declare of_nat_le_iff [of 0, simplified, simp]
paulson@14378
   620
declare of_nat_le_iff [of _ 0, simplified, simp]
paulson@14378
   621
nipkow@14740
   622
text{*The ordering on the @{text comm_semiring_1_cancel} is necessary
nipkow@14740
   623
to exclude the possibility of a finite field, which indeed wraps back to
nipkow@14740
   624
zero.*}
paulson@14378
   625
lemma of_nat_eq_iff [simp]:
obua@14738
   626
     "(of_nat m = (of_nat n::'a::ordered_semidom)) = (m = n)"
paulson@14479
   627
by (simp add: order_eq_iff)
paulson@14378
   628
paulson@14378
   629
text{*Special cases where either operand is zero*}
paulson@14378
   630
declare of_nat_eq_iff [of 0, simplified, simp]
paulson@14378
   631
declare of_nat_eq_iff [of _ 0, simplified, simp]
paulson@14378
   632
paulson@14378
   633
lemma of_nat_diff [simp]:
obua@14738
   634
     "n \<le> m ==> of_nat (m - n) = of_nat m - (of_nat n :: 'a::comm_ring_1)"
paulson@14378
   635
by (simp del: of_nat_add
paulson@14479
   636
	 add: compare_rls of_nat_add [symmetric] split add: nat_diff_split)
paulson@14378
   637
paulson@14378
   638
paulson@14378
   639
subsection{*The Set of Natural Numbers*}
paulson@14378
   640
paulson@14378
   641
constdefs
obua@14738
   642
   Nats  :: "'a::comm_semiring_1_cancel set"
paulson@14378
   643
    "Nats == range of_nat"
paulson@14378
   644
paulson@14378
   645
syntax (xsymbols)    Nats :: "'a set"   ("\<nat>")
paulson@14378
   646
paulson@14378
   647
lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
paulson@14479
   648
by (simp add: Nats_def)
paulson@14378
   649
paulson@14378
   650
lemma Nats_0 [simp]: "0 \<in> Nats"
paulson@14479
   651
apply (simp add: Nats_def)
paulson@14479
   652
apply (rule range_eqI)
paulson@14378
   653
apply (rule of_nat_0 [symmetric])
paulson@14378
   654
done
paulson@14378
   655
paulson@14378
   656
lemma Nats_1 [simp]: "1 \<in> Nats"
paulson@14479
   657
apply (simp add: Nats_def)
paulson@14479
   658
apply (rule range_eqI)
paulson@14378
   659
apply (rule of_nat_1 [symmetric])
paulson@14378
   660
done
paulson@14378
   661
paulson@14378
   662
lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
paulson@14479
   663
apply (auto simp add: Nats_def)
paulson@14479
   664
apply (rule range_eqI)
paulson@14378
   665
apply (rule of_nat_add [symmetric])
paulson@14378
   666
done
paulson@14378
   667
paulson@14378
   668
lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
paulson@14479
   669
apply (auto simp add: Nats_def)
paulson@14479
   670
apply (rule range_eqI)
paulson@14378
   671
apply (rule of_nat_mult [symmetric])
paulson@14259
   672
done
paulson@14259
   673
paulson@14378
   674
text{*Agreement with the specific embedding for the integers*}
paulson@14378
   675
lemma int_eq_of_nat: "int = (of_nat :: nat => int)"
paulson@14378
   676
proof
paulson@14378
   677
  fix n
paulson@14479
   678
  show "int n = of_nat n"  by (induct n, simp_all add: int_Suc add_ac)
paulson@14378
   679
qed
paulson@14378
   680
paulson@14496
   681
lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
paulson@14496
   682
proof
paulson@14496
   683
  fix n
paulson@14496
   684
  show "of_nat n = id n"  by (induct n, simp_all)
paulson@14496
   685
qed
paulson@14496
   686
paulson@14378
   687
nipkow@14740
   688
subsection{*Embedding of the Integers into any @{text comm_ring_1}:
nipkow@14740
   689
@{term of_int}*}
paulson@14378
   690
paulson@14378
   691
constdefs
obua@14738
   692
   of_int :: "int => 'a::comm_ring_1"
paulson@14532
   693
   "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
paulson@14378
   694
paulson@14378
   695
paulson@14378
   696
lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
paulson@14496
   697
proof -
paulson@14496
   698
  have "congruent intrel (\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) })"
paulson@14496
   699
    by (simp add: congruent_def compare_rls of_nat_add [symmetric]
paulson@14496
   700
            del: of_nat_add) 
paulson@14496
   701
  thus ?thesis
paulson@14496
   702
    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
paulson@14496
   703
qed
paulson@14378
   704
paulson@14378
   705
lemma of_int_0 [simp]: "of_int 0 = 0"
paulson@14378
   706
by (simp add: of_int Zero_int_def int_def)
paulson@14378
   707
paulson@14378
   708
lemma of_int_1 [simp]: "of_int 1 = 1"
paulson@14378
   709
by (simp add: of_int One_int_def int_def)
paulson@14378
   710
paulson@14378
   711
lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
paulson@14479
   712
by (cases w, cases z, simp add: compare_rls of_int add)
paulson@14378
   713
paulson@14378
   714
lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
paulson@14479
   715
by (cases z, simp add: compare_rls of_int minus)
paulson@14259
   716
paulson@14378
   717
lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z"
paulson@14378
   718
by (simp add: diff_minus)
paulson@14378
   719
paulson@14378
   720
lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
paulson@14479
   721
apply (cases w, cases z)
paulson@14479
   722
apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
paulson@14479
   723
                 mult add_ac)
paulson@14378
   724
done
paulson@14378
   725
paulson@14378
   726
lemma of_int_le_iff [simp]:
obua@14738
   727
     "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
paulson@14479
   728
apply (cases w)
paulson@14479
   729
apply (cases z)
paulson@14479
   730
apply (simp add: compare_rls of_int le diff_int_def add minus
paulson@14479
   731
                 of_nat_add [symmetric]   del: of_nat_add)
paulson@14378
   732
done
paulson@14378
   733
paulson@14378
   734
text{*Special cases where either operand is zero*}
paulson@14378
   735
declare of_int_le_iff [of 0, simplified, simp]
paulson@14378
   736
declare of_int_le_iff [of _ 0, simplified, simp]
paulson@14259
   737
paulson@14378
   738
lemma of_int_less_iff [simp]:
obua@14738
   739
     "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
paulson@14378
   740
by (simp add: linorder_not_le [symmetric])
paulson@14378
   741
paulson@14378
   742
text{*Special cases where either operand is zero*}
paulson@14378
   743
declare of_int_less_iff [of 0, simplified, simp]
paulson@14378
   744
declare of_int_less_iff [of _ 0, simplified, simp]
paulson@14378
   745
nipkow@14740
   746
text{*The ordering on the @{text comm_ring_1} is necessary.
nipkow@14740
   747
 See @{text of_nat_eq_iff} above.*}
paulson@14378
   748
lemma of_int_eq_iff [simp]:
obua@14738
   749
     "(of_int w = (of_int z::'a::ordered_idom)) = (w = z)"
paulson@14479
   750
by (simp add: order_eq_iff)
paulson@14378
   751
paulson@14378
   752
text{*Special cases where either operand is zero*}
paulson@14378
   753
declare of_int_eq_iff [of 0, simplified, simp]
paulson@14378
   754
declare of_int_eq_iff [of _ 0, simplified, simp]
paulson@14378
   755
paulson@14496
   756
lemma of_int_eq_id [simp]: "of_int = (id :: int => int)"
paulson@14496
   757
proof
paulson@14496
   758
 fix z
paulson@14496
   759
 show "of_int z = id z"  
paulson@14496
   760
  by (cases z,
paulson@14496
   761
      simp add: of_int add minus int_eq_of_nat [symmetric] int_def diff_minus)
paulson@14496
   762
qed
paulson@14496
   763
paulson@14378
   764
paulson@14378
   765
subsection{*The Set of Integers*}
paulson@14378
   766
paulson@14378
   767
constdefs
obua@14738
   768
   Ints  :: "'a::comm_ring_1 set"
paulson@14378
   769
    "Ints == range of_int"
paulson@14271
   770
paulson@14259
   771
paulson@14378
   772
syntax (xsymbols)
paulson@14378
   773
  Ints      :: "'a set"                   ("\<int>")
paulson@14378
   774
paulson@14378
   775
lemma Ints_0 [simp]: "0 \<in> Ints"
paulson@14479
   776
apply (simp add: Ints_def)
paulson@14479
   777
apply (rule range_eqI)
paulson@14378
   778
apply (rule of_int_0 [symmetric])
paulson@14378
   779
done
paulson@14378
   780
paulson@14378
   781
lemma Ints_1 [simp]: "1 \<in> Ints"
paulson@14479
   782
apply (simp add: Ints_def)
paulson@14479
   783
apply (rule range_eqI)
paulson@14378
   784
apply (rule of_int_1 [symmetric])
paulson@14378
   785
done
paulson@14378
   786
paulson@14378
   787
lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints"
paulson@14479
   788
apply (auto simp add: Ints_def)
paulson@14479
   789
apply (rule range_eqI)
paulson@14378
   790
apply (rule of_int_add [symmetric])
paulson@14378
   791
done
paulson@14378
   792
paulson@14378
   793
lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints"
paulson@14479
   794
apply (auto simp add: Ints_def)
paulson@14479
   795
apply (rule range_eqI)
paulson@14378
   796
apply (rule of_int_minus [symmetric])
paulson@14378
   797
done
paulson@14378
   798
paulson@14378
   799
lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints"
paulson@14479
   800
apply (auto simp add: Ints_def)
paulson@14479
   801
apply (rule range_eqI)
paulson@14378
   802
apply (rule of_int_diff [symmetric])
paulson@14378
   803
done
paulson@14378
   804
paulson@14378
   805
lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints"
paulson@14479
   806
apply (auto simp add: Ints_def)
paulson@14479
   807
apply (rule range_eqI)
paulson@14378
   808
apply (rule of_int_mult [symmetric])
paulson@14378
   809
done
paulson@14378
   810
paulson@14378
   811
text{*Collapse nested embeddings*}
paulson@14378
   812
lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
paulson@14479
   813
by (induct n, auto)
paulson@14378
   814
paulson@15013
   815
lemma of_int_int_eq [simp]: "of_int (int n) = of_nat n"
paulson@14479
   816
by (simp add: int_eq_of_nat)
paulson@14341
   817
paulson@14378
   818
lemma Ints_cases [case_names of_int, cases set: Ints]:
paulson@14378
   819
  "q \<in> \<int> ==> (!!z. q = of_int z ==> C) ==> C"
paulson@14479
   820
proof (simp add: Ints_def)
paulson@14378
   821
  assume "!!z. q = of_int z ==> C"
paulson@14378
   822
  assume "q \<in> range of_int" thus C ..
paulson@14378
   823
qed
paulson@14378
   824
paulson@14378
   825
lemma Ints_induct [case_names of_int, induct set: Ints]:
paulson@14378
   826
  "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q"
paulson@14378
   827
  by (rule Ints_cases) auto
paulson@14378
   828
paulson@14378
   829
paulson@14387
   830
(* int (Suc n) = 1 + int n *)
paulson@14387
   831
declare int_Suc [simp]
paulson@14387
   832
paulson@14387
   833
text{*Simplification of @{term "x-y < 0"}, etc.*}
paulson@14387
   834
declare less_iff_diff_less_0 [symmetric, simp]
paulson@14387
   835
declare eq_iff_diff_eq_0 [symmetric, simp]
paulson@14387
   836
declare le_iff_diff_le_0 [symmetric, simp]
paulson@14387
   837
paulson@14378
   838
paulson@14430
   839
subsection{*More Properties of @{term setsum} and  @{term setprod}*}
paulson@14430
   840
paulson@14430
   841
text{*By Jeremy Avigad*}
paulson@14430
   842
paulson@14430
   843
paulson@14430
   844
lemma setsum_of_nat: "of_nat (setsum f A) = setsum (of_nat \<circ> f) A"
paulson@14430
   845
  apply (case_tac "finite A")
paulson@14430
   846
  apply (erule finite_induct, auto)
paulson@14430
   847
  apply (simp add: setsum_def)
paulson@14430
   848
  done
paulson@14430
   849
paulson@14430
   850
lemma setsum_of_int: "of_int (setsum f A) = setsum (of_int \<circ> f) A"
paulson@14430
   851
  apply (case_tac "finite A")
paulson@14430
   852
  apply (erule finite_induct, auto)
paulson@14430
   853
  apply (simp add: setsum_def)
paulson@14430
   854
  done
paulson@14430
   855
paulson@14430
   856
lemma int_setsum: "int (setsum f A) = setsum (int \<circ> f) A"
paulson@14430
   857
  by (subst int_eq_of_nat, rule setsum_of_nat)
paulson@14430
   858
paulson@14430
   859
lemma setprod_of_nat: "of_nat (setprod f A) = setprod (of_nat \<circ> f) A"
paulson@14430
   860
  apply (case_tac "finite A")
paulson@14430
   861
  apply (erule finite_induct, auto)
paulson@14430
   862
  apply (simp add: setprod_def)
paulson@14430
   863
  done
paulson@14430
   864
paulson@14430
   865
lemma setprod_of_int: "of_int (setprod f A) = setprod (of_int \<circ> f) A"
paulson@14430
   866
  apply (case_tac "finite A")
paulson@14430
   867
  apply (erule finite_induct, auto)
paulson@14430
   868
  apply (simp add: setprod_def)
paulson@14430
   869
  done
paulson@14430
   870
paulson@14430
   871
lemma int_setprod: "int (setprod f A) = setprod (int \<circ> f) A"
paulson@14430
   872
  by (subst int_eq_of_nat, rule setprod_of_nat)
paulson@14430
   873
paulson@15047
   874
lemma setsum_constant [simp]: "finite A ==> (\<Sum>x \<in> A. y) = of_nat(card A) * y"
paulson@14430
   875
  apply (erule finite_induct)
paulson@14430
   876
  apply (auto simp add: ring_distrib add_ac)
paulson@14430
   877
  done
paulson@14430
   878
paulson@14430
   879
lemma setprod_nonzero_nat:
paulson@14430
   880
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0"
paulson@14430
   881
  by (rule setprod_nonzero, auto)
paulson@14430
   882
paulson@14430
   883
lemma setprod_zero_eq_nat:
paulson@14430
   884
    "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)"
paulson@14430
   885
  by (rule setprod_zero_eq, auto)
paulson@14430
   886
paulson@14430
   887
lemma setprod_nonzero_int:
paulson@14430
   888
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0"
paulson@14430
   889
  by (rule setprod_nonzero, auto)
paulson@14430
   890
paulson@14430
   891
lemma setprod_zero_eq_int:
paulson@14430
   892
    "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)"
paulson@14430
   893
  by (rule setprod_zero_eq, auto)
paulson@14430
   894
paulson@14430
   895
paulson@14479
   896
text{*Now we replace the case analysis rule by a more conventional one:
paulson@14479
   897
whether an integer is negative or not.*}
paulson@14479
   898
paulson@14479
   899
lemma zless_iff_Suc_zadd:
paulson@14479
   900
    "(w < z) = (\<exists>n. z = w + int(Suc n))"
paulson@14479
   901
apply (cases z, cases w)
paulson@14479
   902
apply (auto simp add: le add int_def linorder_not_le [symmetric]) 
paulson@14479
   903
apply (rename_tac a b c d) 
paulson@14479
   904
apply (rule_tac x="a+d - Suc(c+b)" in exI) 
paulson@14479
   905
apply arith
paulson@14479
   906
done
paulson@14479
   907
paulson@14479
   908
lemma negD: "x<0 ==> \<exists>n. x = - (int (Suc n))"
paulson@14479
   909
apply (cases x)
paulson@14479
   910
apply (auto simp add: le minus Zero_int_def int_def order_less_le) 
paulson@14496
   911
apply (rule_tac x="y - Suc x" in exI, arith)
paulson@14479
   912
done
paulson@14479
   913
paulson@14479
   914
theorem int_cases [cases type: int, case_names nonneg neg]:
paulson@14479
   915
     "[|!! n. z = int n ==> P;  !! n. z =  - (int (Suc n)) ==> P |] ==> P"
paulson@14479
   916
apply (case_tac "z < 0", blast dest!: negD)
paulson@14479
   917
apply (simp add: linorder_not_less)
paulson@14479
   918
apply (blast dest: nat_0_le [THEN sym])
paulson@14479
   919
done
paulson@14479
   920
paulson@14479
   921
theorem int_induct [induct type: int, case_names nonneg neg]:
paulson@14479
   922
     "[|!! n. P (int n);  !!n. P (- (int (Suc n))) |] ==> P z"
paulson@14479
   923
  by (cases z) auto
paulson@14479
   924
paulson@14479
   925
paulson@15013
   926
lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z"
paulson@15013
   927
apply (cases z)
paulson@15013
   928
apply (simp_all add: not_zle_0_negative del: int_Suc)
paulson@15013
   929
done
paulson@15013
   930
paulson@15013
   931
paulson@14378
   932
(*Legacy ML bindings, but no longer the structure Int.*)
paulson@14259
   933
ML
paulson@14259
   934
{*
paulson@14378
   935
val zabs_def = thm "zabs_def"
paulson@14378
   936
paulson@14378
   937
val int_0 = thm "int_0";
paulson@14378
   938
val int_1 = thm "int_1";
paulson@14378
   939
val int_Suc0_eq_1 = thm "int_Suc0_eq_1";
paulson@14378
   940
val neg_eq_less_0 = thm "neg_eq_less_0";
paulson@14378
   941
val not_neg_eq_ge_0 = thm "not_neg_eq_ge_0";
paulson@14378
   942
val not_neg_0 = thm "not_neg_0";
paulson@14378
   943
val not_neg_1 = thm "not_neg_1";
paulson@14378
   944
val iszero_0 = thm "iszero_0";
paulson@14378
   945
val not_iszero_1 = thm "not_iszero_1";
paulson@14378
   946
val int_0_less_1 = thm "int_0_less_1";
paulson@14378
   947
val int_0_neq_1 = thm "int_0_neq_1";
paulson@14378
   948
val negative_zless = thm "negative_zless";
paulson@14378
   949
val negative_zle = thm "negative_zle";
paulson@14378
   950
val not_zle_0_negative = thm "not_zle_0_negative";
paulson@14378
   951
val not_int_zless_negative = thm "not_int_zless_negative";
paulson@14378
   952
val negative_eq_positive = thm "negative_eq_positive";
paulson@14378
   953
val zle_iff_zadd = thm "zle_iff_zadd";
paulson@14378
   954
val abs_int_eq = thm "abs_int_eq";
paulson@14378
   955
val abs_split = thm"abs_split";
paulson@14378
   956
val nat_int = thm "nat_int";
paulson@14378
   957
val nat_zminus_int = thm "nat_zminus_int";
paulson@14378
   958
val nat_zero = thm "nat_zero";
paulson@14378
   959
val not_neg_nat = thm "not_neg_nat";
paulson@14378
   960
val neg_nat = thm "neg_nat";
paulson@14378
   961
val zless_nat_eq_int_zless = thm "zless_nat_eq_int_zless";
paulson@14378
   962
val nat_0_le = thm "nat_0_le";
paulson@14378
   963
val nat_le_0 = thm "nat_le_0";
paulson@14378
   964
val zless_nat_conj = thm "zless_nat_conj";
paulson@14378
   965
val int_cases = thm "int_cases";
paulson@14378
   966
paulson@14259
   967
val int_def = thm "int_def";
paulson@14259
   968
val Zero_int_def = thm "Zero_int_def";
paulson@14259
   969
val One_int_def = thm "One_int_def";
paulson@14479
   970
val diff_int_def = thm "diff_int_def";
paulson@14259
   971
paulson@14259
   972
val inj_int = thm "inj_int";
paulson@14259
   973
val zminus_zminus = thm "zminus_zminus";
paulson@14259
   974
val zminus_0 = thm "zminus_0";
paulson@14259
   975
val zminus_zadd_distrib = thm "zminus_zadd_distrib";
paulson@14259
   976
val zadd_commute = thm "zadd_commute";
paulson@14259
   977
val zadd_assoc = thm "zadd_assoc";
paulson@14259
   978
val zadd_left_commute = thm "zadd_left_commute";
paulson@14259
   979
val zadd_ac = thms "zadd_ac";
paulson@14271
   980
val zmult_ac = thms "zmult_ac";
paulson@14259
   981
val zadd_int = thm "zadd_int";
paulson@14259
   982
val zadd_int_left = thm "zadd_int_left";
paulson@14259
   983
val int_Suc = thm "int_Suc";
paulson@14259
   984
val zadd_0 = thm "zadd_0";
paulson@14259
   985
val zadd_0_right = thm "zadd_0_right";
paulson@14259
   986
val zmult_zminus = thm "zmult_zminus";
paulson@14259
   987
val zmult_commute = thm "zmult_commute";
paulson@14259
   988
val zmult_assoc = thm "zmult_assoc";
paulson@14259
   989
val zadd_zmult_distrib = thm "zadd_zmult_distrib";
paulson@14259
   990
val zadd_zmult_distrib2 = thm "zadd_zmult_distrib2";
paulson@14259
   991
val zdiff_zmult_distrib = thm "zdiff_zmult_distrib";
paulson@14259
   992
val zdiff_zmult_distrib2 = thm "zdiff_zmult_distrib2";
paulson@14259
   993
val int_distrib = thms "int_distrib";
paulson@14259
   994
val zmult_int = thm "zmult_int";
paulson@14259
   995
val zmult_1 = thm "zmult_1";
paulson@14259
   996
val zmult_1_right = thm "zmult_1_right";
paulson@14259
   997
val int_int_eq = thm "int_int_eq";
paulson@14259
   998
val int_eq_0_conv = thm "int_eq_0_conv";
paulson@14259
   999
val zless_int = thm "zless_int";
paulson@14259
  1000
val int_less_0_conv = thm "int_less_0_conv";
paulson@14259
  1001
val zero_less_int_conv = thm "zero_less_int_conv";
paulson@14259
  1002
val zle_int = thm "zle_int";
paulson@14259
  1003
val zero_zle_int = thm "zero_zle_int";
paulson@14259
  1004
val int_le_0_conv = thm "int_le_0_conv";
paulson@14259
  1005
val zle_refl = thm "zle_refl";
paulson@14259
  1006
val zle_linear = thm "zle_linear";
paulson@14259
  1007
val zle_trans = thm "zle_trans";
paulson@14259
  1008
val zle_anti_sym = thm "zle_anti_sym";
paulson@14378
  1009
paulson@14378
  1010
val Ints_def = thm "Ints_def";
paulson@14378
  1011
val Nats_def = thm "Nats_def";
paulson@14378
  1012
paulson@14378
  1013
val of_nat_0 = thm "of_nat_0";
paulson@14378
  1014
val of_nat_Suc = thm "of_nat_Suc";
paulson@14378
  1015
val of_nat_1 = thm "of_nat_1";
paulson@14378
  1016
val of_nat_add = thm "of_nat_add";
paulson@14378
  1017
val of_nat_mult = thm "of_nat_mult";
paulson@14378
  1018
val zero_le_imp_of_nat = thm "zero_le_imp_of_nat";
paulson@14378
  1019
val less_imp_of_nat_less = thm "less_imp_of_nat_less";
paulson@14378
  1020
val of_nat_less_imp_less = thm "of_nat_less_imp_less";
paulson@14378
  1021
val of_nat_less_iff = thm "of_nat_less_iff";
paulson@14378
  1022
val of_nat_le_iff = thm "of_nat_le_iff";
paulson@14378
  1023
val of_nat_eq_iff = thm "of_nat_eq_iff";
paulson@14378
  1024
val Nats_0 = thm "Nats_0";
paulson@14378
  1025
val Nats_1 = thm "Nats_1";
paulson@14378
  1026
val Nats_add = thm "Nats_add";
paulson@14378
  1027
val Nats_mult = thm "Nats_mult";
paulson@14387
  1028
val int_eq_of_nat = thm"int_eq_of_nat";
paulson@14378
  1029
val of_int = thm "of_int";
paulson@14378
  1030
val of_int_0 = thm "of_int_0";
paulson@14378
  1031
val of_int_1 = thm "of_int_1";
paulson@14378
  1032
val of_int_add = thm "of_int_add";
paulson@14378
  1033
val of_int_minus = thm "of_int_minus";
paulson@14378
  1034
val of_int_diff = thm "of_int_diff";
paulson@14378
  1035
val of_int_mult = thm "of_int_mult";
paulson@14378
  1036
val of_int_le_iff = thm "of_int_le_iff";
paulson@14378
  1037
val of_int_less_iff = thm "of_int_less_iff";
paulson@14378
  1038
val of_int_eq_iff = thm "of_int_eq_iff";
paulson@14378
  1039
val Ints_0 = thm "Ints_0";
paulson@14378
  1040
val Ints_1 = thm "Ints_1";
paulson@14378
  1041
val Ints_add = thm "Ints_add";
paulson@14378
  1042
val Ints_minus = thm "Ints_minus";
paulson@14378
  1043
val Ints_diff = thm "Ints_diff";
paulson@14378
  1044
val Ints_mult = thm "Ints_mult";
paulson@14378
  1045
val of_int_of_nat_eq = thm"of_int_of_nat_eq";
paulson@14378
  1046
val Ints_cases = thm "Ints_cases";
paulson@14378
  1047
val Ints_induct = thm "Ints_induct";
paulson@14259
  1048
*}
paulson@14259
  1049
paulson@5508
  1050
end