src/HOL/Integ/IntDef.thy
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 18704 2c86ced392a8
child 18757 f0d901bc0686
permissions -rw-r--r--
setup: theory -> theory;
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
paulson@15300
    11
imports Equiv_Relations 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@15413
    78
text{*Reduces equality on abstractions to equality on representatives:
paulson@14496
    79
  @{term "\<lbrakk>x \<in> Integ; y \<in> Integ\<rbrakk> \<Longrightarrow> (Abs_Integ x = Abs_Integ y) = (x=y)"} *}
paulson@15413
    80
declare Abs_Integ_inject [simp]  Abs_Integ_inverse [simp]
paulson@14259
    81
paulson@14479
    82
text{*Case analysis on the representation of an integer as an equivalence
paulson@14485
    83
      class of pairs of naturals.*}
paulson@14479
    84
lemma eq_Abs_Integ [case_names Abs_Integ, cases type: int]:
paulson@14479
    85
     "(!!x y. z = Abs_Integ(intrel``{(x,y)}) ==> P) ==> P"
paulson@15413
    86
apply (rule Abs_Integ_cases [of z]) 
paulson@15413
    87
apply (auto simp add: Integ_def quotient_def) 
paulson@14479
    88
done
paulson@14259
    89
paulson@14479
    90
paulson@14479
    91
subsubsection{*@{term int}: Embedding the Naturals into the Integers*}
paulson@14259
    92
paulson@14259
    93
lemma inj_int: "inj int"
paulson@14479
    94
by (simp add: inj_on_def int_def)
paulson@14259
    95
paulson@14341
    96
lemma int_int_eq [iff]: "(int m = int n) = (m = n)"
paulson@14341
    97
by (fast elim!: inj_int [THEN injD])
paulson@14341
    98
paulson@14341
    99
paulson@14479
   100
subsubsection{*Integer Unary Negation*}
paulson@14259
   101
paulson@14479
   102
lemma minus: "- Abs_Integ(intrel``{(x,y)}) = Abs_Integ(intrel `` {(y,x)})"
paulson@14479
   103
proof -
paulson@15169
   104
  have "(\<lambda>(x,y). intrel``{(y,x)}) respects intrel"
paulson@14479
   105
    by (simp add: congruent_def) 
paulson@14479
   106
  thus ?thesis
paulson@14479
   107
    by (simp add: minus_int_def UN_equiv_class [OF equiv_intrel])
paulson@14479
   108
qed
paulson@14259
   109
paulson@14479
   110
lemma zminus_zminus: "- (- z) = (z::int)"
paulson@14479
   111
by (cases z, simp add: minus)
paulson@14259
   112
paulson@14479
   113
lemma zminus_0: "- 0 = (0::int)"
paulson@14479
   114
by (simp add: int_def Zero_int_def minus)
paulson@14259
   115
paulson@14259
   116
paulson@14479
   117
subsection{*Integer Addition*}
paulson@14259
   118
paulson@14479
   119
lemma add:
paulson@14479
   120
     "Abs_Integ (intrel``{(x,y)}) + Abs_Integ (intrel``{(u,v)}) =
paulson@14479
   121
      Abs_Integ (intrel``{(x+u, y+v)})"
paulson@14479
   122
proof -
paulson@15169
   123
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). intrel `` {(x+u, y+v)}) w) z) 
paulson@15169
   124
        respects2 intrel"
paulson@14479
   125
    by (simp add: congruent2_def)
paulson@14479
   126
  thus ?thesis
paulson@14479
   127
    by (simp add: add_int_def UN_UN_split_split_eq
paulson@14658
   128
                  UN_equiv_class2 [OF equiv_intrel equiv_intrel])
paulson@14479
   129
qed
paulson@14259
   130
paulson@14479
   131
lemma zminus_zadd_distrib: "- (z + w) = (- z) + (- w::int)"
paulson@14479
   132
by (cases z, cases w, simp add: minus add)
paulson@14259
   133
paulson@14259
   134
lemma zadd_commute: "(z::int) + w = w + z"
paulson@14479
   135
by (cases z, cases w, simp add: add_ac add)
paulson@14259
   136
paulson@14259
   137
lemma zadd_assoc: "((z1::int) + z2) + z3 = z1 + (z2 + z3)"
paulson@14479
   138
by (cases z1, cases z2, cases z3, simp add: add add_assoc)
paulson@14259
   139
paulson@14259
   140
(*For AC rewriting*)
paulson@14271
   141
lemma zadd_left_commute: "x + (y + z) = y + ((x + z)  ::int)"
paulson@14259
   142
  apply (rule mk_left_commute [of "op +"])
paulson@14259
   143
  apply (rule zadd_assoc)
paulson@14259
   144
  apply (rule zadd_commute)
paulson@14259
   145
  done
paulson@14259
   146
paulson@14259
   147
lemmas zadd_ac = zadd_assoc zadd_commute zadd_left_commute
paulson@14259
   148
obua@14738
   149
lemmas zmult_ac = OrderedGroup.mult_ac
paulson@14271
   150
paulson@14259
   151
lemma zadd_int: "(int m) + (int n) = int (m + n)"
paulson@14479
   152
by (simp add: int_def add)
paulson@14259
   153
paulson@14259
   154
lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z"
paulson@14259
   155
by (simp add: zadd_int zadd_assoc [symmetric])
paulson@14259
   156
paulson@14259
   157
lemma int_Suc: "int (Suc m) = 1 + (int m)"
paulson@14259
   158
by (simp add: One_int_def zadd_int)
paulson@14259
   159
obua@14738
   160
(*also for the instance declaration int :: comm_monoid_add*)
paulson@14479
   161
lemma zadd_0: "(0::int) + z = z"
paulson@14479
   162
apply (simp add: Zero_int_def int_def)
paulson@14479
   163
apply (cases z, simp add: add)
paulson@14259
   164
done
paulson@14259
   165
paulson@14479
   166
lemma zadd_0_right: "z + (0::int) = z"
paulson@14479
   167
by (rule trans [OF zadd_commute zadd_0])
paulson@14259
   168
paulson@14479
   169
lemma zadd_zminus_inverse2: "(- z) + z = (0::int)"
paulson@14479
   170
by (cases z, simp add: int_def Zero_int_def minus add)
paulson@14259
   171
paulson@14259
   172
paulson@14479
   173
subsection{*Integer Multiplication*}
paulson@14259
   174
paulson@14378
   175
text{*Congruence property for multiplication*}
paulson@14479
   176
lemma mult_congruent2:
paulson@15169
   177
     "(%p1 p2. (%(x,y). (%(u,v). intrel``{(x*u + y*v, x*v + y*u)}) p2) p1)
paulson@15169
   178
      respects2 intrel"
paulson@14259
   179
apply (rule equiv_intrel [THEN congruent2_commuteI])
paulson@14532
   180
 apply (force simp add: mult_ac, clarify) 
paulson@14532
   181
apply (simp add: congruent_def mult_ac)  
paulson@14479
   182
apply (rename_tac u v w x y z)
paulson@14479
   183
apply (subgoal_tac "u*y + x*y = w*y + v*y  &  u*z + x*z = w*z + v*z")
nipkow@16733
   184
apply (simp add: mult_ac)
paulson@14259
   185
apply (simp add: add_mult_distrib [symmetric])
paulson@14259
   186
done
paulson@14259
   187
paulson@14532
   188
paulson@14479
   189
lemma mult:
paulson@14479
   190
     "Abs_Integ((intrel``{(x,y)})) * Abs_Integ((intrel``{(u,v)})) =
paulson@14479
   191
      Abs_Integ(intrel `` {(x*u + y*v, x*v + y*u)})"
paulson@14479
   192
by (simp add: mult_int_def UN_UN_split_split_eq mult_congruent2
paulson@14658
   193
              UN_equiv_class2 [OF equiv_intrel equiv_intrel])
paulson@14259
   194
paulson@14259
   195
lemma zmult_zminus: "(- z) * w = - (z * (w::int))"
paulson@14479
   196
by (cases z, cases w, simp add: minus mult add_ac)
paulson@14259
   197
paulson@14259
   198
lemma zmult_commute: "(z::int) * w = w * z"
paulson@14479
   199
by (cases z, cases w, simp add: mult add_ac mult_ac)
paulson@14259
   200
paulson@14259
   201
lemma zmult_assoc: "((z1::int) * z2) * z3 = z1 * (z2 * z3)"
paulson@14479
   202
by (cases z1, cases z2, cases z3, simp add: mult add_mult_distrib2 mult_ac)
paulson@14259
   203
paulson@14259
   204
lemma zadd_zmult_distrib: "((z1::int) + z2) * w = (z1 * w) + (z2 * w)"
paulson@14479
   205
by (cases z1, cases z2, cases w, simp add: add mult add_mult_distrib2 mult_ac)
paulson@14259
   206
paulson@14259
   207
lemma zadd_zmult_distrib2: "(w::int) * (z1 + z2) = (w * z1) + (w * z2)"
paulson@14259
   208
by (simp add: zmult_commute [of w] zadd_zmult_distrib)
paulson@14259
   209
paulson@14259
   210
lemma zdiff_zmult_distrib: "((z1::int) - z2) * w = (z1 * w) - (z2 * w)"
paulson@14496
   211
by (simp add: diff_int_def zadd_zmult_distrib zmult_zminus)
paulson@14259
   212
paulson@14259
   213
lemma zdiff_zmult_distrib2: "(w::int) * (z1 - z2) = (w * z1) - (w * z2)"
paulson@14259
   214
by (simp add: zmult_commute [of w] zdiff_zmult_distrib)
paulson@14259
   215
paulson@14259
   216
lemmas int_distrib =
paulson@14479
   217
  zadd_zmult_distrib zadd_zmult_distrib2
paulson@14259
   218
  zdiff_zmult_distrib zdiff_zmult_distrib2
paulson@14259
   219
paulson@16413
   220
lemma int_mult: "int (m * n) = (int m) * (int n)"
paulson@14479
   221
by (simp add: int_def mult)
paulson@14259
   222
paulson@16413
   223
text{*Compatibility binding*}
paulson@16413
   224
lemmas zmult_int = int_mult [symmetric]
paulson@16413
   225
paulson@14479
   226
lemma zmult_1: "(1::int) * z = z"
paulson@14479
   227
by (cases z, simp add: One_int_def int_def mult)
paulson@14259
   228
paulson@14479
   229
lemma zmult_1_right: "z * (1::int) = z"
paulson@14259
   230
by (rule trans [OF zmult_commute zmult_1])
paulson@14259
   231
paulson@14259
   232
nipkow@14740
   233
text{*The integers form a @{text comm_ring_1}*}
obua@14738
   234
instance int :: comm_ring_1
paulson@14341
   235
proof
paulson@14341
   236
  fix i j k :: int
paulson@14341
   237
  show "(i + j) + k = i + (j + k)" by (simp add: zadd_assoc)
paulson@14341
   238
  show "i + j = j + i" by (simp add: zadd_commute)
paulson@14479
   239
  show "0 + i = i" by (rule zadd_0)
paulson@14479
   240
  show "- i + i = 0" by (rule zadd_zminus_inverse2)
paulson@14479
   241
  show "i - j = i + (-j)" by (simp add: diff_int_def)
paulson@14341
   242
  show "(i * j) * k = i * (j * k)" by (rule zmult_assoc)
paulson@14341
   243
  show "i * j = j * i" by (rule zmult_commute)
paulson@14479
   244
  show "1 * i = i" by (rule zmult_1) 
paulson@14341
   245
  show "(i + j) * k = i * k + j * k" by (simp add: int_distrib)
paulson@14479
   246
  show "0 \<noteq> (1::int)"
paulson@14348
   247
    by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
paulson@14341
   248
qed
paulson@14341
   249
paulson@14341
   250
paulson@14378
   251
subsection{*The @{text "\<le>"} Ordering*}
paulson@14378
   252
paulson@14479
   253
lemma le:
paulson@14479
   254
  "(Abs_Integ(intrel``{(x,y)}) \<le> Abs_Integ(intrel``{(u,v)})) = (x+v \<le> u+y)"
paulson@14479
   255
by (force simp add: le_int_def)
paulson@14378
   256
paulson@14378
   257
lemma zle_refl: "w \<le> (w::int)"
paulson@14479
   258
by (cases w, simp add: le)
paulson@14378
   259
paulson@14378
   260
lemma zle_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::int)"
paulson@14479
   261
by (cases i, cases j, cases k, simp add: le)
paulson@14378
   262
paulson@14378
   263
lemma zle_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::int)"
paulson@14479
   264
by (cases w, cases z, simp add: le)
paulson@14378
   265
paulson@14378
   266
(* Axiom 'order_less_le' of class 'order': *)
paulson@14378
   267
lemma zless_le: "((w::int) < z) = (w \<le> z & w \<noteq> z)"
paulson@14479
   268
by (simp add: less_int_def)
paulson@14378
   269
paulson@14378
   270
instance int :: order
wenzelm@14691
   271
  by intro_classes
wenzelm@14691
   272
    (assumption |
wenzelm@14691
   273
      rule zle_refl zle_trans zle_anti_sym zless_le)+
paulson@14378
   274
paulson@14378
   275
(* Axiom 'linorder_linear' of class 'linorder': *)
paulson@14378
   276
lemma zle_linear: "(z::int) \<le> w | w \<le> z"
wenzelm@14691
   277
by (cases z, cases w) (simp add: le linorder_linear)
paulson@14378
   278
paulson@14378
   279
instance int :: linorder
wenzelm@14691
   280
  by intro_classes (rule zle_linear)
paulson@14378
   281
paulson@14378
   282
paulson@14378
   283
lemmas zless_linear = linorder_less_linear [where 'a = int]
nipkow@15921
   284
lemmas linorder_neqE_int = linorder_neqE[where 'a = int]
paulson@14378
   285
paulson@14378
   286
paulson@14378
   287
lemma int_eq_0_conv [simp]: "(int n = 0) = (n = 0)"
paulson@14378
   288
by (simp add: Zero_int_def)
paulson@14259
   289
paulson@14259
   290
lemma zless_int [simp]: "(int m < int n) = (m<n)"
paulson@14479
   291
by (simp add: le add int_def linorder_not_le [symmetric]) 
paulson@14259
   292
paulson@14259
   293
lemma int_less_0_conv [simp]: "~ (int k < 0)"
paulson@14259
   294
by (simp add: Zero_int_def)
paulson@14259
   295
paulson@14259
   296
lemma zero_less_int_conv [simp]: "(0 < int n) = (0 < n)"
paulson@14259
   297
by (simp add: Zero_int_def)
paulson@14259
   298
paulson@14341
   299
lemma int_0_less_1: "0 < (1::int)"
paulson@14341
   300
by (simp only: Zero_int_def One_int_def One_nat_def zless_int)
paulson@14259
   301
paulson@14341
   302
lemma int_0_neq_1 [simp]: "0 \<noteq> (1::int)"
paulson@14341
   303
by (simp only: Zero_int_def One_int_def One_nat_def int_int_eq)
paulson@14341
   304
paulson@14378
   305
lemma zle_int [simp]: "(int m \<le> int n) = (m\<le>n)"
paulson@14378
   306
by (simp add: linorder_not_less [symmetric])
paulson@14259
   307
paulson@14378
   308
lemma zero_zle_int [simp]: "(0 \<le> int n)"
paulson@14378
   309
by (simp add: Zero_int_def)
paulson@14259
   310
paulson@14378
   311
lemma int_le_0_conv [simp]: "(int n \<le> 0) = (n = 0)"
paulson@14378
   312
by (simp add: Zero_int_def)
paulson@14378
   313
paulson@14378
   314
lemma int_0 [simp]: "int 0 = (0::int)"
paulson@14259
   315
by (simp add: Zero_int_def)
paulson@14259
   316
paulson@14378
   317
lemma int_1 [simp]: "int 1 = 1"
paulson@14378
   318
by (simp add: One_int_def)
paulson@14378
   319
paulson@14378
   320
lemma int_Suc0_eq_1: "int (Suc 0) = 1"
paulson@14378
   321
by (simp add: One_int_def One_nat_def)
paulson@14378
   322
paulson@14479
   323
paulson@14378
   324
subsection{*Monotonicity results*}
paulson@14378
   325
paulson@14479
   326
lemma zadd_left_mono: "i \<le> j ==> k + i \<le> k + (j::int)"
paulson@14479
   327
by (cases i, cases j, cases k, simp add: le add)
paulson@14378
   328
paulson@14479
   329
lemma zadd_strict_right_mono: "i < j ==> i + k < j + (k::int)"
paulson@14479
   330
apply (cases i, cases j, cases k)
paulson@14479
   331
apply (simp add: linorder_not_le [where 'a = int, symmetric]
paulson@14479
   332
                 linorder_not_le [where 'a = nat]  le add)
paulson@14378
   333
done
paulson@14378
   334
paulson@14378
   335
lemma zadd_zless_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::int)"
paulson@14479
   336
by (rule order_less_le_trans [OF zadd_strict_right_mono zadd_left_mono])
paulson@14378
   337
paulson@14378
   338
paulson@14378
   339
subsection{*Strict Monotonicity of Multiplication*}
paulson@14378
   340
paulson@14378
   341
text{*strict, in 1st argument; proof is by induction on k>0*}
paulson@15251
   342
lemma zmult_zless_mono2_lemma:
paulson@15251
   343
     "i<j ==> 0<k ==> int k * i < int k * j"
paulson@15251
   344
apply (induct "k", simp)
paulson@14378
   345
apply (simp add: int_Suc)
paulson@15251
   346
apply (case_tac "k=0")
paulson@14378
   347
apply (simp_all add: zadd_zmult_distrib int_Suc0_eq_1 order_le_less)
paulson@14378
   348
apply (simp add: zadd_zless_mono int_Suc0_eq_1 order_le_less)
paulson@14378
   349
done
paulson@14259
   350
paulson@14378
   351
lemma zero_le_imp_eq_int: "0 \<le> k ==> \<exists>n. k = int n"
paulson@14479
   352
apply (cases k)
paulson@14479
   353
apply (auto simp add: le add int_def Zero_int_def)
paulson@14479
   354
apply (rule_tac x="x-y" in exI, simp)
paulson@14378
   355
done
paulson@14378
   356
paulson@14378
   357
lemma zmult_zless_mono2: "[| i<j;  (0::int) < k |] ==> k*i < k*j"
paulson@14479
   358
apply (frule order_less_imp_le [THEN zero_le_imp_eq_int])
paulson@14479
   359
apply (auto simp add: zmult_zless_mono2_lemma)
paulson@14378
   360
done
paulson@14378
   361
paulson@14378
   362
paulson@14378
   363
defs (overloaded)
paulson@14378
   364
    zabs_def:  "abs(i::int) == if i < 0 then -i else i"
paulson@14378
   365
paulson@14378
   366
nipkow@14740
   367
text{*The integers form an ordered @{text comm_ring_1}*}
obua@14738
   368
instance int :: ordered_idom
paulson@14378
   369
proof
paulson@14378
   370
  fix i j k :: int
paulson@14378
   371
  show "i \<le> j ==> k + i \<le> k + j" by (rule zadd_left_mono)
paulson@14378
   372
  show "i < j ==> 0 < k ==> k * i < k * j" by (rule zmult_zless_mono2)
paulson@14378
   373
  show "\<bar>i\<bar> = (if i < 0 then -i else i)" by (simp only: zabs_def)
paulson@14378
   374
qed
paulson@14378
   375
paulson@14378
   376
paulson@14479
   377
lemma zless_imp_add1_zle: "w<z ==> w + (1::int) \<le> z"
paulson@14479
   378
apply (cases w, cases z) 
paulson@14479
   379
apply (simp add: linorder_not_le [symmetric] le int_def add One_int_def)
paulson@14479
   380
done
paulson@14479
   381
paulson@14378
   382
subsection{*Magnitide of an Integer, as a Natural Number: @{term nat}*}
paulson@14378
   383
paulson@14378
   384
constdefs
paulson@14378
   385
   nat  :: "int => nat"
paulson@14532
   386
    "nat z == contents (\<Union>(x,y) \<in> Rep_Integ z. { x-y })"
paulson@14479
   387
paulson@14479
   388
lemma nat: "nat (Abs_Integ (intrel``{(x,y)})) = x-y"
paulson@14479
   389
proof -
paulson@15169
   390
  have "(\<lambda>(x,y). {x-y}) respects intrel"
paulson@14479
   391
    by (simp add: congruent_def, arith) 
paulson@14479
   392
  thus ?thesis
paulson@14479
   393
    by (simp add: nat_def UN_equiv_class [OF equiv_intrel])
paulson@14479
   394
qed
paulson@14378
   395
paulson@14378
   396
lemma nat_int [simp]: "nat(int n) = n"
paulson@14479
   397
by (simp add: nat int_def) 
paulson@14378
   398
paulson@14378
   399
lemma nat_zero [simp]: "nat 0 = 0"
paulson@14479
   400
by (simp only: Zero_int_def nat_int)
paulson@14378
   401
paulson@14479
   402
lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)"
paulson@14479
   403
by (cases z, simp add: nat le int_def Zero_int_def)
paulson@14479
   404
paulson@14479
   405
corollary nat_0_le: "0 \<le> z ==> int (nat z) = z"
paulson@15413
   406
by simp
paulson@14259
   407
paulson@14378
   408
lemma nat_le_0 [simp]: "z \<le> 0 ==> nat z = 0"
paulson@14479
   409
by (cases z, simp add: nat le int_def Zero_int_def)
paulson@14479
   410
paulson@14479
   411
lemma nat_le_eq_zle: "0 < w | 0 \<le> z ==> (nat w \<le> nat z) = (w\<le>z)"
paulson@14479
   412
apply (cases w, cases z) 
paulson@14479
   413
apply (simp add: nat le linorder_not_le [symmetric] int_def Zero_int_def, arith)
paulson@14479
   414
done
paulson@14378
   415
paulson@14378
   416
text{*An alternative condition is @{term "0 \<le> w"} *}
paulson@14479
   417
corollary nat_mono_iff: "0 < z ==> (nat w < nat z) = (w < z)"
paulson@14479
   418
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
paulson@14479
   419
paulson@14479
   420
corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)"
paulson@14479
   421
by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) 
paulson@14479
   422
paulson@14479
   423
lemma zless_nat_conj: "(nat w < nat z) = (0 < z & w < z)"
paulson@14479
   424
apply (cases w, cases z) 
paulson@14479
   425
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
paulson@14378
   426
done
paulson@14378
   427
paulson@14479
   428
lemma nonneg_eq_int: "[| 0 \<le> z;  !!m. z = int m ==> P |] ==> P"
paulson@14479
   429
by (blast dest: nat_0_le sym)
paulson@14479
   430
paulson@14479
   431
lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)"
paulson@14479
   432
by (cases w, simp add: nat le int_def Zero_int_def, arith)
paulson@14479
   433
paulson@14479
   434
corollary nat_eq_iff2: "(m = nat w) = (if 0 \<le> w then w = int m else m=0)"
paulson@14479
   435
by (simp only: eq_commute [of m] nat_eq_iff) 
paulson@14479
   436
paulson@14479
   437
lemma nat_less_iff: "0 \<le> w ==> (nat w < m) = (w < int m)"
paulson@14479
   438
apply (cases w)
paulson@14479
   439
apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith)
paulson@14378
   440
done
paulson@14378
   441
paulson@14479
   442
lemma int_eq_iff: "(int m = z) = (m = nat z & 0 \<le> z)"
paulson@14479
   443
by (auto simp add: nat_eq_iff2)
paulson@14479
   444
paulson@14479
   445
lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)"
paulson@14479
   446
by (insert zless_nat_conj [of 0], auto)
paulson@14479
   447
paulson@14479
   448
lemma nat_add_distrib:
paulson@14479
   449
     "[| (0::int) \<le> z;  0 \<le> z' |] ==> nat (z+z') = nat z + nat z'"
paulson@14479
   450
by (cases z, cases z', simp add: nat add le int_def Zero_int_def)
paulson@14479
   451
paulson@14479
   452
lemma nat_diff_distrib:
paulson@14479
   453
     "[| (0::int) \<le> z';  z' \<le> z |] ==> nat (z-z') = nat z - nat z'"
paulson@14479
   454
by (cases z, cases z', 
paulson@14479
   455
    simp add: nat add minus diff_minus le int_def Zero_int_def)
paulson@14479
   456
paulson@14479
   457
paulson@14479
   458
lemma nat_zminus_int [simp]: "nat (- (int n)) = 0"
paulson@14479
   459
by (simp add: int_def minus nat Zero_int_def) 
paulson@14479
   460
paulson@14479
   461
lemma zless_nat_eq_int_zless: "(m < nat z) = (int m < z)"
paulson@14479
   462
by (cases z, simp add: nat le int_def  linorder_not_le [symmetric], arith)
paulson@14479
   463
paulson@14378
   464
paulson@14378
   465
subsection{*Lemmas about the Function @{term int} and Orderings*}
paulson@14378
   466
paulson@14378
   467
lemma negative_zless_0: "- (int (Suc n)) < 0"
paulson@14479
   468
by (simp add: order_less_le)
paulson@14378
   469
paulson@14378
   470
lemma negative_zless [iff]: "- (int (Suc n)) < int m"
paulson@14378
   471
by (rule negative_zless_0 [THEN order_less_le_trans], simp)
paulson@14378
   472
paulson@14378
   473
lemma negative_zle_0: "- int n \<le> 0"
paulson@14378
   474
by (simp add: minus_le_iff)
paulson@14378
   475
paulson@14378
   476
lemma negative_zle [iff]: "- int n \<le> int m"
paulson@14378
   477
by (rule order_trans [OF negative_zle_0 zero_zle_int])
paulson@14378
   478
paulson@14378
   479
lemma not_zle_0_negative [simp]: "~ (0 \<le> - (int (Suc n)))"
paulson@14378
   480
by (subst le_minus_iff, simp)
paulson@14378
   481
paulson@14378
   482
lemma int_zle_neg: "(int n \<le> - int m) = (n = 0 & m = 0)"
paulson@14479
   483
by (simp add: int_def le minus Zero_int_def) 
paulson@14259
   484
paulson@14378
   485
lemma not_int_zless_negative [simp]: "~ (int n < - int m)"
paulson@14378
   486
by (simp add: linorder_not_less)
paulson@14378
   487
paulson@14378
   488
lemma negative_eq_positive [simp]: "(- int n = int m) = (n = 0 & m = 0)"
paulson@14378
   489
by (force simp add: order_eq_iff [of "- int n"] int_zle_neg)
paulson@14378
   490
paulson@14378
   491
lemma zle_iff_zadd: "(w \<le> z) = (\<exists>n. z = w + int n)"
paulson@15413
   492
proof (cases w, cases z, simp add: le add int_def)
paulson@15413
   493
  fix a b c d
paulson@15413
   494
  assume "w = Abs_Integ (intrel `` {(a,b)})" "z = Abs_Integ (intrel `` {(c,d)})"
paulson@15413
   495
  show "(a+d \<le> c+b) = (\<exists>n. c+b = a+n+d)"
paulson@15413
   496
  proof
paulson@15413
   497
    assume "a + d \<le> c + b" 
paulson@15413
   498
    thus "\<exists>n. c + b = a + n + d" 
paulson@15413
   499
      by (auto intro!: exI [where x="c+b - (a+d)"])
paulson@15413
   500
  next    
paulson@15413
   501
    assume "\<exists>n. c + b = a + n + d" 
paulson@15413
   502
    then obtain n where "c + b = a + n + d" ..
paulson@15413
   503
    thus "a + d \<le> c + b" by arith
paulson@15413
   504
  qed
paulson@15413
   505
qed
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
paulson@14378
   565
subsection{*The Set of Natural Numbers*}
paulson@14378
   566
paulson@14378
   567
constdefs
obua@14738
   568
   Nats  :: "'a::comm_semiring_1_cancel set"
paulson@14378
   569
    "Nats == range of_nat"
paulson@14378
   570
paulson@14378
   571
syntax (xsymbols)    Nats :: "'a set"   ("\<nat>")
paulson@14378
   572
paulson@14378
   573
lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
paulson@14479
   574
by (simp add: Nats_def)
paulson@14378
   575
paulson@14378
   576
lemma Nats_0 [simp]: "0 \<in> Nats"
paulson@14479
   577
apply (simp add: Nats_def)
paulson@14479
   578
apply (rule range_eqI)
paulson@14378
   579
apply (rule of_nat_0 [symmetric])
paulson@14378
   580
done
paulson@14378
   581
paulson@14378
   582
lemma Nats_1 [simp]: "1 \<in> Nats"
paulson@14479
   583
apply (simp add: Nats_def)
paulson@14479
   584
apply (rule range_eqI)
paulson@14378
   585
apply (rule of_nat_1 [symmetric])
paulson@14378
   586
done
paulson@14378
   587
paulson@14378
   588
lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
paulson@14479
   589
apply (auto simp add: Nats_def)
paulson@14479
   590
apply (rule range_eqI)
paulson@14378
   591
apply (rule of_nat_add [symmetric])
paulson@14378
   592
done
paulson@14378
   593
paulson@14378
   594
lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
paulson@14479
   595
apply (auto simp add: Nats_def)
paulson@14479
   596
apply (rule range_eqI)
paulson@14378
   597
apply (rule of_nat_mult [symmetric])
paulson@14259
   598
done
paulson@14259
   599
paulson@14378
   600
text{*Agreement with the specific embedding for the integers*}
paulson@14378
   601
lemma int_eq_of_nat: "int = (of_nat :: nat => int)"
paulson@14378
   602
proof
paulson@14378
   603
  fix n
paulson@14479
   604
  show "int n = of_nat n"  by (induct n, simp_all add: int_Suc add_ac)
paulson@14378
   605
qed
paulson@14378
   606
paulson@14496
   607
lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
paulson@14496
   608
proof
paulson@14496
   609
  fix n
paulson@14496
   610
  show "of_nat n = id n"  by (induct n, simp_all)
paulson@14496
   611
qed
paulson@14496
   612
paulson@14378
   613
nipkow@14740
   614
subsection{*Embedding of the Integers into any @{text comm_ring_1}:
nipkow@14740
   615
@{term of_int}*}
paulson@14378
   616
paulson@14378
   617
constdefs
obua@14738
   618
   of_int :: "int => 'a::comm_ring_1"
paulson@14532
   619
   "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })"
paulson@14378
   620
paulson@14378
   621
paulson@14378
   622
lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j"
paulson@14496
   623
proof -
paulson@15169
   624
  have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel"
paulson@14496
   625
    by (simp add: congruent_def compare_rls of_nat_add [symmetric]
paulson@14496
   626
            del: of_nat_add) 
paulson@14496
   627
  thus ?thesis
paulson@14496
   628
    by (simp add: of_int_def UN_equiv_class [OF equiv_intrel])
paulson@14496
   629
qed
paulson@14378
   630
paulson@14378
   631
lemma of_int_0 [simp]: "of_int 0 = 0"
paulson@14378
   632
by (simp add: of_int Zero_int_def int_def)
paulson@14378
   633
paulson@14378
   634
lemma of_int_1 [simp]: "of_int 1 = 1"
paulson@14378
   635
by (simp add: of_int One_int_def int_def)
paulson@14378
   636
paulson@14378
   637
lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z"
paulson@14479
   638
by (cases w, cases z, simp add: compare_rls of_int add)
paulson@14378
   639
paulson@14378
   640
lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)"
paulson@14479
   641
by (cases z, simp add: compare_rls of_int minus)
paulson@14259
   642
paulson@14378
   643
lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z"
paulson@14378
   644
by (simp add: diff_minus)
paulson@14378
   645
paulson@14378
   646
lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z"
paulson@14479
   647
apply (cases w, cases z)
paulson@14479
   648
apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib
paulson@14479
   649
                 mult add_ac)
paulson@14378
   650
done
paulson@14378
   651
paulson@14378
   652
lemma of_int_le_iff [simp]:
obua@14738
   653
     "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)"
paulson@14479
   654
apply (cases w)
paulson@14479
   655
apply (cases z)
paulson@14479
   656
apply (simp add: compare_rls of_int le diff_int_def add minus
paulson@14479
   657
                 of_nat_add [symmetric]   del: of_nat_add)
paulson@14378
   658
done
paulson@14378
   659
paulson@14378
   660
text{*Special cases where either operand is zero*}
paulson@17085
   661
lemmas of_int_0_le_iff = of_int_le_iff [of 0, simplified]
paulson@17085
   662
lemmas of_int_le_0_iff = of_int_le_iff [of _ 0, simplified]
paulson@17085
   663
declare of_int_0_le_iff [simp]
paulson@17085
   664
declare of_int_le_0_iff [simp]
paulson@14259
   665
paulson@14378
   666
lemma of_int_less_iff [simp]:
obua@14738
   667
     "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)"
paulson@14378
   668
by (simp add: linorder_not_le [symmetric])
paulson@14378
   669
paulson@14378
   670
text{*Special cases where either operand is zero*}
paulson@17085
   671
lemmas of_int_0_less_iff = of_int_less_iff [of 0, simplified]
paulson@17085
   672
lemmas of_int_less_0_iff = of_int_less_iff [of _ 0, simplified]
paulson@17085
   673
declare of_int_0_less_iff [simp]
paulson@17085
   674
declare of_int_less_0_iff [simp]
paulson@14378
   675
nipkow@14740
   676
text{*The ordering on the @{text comm_ring_1} is necessary.
nipkow@14740
   677
 See @{text of_nat_eq_iff} above.*}
paulson@14378
   678
lemma of_int_eq_iff [simp]:
obua@14738
   679
     "(of_int w = (of_int z::'a::ordered_idom)) = (w = z)"
paulson@14479
   680
by (simp add: order_eq_iff)
paulson@14378
   681
paulson@14378
   682
text{*Special cases where either operand is zero*}
paulson@17085
   683
lemmas of_int_0_eq_iff = of_int_eq_iff [of 0, simplified]
paulson@17085
   684
lemmas of_int_eq_0_iff = of_int_eq_iff [of _ 0, simplified]
paulson@17085
   685
declare of_int_0_eq_iff [simp]
paulson@17085
   686
declare of_int_eq_0_iff [simp]
paulson@14378
   687
paulson@14496
   688
lemma of_int_eq_id [simp]: "of_int = (id :: int => int)"
paulson@14496
   689
proof
paulson@14496
   690
 fix z
paulson@14496
   691
 show "of_int z = id z"  
paulson@14496
   692
  by (cases z,
paulson@14496
   693
      simp add: of_int add minus int_eq_of_nat [symmetric] int_def diff_minus)
paulson@14496
   694
qed
paulson@14496
   695
paulson@14378
   696
paulson@14378
   697
subsection{*The Set of Integers*}
paulson@14378
   698
paulson@14378
   699
constdefs
obua@14738
   700
   Ints  :: "'a::comm_ring_1 set"
paulson@14378
   701
    "Ints == range of_int"
paulson@14271
   702
paulson@14259
   703
paulson@14378
   704
syntax (xsymbols)
paulson@14378
   705
  Ints      :: "'a set"                   ("\<int>")
paulson@14378
   706
paulson@14378
   707
lemma Ints_0 [simp]: "0 \<in> Ints"
paulson@14479
   708
apply (simp add: Ints_def)
paulson@14479
   709
apply (rule range_eqI)
paulson@14378
   710
apply (rule of_int_0 [symmetric])
paulson@14378
   711
done
paulson@14378
   712
paulson@14378
   713
lemma Ints_1 [simp]: "1 \<in> Ints"
paulson@14479
   714
apply (simp add: Ints_def)
paulson@14479
   715
apply (rule range_eqI)
paulson@14378
   716
apply (rule of_int_1 [symmetric])
paulson@14378
   717
done
paulson@14378
   718
paulson@14378
   719
lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints"
paulson@14479
   720
apply (auto simp add: Ints_def)
paulson@14479
   721
apply (rule range_eqI)
paulson@14378
   722
apply (rule of_int_add [symmetric])
paulson@14378
   723
done
paulson@14378
   724
paulson@14378
   725
lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints"
paulson@14479
   726
apply (auto simp add: Ints_def)
paulson@14479
   727
apply (rule range_eqI)
paulson@14378
   728
apply (rule of_int_minus [symmetric])
paulson@14378
   729
done
paulson@14378
   730
paulson@14378
   731
lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints"
paulson@14479
   732
apply (auto simp add: Ints_def)
paulson@14479
   733
apply (rule range_eqI)
paulson@14378
   734
apply (rule of_int_diff [symmetric])
paulson@14378
   735
done
paulson@14378
   736
paulson@14378
   737
lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints"
paulson@14479
   738
apply (auto simp add: Ints_def)
paulson@14479
   739
apply (rule range_eqI)
paulson@14378
   740
apply (rule of_int_mult [symmetric])
paulson@14378
   741
done
paulson@14378
   742
paulson@14378
   743
text{*Collapse nested embeddings*}
paulson@14378
   744
lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n"
paulson@14479
   745
by (induct n, auto)
paulson@14378
   746
paulson@15013
   747
lemma of_int_int_eq [simp]: "of_int (int n) = of_nat n"
paulson@14479
   748
by (simp add: int_eq_of_nat)
paulson@14341
   749
paulson@14378
   750
lemma Ints_cases [case_names of_int, cases set: Ints]:
paulson@14378
   751
  "q \<in> \<int> ==> (!!z. q = of_int z ==> C) ==> C"
paulson@14479
   752
proof (simp add: Ints_def)
paulson@14378
   753
  assume "!!z. q = of_int z ==> C"
paulson@14378
   754
  assume "q \<in> range of_int" thus C ..
paulson@14378
   755
qed
paulson@14378
   756
paulson@14378
   757
lemma Ints_induct [case_names of_int, induct set: Ints]:
paulson@14378
   758
  "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q"
paulson@14378
   759
  by (rule Ints_cases) auto
paulson@14378
   760
paulson@14378
   761
paulson@14387
   762
(* int (Suc n) = 1 + int n *)
paulson@14387
   763
declare int_Suc [simp]
paulson@14387
   764
paulson@14378
   765
paulson@14430
   766
subsection{*More Properties of @{term setsum} and  @{term setprod}*}
paulson@14430
   767
paulson@14430
   768
text{*By Jeremy Avigad*}
paulson@14430
   769
paulson@14430
   770
nipkow@15554
   771
lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))"
paulson@14430
   772
  apply (case_tac "finite A")
paulson@14430
   773
  apply (erule finite_induct, auto)
paulson@14430
   774
  done
paulson@14430
   775
nipkow@15554
   776
lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))"
paulson@14430
   777
  apply (case_tac "finite A")
paulson@14430
   778
  apply (erule finite_induct, auto)
paulson@14430
   779
  done
paulson@14430
   780
nipkow@15554
   781
lemma int_setsum: "int (setsum f A) = (\<Sum>x\<in>A. int(f x))"
nipkow@15554
   782
  by (simp add: int_eq_of_nat of_nat_setsum)
paulson@14430
   783
nipkow@15554
   784
lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))"
paulson@14430
   785
  apply (case_tac "finite A")
paulson@14430
   786
  apply (erule finite_induct, auto)
paulson@14430
   787
  done
paulson@14430
   788
nipkow@15554
   789
lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))"
paulson@14430
   790
  apply (case_tac "finite A")
paulson@14430
   791
  apply (erule finite_induct, auto)
paulson@14430
   792
  done
paulson@14430
   793
nipkow@15554
   794
lemma int_setprod: "int (setprod f A) = (\<Prod>x\<in>A. int(f x))"
nipkow@15554
   795
  by (simp add: int_eq_of_nat of_nat_setprod)
paulson@14430
   796
paulson@14430
   797
lemma setprod_nonzero_nat:
paulson@14430
   798
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0"
paulson@14430
   799
  by (rule setprod_nonzero, auto)
paulson@14430
   800
paulson@14430
   801
lemma setprod_zero_eq_nat:
paulson@14430
   802
    "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)"
paulson@14430
   803
  by (rule setprod_zero_eq, auto)
paulson@14430
   804
paulson@14430
   805
lemma setprod_nonzero_int:
paulson@14430
   806
    "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0"
paulson@14430
   807
  by (rule setprod_nonzero, auto)
paulson@14430
   808
paulson@14430
   809
lemma setprod_zero_eq_int:
paulson@14430
   810
    "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)"
paulson@14430
   811
  by (rule setprod_zero_eq, auto)
paulson@14430
   812
paulson@14430
   813
paulson@14479
   814
text{*Now we replace the case analysis rule by a more conventional one:
paulson@14479
   815
whether an integer is negative or not.*}
paulson@14479
   816
paulson@14479
   817
lemma zless_iff_Suc_zadd:
paulson@14479
   818
    "(w < z) = (\<exists>n. z = w + int(Suc n))"
paulson@14479
   819
apply (cases z, cases w)
paulson@14479
   820
apply (auto simp add: le add int_def linorder_not_le [symmetric]) 
paulson@14479
   821
apply (rename_tac a b c d) 
paulson@14479
   822
apply (rule_tac x="a+d - Suc(c+b)" in exI) 
paulson@14479
   823
apply arith
paulson@14479
   824
done
paulson@14479
   825
paulson@14479
   826
lemma negD: "x<0 ==> \<exists>n. x = - (int (Suc n))"
paulson@14479
   827
apply (cases x)
paulson@14479
   828
apply (auto simp add: le minus Zero_int_def int_def order_less_le) 
paulson@14496
   829
apply (rule_tac x="y - Suc x" in exI, arith)
paulson@14479
   830
done
paulson@14479
   831
paulson@14479
   832
theorem int_cases [cases type: int, case_names nonneg neg]:
paulson@14479
   833
     "[|!! n. z = int n ==> P;  !! n. z =  - (int (Suc n)) ==> P |] ==> P"
paulson@14479
   834
apply (case_tac "z < 0", blast dest!: negD)
paulson@14479
   835
apply (simp add: linorder_not_less)
paulson@14479
   836
apply (blast dest: nat_0_le [THEN sym])
paulson@14479
   837
done
paulson@14479
   838
paulson@14479
   839
theorem int_induct [induct type: int, case_names nonneg neg]:
paulson@14479
   840
     "[|!! n. P (int n);  !!n. P (- (int (Suc n))) |] ==> P z"
paulson@14479
   841
  by (cases z) auto
paulson@14479
   842
paulson@15558
   843
text{*Contributed by Brian Huffman*}
paulson@15558
   844
theorem int_diff_cases [case_names diff]:
paulson@15558
   845
assumes prem: "!!m n. z = int m - int n ==> P" shows "P"
paulson@15558
   846
 apply (rule_tac z=z in int_cases)
paulson@15558
   847
  apply (rule_tac m=n and n=0 in prem, simp)
paulson@15558
   848
 apply (rule_tac m=0 and n="Suc n" in prem, simp)
paulson@15558
   849
done
paulson@14479
   850
paulson@15013
   851
lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z"
paulson@15013
   852
apply (cases z)
paulson@15013
   853
apply (simp_all add: not_zle_0_negative del: int_Suc)
paulson@15013
   854
done
paulson@15013
   855
paulson@15013
   856
berghofe@16642
   857
subsection {* Configuration of the code generator *}
berghofe@16642
   858
berghofe@16770
   859
(*FIXME: the IntInf.fromInt below hides a dependence on fixed-precision ints!*)
berghofe@16642
   860
berghofe@16642
   861
types_code
berghofe@16642
   862
  "int" ("int")
berghofe@16770
   863
attach (term_of) {*
berghofe@16770
   864
val term_of_int = HOLogic.mk_int o IntInf.fromInt;
berghofe@16770
   865
*}
berghofe@16770
   866
attach (test) {*
berghofe@16770
   867
fun gen_int i = one_of [~1, 1] * random_range 0 i;
berghofe@16770
   868
*}
berghofe@16642
   869
berghofe@16642
   870
constdefs
berghofe@16642
   871
  int_aux :: "int \<Rightarrow> nat \<Rightarrow> int"
berghofe@16642
   872
  "int_aux i n == (i + int n)"
berghofe@16642
   873
  nat_aux :: "nat \<Rightarrow> int \<Rightarrow> nat"
berghofe@16642
   874
  "nat_aux n i == (n + nat i)"
berghofe@16642
   875
berghofe@16642
   876
lemma [code]:
berghofe@16642
   877
  "int_aux i 0 = i"
berghofe@16642
   878
  "int_aux i (Suc n) = int_aux (i + 1) n" -- {* tail recursive *}
berghofe@16642
   879
  "int n = int_aux 0 n"
berghofe@16642
   880
  by (simp add: int_aux_def)+
berghofe@16642
   881
berghofe@16642
   882
lemma [code]: "nat_aux n i = (if i <= 0 then n else nat_aux (Suc n) (i - 1))"
berghofe@16642
   883
  -- {* tail recursive *}
berghofe@16642
   884
  by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le
berghofe@16642
   885
    dest: zless_imp_add1_zle)
berghofe@16642
   886
lemma [code]: "nat i = nat_aux 0 i"
berghofe@16642
   887
  by (simp add: nat_aux_def)
berghofe@16642
   888
berghofe@16642
   889
consts_code
berghofe@16642
   890
  "0" :: "int"                  ("0")
berghofe@16642
   891
  "1" :: "int"                  ("1")
berghofe@16770
   892
  "uminus" :: "int => int"      ("~")
berghofe@16770
   893
  "op +" :: "int => int => int" ("(_ +/ _)")
berghofe@16770
   894
  "op *" :: "int => int => int" ("(_ */ _)")
berghofe@16642
   895
  "op <" :: "int => int => bool" ("(_ </ _)")
berghofe@16642
   896
  "op <=" :: "int => int => bool" ("(_ <=/ _)")
berghofe@16642
   897
  "neg"                         ("(_ < 0)")
berghofe@16642
   898
haftmann@18702
   899
code_syntax_tyco int
haftmann@18702
   900
  ml (atom "IntInf.int")
haftmann@18702
   901
  haskell (atom "Integer")
haftmann@18702
   902
haftmann@18702
   903
code_syntax_const
haftmann@18702
   904
  0 :: "int"
haftmann@18704
   905
    ml (atom "(0:IntInf.int)")
haftmann@18702
   906
    haskell (atom "0")
haftmann@18702
   907
  1 :: "int"
haftmann@18704
   908
    ml (atom "(1:IntInf.int)")
haftmann@18702
   909
    haskell (atom "1")
haftmann@18702
   910
  "op +" :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@18702
   911
    ml (infixl 8 "+")
haftmann@18702
   912
    haskell (infixl 6 "+")
haftmann@18702
   913
  "op *" :: "int \<Rightarrow> int \<Rightarrow> int"
haftmann@18702
   914
    ml (infixl 9 "*")
haftmann@18702
   915
    haskell (infixl 7 "*")
haftmann@18702
   916
  uminus :: "int \<Rightarrow> int"
haftmann@18702
   917
    ml (atom "~")
haftmann@18702
   918
    haskell (atom "negate")
haftmann@18702
   919
  "op <" :: "int \<Rightarrow> int \<Rightarrow> bool"
haftmann@18702
   920
    ml (infix 6 "<")
haftmann@18702
   921
    haskell (infix 4 "<")
haftmann@18702
   922
  "op <=" :: "int \<Rightarrow> int \<Rightarrow> bool"
haftmann@18702
   923
    ml (infix 6 "<=")
haftmann@18702
   924
    haskell (infix 4 "<=")
haftmann@18702
   925
  "neg" :: "int \<Rightarrow> bool"
haftmann@18704
   926
    ml ("_/ </ 0")
haftmann@18704
   927
    haskell ("_/ </ 0")
haftmann@18702
   928
berghofe@16642
   929
ML {*
haftmann@18220
   930
fun mk_int_to_nat bin =
haftmann@18220
   931
  Const ("IntDef.nat", HOLogic.intT --> HOLogic.natT)
haftmann@18220
   932
  $ (Const ("Numeral.number_of", HOLogic.binT --> HOLogic.intT) $ bin);
haftmann@18220
   933
haftmann@18702
   934
fun bin_to_int bin = HOLogic.dest_binum bin
haftmann@18702
   935
  handle TERM _
haftmann@18702
   936
    => error ("not a number: " ^ Sign.string_of_term thy bin);
haftmann@18702
   937
berghofe@17551
   938
fun number_of_codegen thy defs gr dep module b (Const ("Numeral.number_of",
berghofe@17551
   939
      Type ("fun", [_, T as Type ("IntDef.int", [])])) $ bin) =
berghofe@17551
   940
        (SOME (fst (Codegen.invoke_tycodegen thy defs dep module false (gr, T)),
berghofe@17551
   941
           Pretty.str (IntInf.toString (HOLogic.dest_binum bin))) handle TERM _ => NONE)
berghofe@17667
   942
  | number_of_codegen thy defs gr s thyname b (Const ("Numeral.number_of",
berghofe@17667
   943
      Type ("fun", [_, Type ("nat", [])])) $ bin) =
haftmann@18115
   944
        SOME (Codegen.invoke_codegen thy defs s thyname b (gr,
haftmann@18115
   945
          Const ("IntDef.nat", HOLogic.intT --> HOLogic.natT) $
haftmann@18115
   946
            (Const ("Numeral.number_of", HOLogic.binT --> HOLogic.intT) $ bin)))
berghofe@16642
   947
  | number_of_codegen _ _ _ _ _ _ _ = NONE;
haftmann@18702
   948
berghofe@16642
   949
*}
berghofe@16642
   950
wenzelm@18708
   951
setup {*
wenzelm@18708
   952
  Codegen.add_codegen "number_of_codegen" number_of_codegen #>
haftmann@18518
   953
  CodegenPackage.add_appconst
wenzelm@18708
   954
    ("Numeral.number_of", ((1, 1), CodegenPackage.appgen_number_of mk_int_to_nat bin_to_int "IntDef.int" "nat")) #>
haftmann@18702
   955
  CodegenPackage.set_int_tyco "IntDef.int"
wenzelm@18708
   956
*}
berghofe@16642
   957
wenzelm@17464
   958
quickcheck_params [default_type = int]
wenzelm@17464
   959
berghofe@16642
   960
paulson@14378
   961
(*Legacy ML bindings, but no longer the structure Int.*)
paulson@14259
   962
ML
paulson@14259
   963
{*
paulson@14378
   964
val zabs_def = thm "zabs_def"
paulson@14378
   965
paulson@14378
   966
val int_0 = thm "int_0";
paulson@14378
   967
val int_1 = thm "int_1";
paulson@14378
   968
val int_Suc0_eq_1 = thm "int_Suc0_eq_1";
paulson@14378
   969
val neg_eq_less_0 = thm "neg_eq_less_0";
paulson@14378
   970
val not_neg_eq_ge_0 = thm "not_neg_eq_ge_0";
paulson@14378
   971
val not_neg_0 = thm "not_neg_0";
paulson@14378
   972
val not_neg_1 = thm "not_neg_1";
paulson@14378
   973
val iszero_0 = thm "iszero_0";
paulson@14378
   974
val not_iszero_1 = thm "not_iszero_1";
paulson@14378
   975
val int_0_less_1 = thm "int_0_less_1";
paulson@14378
   976
val int_0_neq_1 = thm "int_0_neq_1";
paulson@14378
   977
val negative_zless = thm "negative_zless";
paulson@14378
   978
val negative_zle = thm "negative_zle";
paulson@14378
   979
val not_zle_0_negative = thm "not_zle_0_negative";
paulson@14378
   980
val not_int_zless_negative = thm "not_int_zless_negative";
paulson@14378
   981
val negative_eq_positive = thm "negative_eq_positive";
paulson@14378
   982
val zle_iff_zadd = thm "zle_iff_zadd";
paulson@14378
   983
val abs_int_eq = thm "abs_int_eq";
paulson@14378
   984
val abs_split = thm"abs_split";
paulson@14378
   985
val nat_int = thm "nat_int";
paulson@14378
   986
val nat_zminus_int = thm "nat_zminus_int";
paulson@14378
   987
val nat_zero = thm "nat_zero";
paulson@14378
   988
val not_neg_nat = thm "not_neg_nat";
paulson@14378
   989
val neg_nat = thm "neg_nat";
paulson@14378
   990
val zless_nat_eq_int_zless = thm "zless_nat_eq_int_zless";
paulson@14378
   991
val nat_0_le = thm "nat_0_le";
paulson@14378
   992
val nat_le_0 = thm "nat_le_0";
paulson@14378
   993
val zless_nat_conj = thm "zless_nat_conj";
paulson@14378
   994
val int_cases = thm "int_cases";
paulson@14378
   995
paulson@14259
   996
val int_def = thm "int_def";
paulson@14259
   997
val Zero_int_def = thm "Zero_int_def";
paulson@14259
   998
val One_int_def = thm "One_int_def";
paulson@14479
   999
val diff_int_def = thm "diff_int_def";
paulson@14259
  1000
paulson@14259
  1001
val inj_int = thm "inj_int";
paulson@14259
  1002
val zminus_zminus = thm "zminus_zminus";
paulson@14259
  1003
val zminus_0 = thm "zminus_0";
paulson@14259
  1004
val zminus_zadd_distrib = thm "zminus_zadd_distrib";
paulson@14259
  1005
val zadd_commute = thm "zadd_commute";
paulson@14259
  1006
val zadd_assoc = thm "zadd_assoc";
paulson@14259
  1007
val zadd_left_commute = thm "zadd_left_commute";
paulson@14259
  1008
val zadd_ac = thms "zadd_ac";
paulson@14271
  1009
val zmult_ac = thms "zmult_ac";
paulson@14259
  1010
val zadd_int = thm "zadd_int";
paulson@14259
  1011
val zadd_int_left = thm "zadd_int_left";
paulson@14259
  1012
val int_Suc = thm "int_Suc";
paulson@14259
  1013
val zadd_0 = thm "zadd_0";
paulson@14259
  1014
val zadd_0_right = thm "zadd_0_right";
paulson@14259
  1015
val zmult_zminus = thm "zmult_zminus";
paulson@14259
  1016
val zmult_commute = thm "zmult_commute";
paulson@14259
  1017
val zmult_assoc = thm "zmult_assoc";
paulson@14259
  1018
val zadd_zmult_distrib = thm "zadd_zmult_distrib";
paulson@14259
  1019
val zadd_zmult_distrib2 = thm "zadd_zmult_distrib2";
paulson@14259
  1020
val zdiff_zmult_distrib = thm "zdiff_zmult_distrib";
paulson@14259
  1021
val zdiff_zmult_distrib2 = thm "zdiff_zmult_distrib2";
paulson@14259
  1022
val int_distrib = thms "int_distrib";
paulson@14259
  1023
val zmult_int = thm "zmult_int";
paulson@14259
  1024
val zmult_1 = thm "zmult_1";
paulson@14259
  1025
val zmult_1_right = thm "zmult_1_right";
paulson@14259
  1026
val int_int_eq = thm "int_int_eq";
paulson@14259
  1027
val int_eq_0_conv = thm "int_eq_0_conv";
paulson@14259
  1028
val zless_int = thm "zless_int";
paulson@14259
  1029
val int_less_0_conv = thm "int_less_0_conv";
paulson@14259
  1030
val zero_less_int_conv = thm "zero_less_int_conv";
paulson@14259
  1031
val zle_int = thm "zle_int";
paulson@14259
  1032
val zero_zle_int = thm "zero_zle_int";
paulson@14259
  1033
val int_le_0_conv = thm "int_le_0_conv";
paulson@14259
  1034
val zle_refl = thm "zle_refl";
paulson@14259
  1035
val zle_linear = thm "zle_linear";
paulson@14259
  1036
val zle_trans = thm "zle_trans";
paulson@14259
  1037
val zle_anti_sym = thm "zle_anti_sym";
paulson@14378
  1038
paulson@14378
  1039
val Ints_def = thm "Ints_def";
paulson@14378
  1040
val Nats_def = thm "Nats_def";
paulson@14378
  1041
paulson@14378
  1042
val of_nat_0 = thm "of_nat_0";
paulson@14378
  1043
val of_nat_Suc = thm "of_nat_Suc";
paulson@14378
  1044
val of_nat_1 = thm "of_nat_1";
paulson@14378
  1045
val of_nat_add = thm "of_nat_add";
paulson@14378
  1046
val of_nat_mult = thm "of_nat_mult";
paulson@14378
  1047
val zero_le_imp_of_nat = thm "zero_le_imp_of_nat";
paulson@14378
  1048
val less_imp_of_nat_less = thm "less_imp_of_nat_less";
paulson@14378
  1049
val of_nat_less_imp_less = thm "of_nat_less_imp_less";
paulson@14378
  1050
val of_nat_less_iff = thm "of_nat_less_iff";
paulson@14378
  1051
val of_nat_le_iff = thm "of_nat_le_iff";
paulson@14378
  1052
val of_nat_eq_iff = thm "of_nat_eq_iff";
paulson@14378
  1053
val Nats_0 = thm "Nats_0";
paulson@14378
  1054
val Nats_1 = thm "Nats_1";
paulson@14378
  1055
val Nats_add = thm "Nats_add";
paulson@14378
  1056
val Nats_mult = thm "Nats_mult";
paulson@14387
  1057
val int_eq_of_nat = thm"int_eq_of_nat";
paulson@14378
  1058
val of_int = thm "of_int";
paulson@14378
  1059
val of_int_0 = thm "of_int_0";
paulson@14378
  1060
val of_int_1 = thm "of_int_1";
paulson@14378
  1061
val of_int_add = thm "of_int_add";
paulson@14378
  1062
val of_int_minus = thm "of_int_minus";
paulson@14378
  1063
val of_int_diff = thm "of_int_diff";
paulson@14378
  1064
val of_int_mult = thm "of_int_mult";
paulson@14378
  1065
val of_int_le_iff = thm "of_int_le_iff";
paulson@14378
  1066
val of_int_less_iff = thm "of_int_less_iff";
paulson@14378
  1067
val of_int_eq_iff = thm "of_int_eq_iff";
paulson@14378
  1068
val Ints_0 = thm "Ints_0";
paulson@14378
  1069
val Ints_1 = thm "Ints_1";
paulson@14378
  1070
val Ints_add = thm "Ints_add";
paulson@14378
  1071
val Ints_minus = thm "Ints_minus";
paulson@14378
  1072
val Ints_diff = thm "Ints_diff";
paulson@14378
  1073
val Ints_mult = thm "Ints_mult";
paulson@14378
  1074
val of_int_of_nat_eq = thm"of_int_of_nat_eq";
paulson@14378
  1075
val Ints_cases = thm "Ints_cases";
paulson@14378
  1076
val Ints_induct = thm "Ints_induct";
paulson@14259
  1077
*}
paulson@14259
  1078
paulson@5508
  1079
end