src/ZF/IntDiv_ZF.thy
author krauss
Mon Feb 11 15:40:21 2008 +0100 (2008-02-11)
changeset 26056 6a0801279f4c
child 32149 ef59550a55d3
permissions -rw-r--r--
Made theory names in ZF disjoint from HOL theory names to allow loading both developments
in a single session (but not merge them).
krauss@26056
     1
(*  Title:      ZF/IntDiv.thy
krauss@26056
     2
    ID:         $Id$
krauss@26056
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
krauss@26056
     4
    Copyright   1999  University of Cambridge
krauss@26056
     5
krauss@26056
     6
Here is the division algorithm in ML:
krauss@26056
     7
krauss@26056
     8
    fun posDivAlg (a,b) =
krauss@26056
     9
      if a<b then (0,a)
krauss@26056
    10
      else let val (q,r) = posDivAlg(a, 2*b)
krauss@26056
    11
	       in  if 0<=r-b then (2*q+1, r-b) else (2*q, r)
krauss@26056
    12
	   end
krauss@26056
    13
krauss@26056
    14
    fun negDivAlg (a,b) =
krauss@26056
    15
      if 0<=a+b then (~1,a+b)
krauss@26056
    16
      else let val (q,r) = negDivAlg(a, 2*b)
krauss@26056
    17
	       in  if 0<=r-b then (2*q+1, r-b) else (2*q, r)
krauss@26056
    18
	   end;
krauss@26056
    19
krauss@26056
    20
    fun negateSnd (q,r:int) = (q,~r);
krauss@26056
    21
krauss@26056
    22
    fun divAlg (a,b) = if 0<=a then 
krauss@26056
    23
			  if b>0 then posDivAlg (a,b) 
krauss@26056
    24
			   else if a=0 then (0,0)
krauss@26056
    25
				else negateSnd (negDivAlg (~a,~b))
krauss@26056
    26
		       else 
krauss@26056
    27
			  if 0<b then negDivAlg (a,b)
krauss@26056
    28
			  else        negateSnd (posDivAlg (~a,~b));
krauss@26056
    29
krauss@26056
    30
*)
krauss@26056
    31
krauss@26056
    32
header{*The Division Operators Div and Mod*}
krauss@26056
    33
krauss@26056
    34
theory IntDiv_ZF imports IntArith OrderArith begin
krauss@26056
    35
krauss@26056
    36
definition
krauss@26056
    37
  quorem :: "[i,i] => o"  where
krauss@26056
    38
    "quorem == %<a,b> <q,r>.
krauss@26056
    39
                      a = b$*q $+ r &
krauss@26056
    40
                      (#0$<b & #0$<=r & r$<b | ~(#0$<b) & b$<r & r $<= #0)"
krauss@26056
    41
krauss@26056
    42
definition
krauss@26056
    43
  adjust :: "[i,i] => i"  where
krauss@26056
    44
    "adjust(b) == %<q,r>. if #0 $<= r$-b then <#2$*q $+ #1,r$-b>
krauss@26056
    45
                          else <#2$*q,r>"
krauss@26056
    46
krauss@26056
    47
krauss@26056
    48
(** the division algorithm **)
krauss@26056
    49
krauss@26056
    50
definition
krauss@26056
    51
  posDivAlg :: "i => i"  where
krauss@26056
    52
(*for the case a>=0, b>0*)
krauss@26056
    53
(*recdef posDivAlg "inv_image less_than (%(a,b). nat_of(a $- b $+ #1))"*)
krauss@26056
    54
    "posDivAlg(ab) ==
krauss@26056
    55
       wfrec(measure(int*int, %<a,b>. nat_of (a $- b $+ #1)),
krauss@26056
    56
	     ab,
krauss@26056
    57
	     %<a,b> f. if (a$<b | b$<=#0) then <#0,a>
krauss@26056
    58
                       else adjust(b, f ` <a,#2$*b>))"
krauss@26056
    59
krauss@26056
    60
krauss@26056
    61
(*for the case a<0, b>0*)
krauss@26056
    62
definition
krauss@26056
    63
  negDivAlg :: "i => i"  where
krauss@26056
    64
(*recdef negDivAlg "inv_image less_than (%(a,b). nat_of(- a $- b))"*)
krauss@26056
    65
    "negDivAlg(ab) ==
krauss@26056
    66
       wfrec(measure(int*int, %<a,b>. nat_of ($- a $- b)),
krauss@26056
    67
	     ab,
krauss@26056
    68
	     %<a,b> f. if (#0 $<= a$+b | b$<=#0) then <#-1,a$+b>
krauss@26056
    69
                       else adjust(b, f ` <a,#2$*b>))"
krauss@26056
    70
krauss@26056
    71
(*for the general case b\<noteq>0*)
krauss@26056
    72
krauss@26056
    73
definition
krauss@26056
    74
  negateSnd :: "i => i"  where
krauss@26056
    75
    "negateSnd == %<q,r>. <q, $-r>"
krauss@26056
    76
krauss@26056
    77
  (*The full division algorithm considers all possible signs for a, b
krauss@26056
    78
    including the special case a=0, b<0, because negDivAlg requires a<0*)
krauss@26056
    79
definition
krauss@26056
    80
  divAlg :: "i => i"  where
krauss@26056
    81
    "divAlg ==
krauss@26056
    82
       %<a,b>. if #0 $<= a then
krauss@26056
    83
                  if #0 $<= b then posDivAlg (<a,b>)
krauss@26056
    84
                  else if a=#0 then <#0,#0>
krauss@26056
    85
                       else negateSnd (negDivAlg (<$-a,$-b>))
krauss@26056
    86
               else 
krauss@26056
    87
                  if #0$<b then negDivAlg (<a,b>)
krauss@26056
    88
                  else         negateSnd (posDivAlg (<$-a,$-b>))"
krauss@26056
    89
krauss@26056
    90
definition
krauss@26056
    91
  zdiv  :: "[i,i]=>i"                    (infixl "zdiv" 70)  where
krauss@26056
    92
    "a zdiv b == fst (divAlg (<intify(a), intify(b)>))"
krauss@26056
    93
krauss@26056
    94
definition
krauss@26056
    95
  zmod  :: "[i,i]=>i"                    (infixl "zmod" 70)  where
krauss@26056
    96
    "a zmod b == snd (divAlg (<intify(a), intify(b)>))"
krauss@26056
    97
krauss@26056
    98
krauss@26056
    99
(** Some basic laws by Sidi Ehmety (need linear arithmetic!) **)
krauss@26056
   100
krauss@26056
   101
lemma zspos_add_zspos_imp_zspos: "[| #0 $< x;  #0 $< y |] ==> #0 $< x $+ y"
krauss@26056
   102
apply (rule_tac y = "y" in zless_trans)
krauss@26056
   103
apply (rule_tac [2] zdiff_zless_iff [THEN iffD1])
krauss@26056
   104
apply auto
krauss@26056
   105
done
krauss@26056
   106
krauss@26056
   107
lemma zpos_add_zpos_imp_zpos: "[| #0 $<= x;  #0 $<= y |] ==> #0 $<= x $+ y"
krauss@26056
   108
apply (rule_tac y = "y" in zle_trans)
krauss@26056
   109
apply (rule_tac [2] zdiff_zle_iff [THEN iffD1])
krauss@26056
   110
apply auto
krauss@26056
   111
done
krauss@26056
   112
krauss@26056
   113
lemma zneg_add_zneg_imp_zneg: "[| x $< #0;  y $< #0 |] ==> x $+ y $< #0"
krauss@26056
   114
apply (rule_tac y = "y" in zless_trans)
krauss@26056
   115
apply (rule zless_zdiff_iff [THEN iffD1])
krauss@26056
   116
apply auto
krauss@26056
   117
done
krauss@26056
   118
krauss@26056
   119
(* this theorem is used below *)
krauss@26056
   120
lemma zneg_or_0_add_zneg_or_0_imp_zneg_or_0:
krauss@26056
   121
     "[| x $<= #0;  y $<= #0 |] ==> x $+ y $<= #0"
krauss@26056
   122
apply (rule_tac y = "y" in zle_trans)
krauss@26056
   123
apply (rule zle_zdiff_iff [THEN iffD1])
krauss@26056
   124
apply auto
krauss@26056
   125
done
krauss@26056
   126
krauss@26056
   127
lemma zero_lt_zmagnitude: "[| #0 $< k; k \<in> int |] ==> 0 < zmagnitude(k)"
krauss@26056
   128
apply (drule zero_zless_imp_znegative_zminus)
krauss@26056
   129
apply (drule_tac [2] zneg_int_of)
krauss@26056
   130
apply (auto simp add: zminus_equation [of k])
krauss@26056
   131
apply (subgoal_tac "0 < zmagnitude ($# succ (n))")
krauss@26056
   132
 apply simp
krauss@26056
   133
apply (simp only: zmagnitude_int_of)
krauss@26056
   134
apply simp
krauss@26056
   135
done
krauss@26056
   136
krauss@26056
   137
krauss@26056
   138
(*** Inequality lemmas involving $#succ(m) ***)
krauss@26056
   139
krauss@26056
   140
lemma zless_add_succ_iff:
krauss@26056
   141
     "(w $< z $+ $# succ(m)) <-> (w $< z $+ $#m | intify(w) = z $+ $#m)"
krauss@26056
   142
apply (auto simp add: zless_iff_succ_zadd zadd_assoc int_of_add [symmetric])
krauss@26056
   143
apply (rule_tac [3] x = "0" in bexI)
krauss@26056
   144
apply (cut_tac m = "m" in int_succ_int_1)
krauss@26056
   145
apply (cut_tac m = "n" in int_succ_int_1)
krauss@26056
   146
apply simp
krauss@26056
   147
apply (erule natE)
krauss@26056
   148
apply auto
krauss@26056
   149
apply (rule_tac x = "succ (n) " in bexI)
krauss@26056
   150
apply auto
krauss@26056
   151
done
krauss@26056
   152
krauss@26056
   153
lemma zadd_succ_lemma:
krauss@26056
   154
     "z \<in> int ==> (w $+ $# succ(m) $<= z) <-> (w $+ $#m $< z)"
krauss@26056
   155
apply (simp only: not_zless_iff_zle [THEN iff_sym] zless_add_succ_iff)
krauss@26056
   156
apply (auto intro: zle_anti_sym elim: zless_asym
krauss@26056
   157
            simp add: zless_imp_zle not_zless_iff_zle)
krauss@26056
   158
done
krauss@26056
   159
krauss@26056
   160
lemma zadd_succ_zle_iff: "(w $+ $# succ(m) $<= z) <-> (w $+ $#m $< z)"
krauss@26056
   161
apply (cut_tac z = "intify (z)" in zadd_succ_lemma)
krauss@26056
   162
apply auto
krauss@26056
   163
done
krauss@26056
   164
krauss@26056
   165
(** Inequality reasoning **)
krauss@26056
   166
krauss@26056
   167
lemma zless_add1_iff_zle: "(w $< z $+ #1) <-> (w$<=z)"
krauss@26056
   168
apply (subgoal_tac "#1 = $# 1")
krauss@26056
   169
apply (simp only: zless_add_succ_iff zle_def)
krauss@26056
   170
apply auto
krauss@26056
   171
done
krauss@26056
   172
krauss@26056
   173
lemma add1_zle_iff: "(w $+ #1 $<= z) <-> (w $< z)"
krauss@26056
   174
apply (subgoal_tac "#1 = $# 1")
krauss@26056
   175
apply (simp only: zadd_succ_zle_iff)
krauss@26056
   176
apply auto
krauss@26056
   177
done
krauss@26056
   178
krauss@26056
   179
lemma add1_left_zle_iff: "(#1 $+ w $<= z) <-> (w $< z)"
krauss@26056
   180
apply (subst zadd_commute)
krauss@26056
   181
apply (rule add1_zle_iff)
krauss@26056
   182
done
krauss@26056
   183
krauss@26056
   184
krauss@26056
   185
(*** Monotonicity of Multiplication ***)
krauss@26056
   186
krauss@26056
   187
lemma zmult_mono_lemma: "k \<in> nat ==> i $<= j ==> i $* $#k $<= j $* $#k"
krauss@26056
   188
apply (induct_tac "k")
krauss@26056
   189
 prefer 2 apply (subst int_succ_int_1)
krauss@26056
   190
apply (simp_all (no_asm_simp) add: zadd_zmult_distrib2 zadd_zle_mono)
krauss@26056
   191
done
krauss@26056
   192
krauss@26056
   193
lemma zmult_zle_mono1: "[| i $<= j;  #0 $<= k |] ==> i$*k $<= j$*k"
krauss@26056
   194
apply (subgoal_tac "i $* intify (k) $<= j $* intify (k) ")
krauss@26056
   195
apply (simp (no_asm_use))
krauss@26056
   196
apply (rule_tac b = "intify (k)" in not_zneg_mag [THEN subst])
krauss@26056
   197
apply (rule_tac [3] zmult_mono_lemma)
krauss@26056
   198
apply auto
krauss@26056
   199
apply (simp add: znegative_iff_zless_0 not_zless_iff_zle [THEN iff_sym])
krauss@26056
   200
done
krauss@26056
   201
krauss@26056
   202
lemma zmult_zle_mono1_neg: "[| i $<= j;  k $<= #0 |] ==> j$*k $<= i$*k"
krauss@26056
   203
apply (rule zminus_zle_zminus [THEN iffD1])
krauss@26056
   204
apply (simp del: zmult_zminus_right
krauss@26056
   205
            add: zmult_zminus_right [symmetric] zmult_zle_mono1 zle_zminus)
krauss@26056
   206
done
krauss@26056
   207
krauss@26056
   208
lemma zmult_zle_mono2: "[| i $<= j;  #0 $<= k |] ==> k$*i $<= k$*j"
krauss@26056
   209
apply (drule zmult_zle_mono1)
krauss@26056
   210
apply (simp_all add: zmult_commute)
krauss@26056
   211
done
krauss@26056
   212
krauss@26056
   213
lemma zmult_zle_mono2_neg: "[| i $<= j;  k $<= #0 |] ==> k$*j $<= k$*i"
krauss@26056
   214
apply (drule zmult_zle_mono1_neg)
krauss@26056
   215
apply (simp_all add: zmult_commute)
krauss@26056
   216
done
krauss@26056
   217
krauss@26056
   218
(* $<= monotonicity, BOTH arguments*)
krauss@26056
   219
lemma zmult_zle_mono:
krauss@26056
   220
     "[| i $<= j;  k $<= l;  #0 $<= j;  #0 $<= k |] ==> i$*k $<= j$*l"
krauss@26056
   221
apply (erule zmult_zle_mono1 [THEN zle_trans])
krauss@26056
   222
apply assumption
krauss@26056
   223
apply (erule zmult_zle_mono2)
krauss@26056
   224
apply assumption
krauss@26056
   225
done
krauss@26056
   226
krauss@26056
   227
krauss@26056
   228
(** strict, in 1st argument; proof is by induction on k>0 **)
krauss@26056
   229
krauss@26056
   230
lemma zmult_zless_mono2_lemma [rule_format]:
krauss@26056
   231
     "[| i$<j; k \<in> nat |] ==> 0<k --> $#k $* i $< $#k $* j"
krauss@26056
   232
apply (induct_tac "k")
krauss@26056
   233
 prefer 2
krauss@26056
   234
 apply (subst int_succ_int_1)
krauss@26056
   235
 apply (erule natE)
krauss@26056
   236
apply (simp_all add: zadd_zmult_distrib zadd_zless_mono zle_def)
krauss@26056
   237
apply (frule nat_0_le)
krauss@26056
   238
apply (subgoal_tac "i $+ (i $+ $# xa $* i) $< j $+ (j $+ $# xa $* j) ")
krauss@26056
   239
apply (simp (no_asm_use))
krauss@26056
   240
apply (rule zadd_zless_mono)
krauss@26056
   241
apply (simp_all (no_asm_simp) add: zle_def)
krauss@26056
   242
done
krauss@26056
   243
krauss@26056
   244
lemma zmult_zless_mono2: "[| i$<j;  #0 $< k |] ==> k$*i $< k$*j"
krauss@26056
   245
apply (subgoal_tac "intify (k) $* i $< intify (k) $* j")
krauss@26056
   246
apply (simp (no_asm_use))
krauss@26056
   247
apply (rule_tac b = "intify (k)" in not_zneg_mag [THEN subst])
krauss@26056
   248
apply (rule_tac [3] zmult_zless_mono2_lemma)
krauss@26056
   249
apply auto
krauss@26056
   250
apply (simp add: znegative_iff_zless_0)
krauss@26056
   251
apply (drule zless_trans, assumption)
krauss@26056
   252
apply (auto simp add: zero_lt_zmagnitude)
krauss@26056
   253
done
krauss@26056
   254
krauss@26056
   255
lemma zmult_zless_mono1: "[| i$<j;  #0 $< k |] ==> i$*k $< j$*k"
krauss@26056
   256
apply (drule zmult_zless_mono2)
krauss@26056
   257
apply (simp_all add: zmult_commute)
krauss@26056
   258
done
krauss@26056
   259
krauss@26056
   260
(* < monotonicity, BOTH arguments*)
krauss@26056
   261
lemma zmult_zless_mono:
krauss@26056
   262
     "[| i $< j;  k $< l;  #0 $< j;  #0 $< k |] ==> i$*k $< j$*l"
krauss@26056
   263
apply (erule zmult_zless_mono1 [THEN zless_trans])
krauss@26056
   264
apply assumption
krauss@26056
   265
apply (erule zmult_zless_mono2)
krauss@26056
   266
apply assumption
krauss@26056
   267
done
krauss@26056
   268
krauss@26056
   269
lemma zmult_zless_mono1_neg: "[| i $< j;  k $< #0 |] ==> j$*k $< i$*k"
krauss@26056
   270
apply (rule zminus_zless_zminus [THEN iffD1])
krauss@26056
   271
apply (simp del: zmult_zminus_right 
krauss@26056
   272
            add: zmult_zminus_right [symmetric] zmult_zless_mono1 zless_zminus)
krauss@26056
   273
done
krauss@26056
   274
krauss@26056
   275
lemma zmult_zless_mono2_neg: "[| i $< j;  k $< #0 |] ==> k$*j $< k$*i"
krauss@26056
   276
apply (rule zminus_zless_zminus [THEN iffD1])
krauss@26056
   277
apply (simp del: zmult_zminus 
krauss@26056
   278
            add: zmult_zminus [symmetric] zmult_zless_mono2 zless_zminus)
krauss@26056
   279
done
krauss@26056
   280
krauss@26056
   281
krauss@26056
   282
(** Products of zeroes **)
krauss@26056
   283
krauss@26056
   284
lemma zmult_eq_lemma:
krauss@26056
   285
     "[| m \<in> int; n \<in> int |] ==> (m = #0 | n = #0) <-> (m$*n = #0)"
krauss@26056
   286
apply (case_tac "m $< #0")
krauss@26056
   287
apply (auto simp add: not_zless_iff_zle zle_def neq_iff_zless)
krauss@26056
   288
apply (force dest: zmult_zless_mono1_neg zmult_zless_mono1)+
krauss@26056
   289
done
krauss@26056
   290
krauss@26056
   291
lemma zmult_eq_0_iff [iff]: "(m$*n = #0) <-> (intify(m) = #0 | intify(n) = #0)"
krauss@26056
   292
apply (simp add: zmult_eq_lemma)
krauss@26056
   293
done
krauss@26056
   294
krauss@26056
   295
krauss@26056
   296
(** Cancellation laws for k*m < k*n and m*k < n*k, also for <= and =,
krauss@26056
   297
    but not (yet?) for k*m < n*k. **)
krauss@26056
   298
krauss@26056
   299
lemma zmult_zless_lemma:
krauss@26056
   300
     "[| k \<in> int; m \<in> int; n \<in> int |]  
krauss@26056
   301
      ==> (m$*k $< n$*k) <-> ((#0 $< k & m$<n) | (k $< #0 & n$<m))"
krauss@26056
   302
apply (case_tac "k = #0")
krauss@26056
   303
apply (auto simp add: neq_iff_zless zmult_zless_mono1 zmult_zless_mono1_neg)
krauss@26056
   304
apply (auto simp add: not_zless_iff_zle 
krauss@26056
   305
                      not_zle_iff_zless [THEN iff_sym, of "m$*k"] 
krauss@26056
   306
                      not_zle_iff_zless [THEN iff_sym, of m])
krauss@26056
   307
apply (auto elim: notE
krauss@26056
   308
            simp add: zless_imp_zle zmult_zle_mono1 zmult_zle_mono1_neg)
krauss@26056
   309
done
krauss@26056
   310
krauss@26056
   311
lemma zmult_zless_cancel2:
krauss@26056
   312
     "(m$*k $< n$*k) <-> ((#0 $< k & m$<n) | (k $< #0 & n$<m))"
krauss@26056
   313
apply (cut_tac k = "intify (k)" and m = "intify (m)" and n = "intify (n)" 
krauss@26056
   314
       in zmult_zless_lemma)
krauss@26056
   315
apply auto
krauss@26056
   316
done
krauss@26056
   317
krauss@26056
   318
lemma zmult_zless_cancel1:
krauss@26056
   319
     "(k$*m $< k$*n) <-> ((#0 $< k & m$<n) | (k $< #0 & n$<m))"
krauss@26056
   320
by (simp add: zmult_commute [of k] zmult_zless_cancel2)
krauss@26056
   321
krauss@26056
   322
lemma zmult_zle_cancel2:
krauss@26056
   323
     "(m$*k $<= n$*k) <-> ((#0 $< k --> m$<=n) & (k $< #0 --> n$<=m))"
krauss@26056
   324
by (auto simp add: not_zless_iff_zle [THEN iff_sym] zmult_zless_cancel2)
krauss@26056
   325
krauss@26056
   326
lemma zmult_zle_cancel1:
krauss@26056
   327
     "(k$*m $<= k$*n) <-> ((#0 $< k --> m$<=n) & (k $< #0 --> n$<=m))"
krauss@26056
   328
by (auto simp add: not_zless_iff_zle [THEN iff_sym] zmult_zless_cancel1)
krauss@26056
   329
krauss@26056
   330
lemma int_eq_iff_zle: "[| m \<in> int; n \<in> int |] ==> m=n <-> (m $<= n & n $<= m)"
krauss@26056
   331
apply (blast intro: zle_refl zle_anti_sym)
krauss@26056
   332
done
krauss@26056
   333
krauss@26056
   334
lemma zmult_cancel2_lemma:
krauss@26056
   335
     "[| k \<in> int; m \<in> int; n \<in> int |] ==> (m$*k = n$*k) <-> (k=#0 | m=n)"
krauss@26056
   336
apply (simp add: int_eq_iff_zle [of "m$*k"] int_eq_iff_zle [of m])
krauss@26056
   337
apply (auto simp add: zmult_zle_cancel2 neq_iff_zless)
krauss@26056
   338
done
krauss@26056
   339
krauss@26056
   340
lemma zmult_cancel2 [simp]:
krauss@26056
   341
     "(m$*k = n$*k) <-> (intify(k) = #0 | intify(m) = intify(n))"
krauss@26056
   342
apply (rule iff_trans)
krauss@26056
   343
apply (rule_tac [2] zmult_cancel2_lemma)
krauss@26056
   344
apply auto
krauss@26056
   345
done
krauss@26056
   346
krauss@26056
   347
lemma zmult_cancel1 [simp]:
krauss@26056
   348
     "(k$*m = k$*n) <-> (intify(k) = #0 | intify(m) = intify(n))"
krauss@26056
   349
by (simp add: zmult_commute [of k] zmult_cancel2)
krauss@26056
   350
krauss@26056
   351
krauss@26056
   352
subsection{* Uniqueness and monotonicity of quotients and remainders *}
krauss@26056
   353
krauss@26056
   354
lemma unique_quotient_lemma:
krauss@26056
   355
     "[| b$*q' $+ r' $<= b$*q $+ r;  #0 $<= r';  #0 $< b;  r $< b |]  
krauss@26056
   356
      ==> q' $<= q"
krauss@26056
   357
apply (subgoal_tac "r' $+ b $* (q'$-q) $<= r")
krauss@26056
   358
 prefer 2 apply (simp add: zdiff_zmult_distrib2 zadd_ac zcompare_rls)
krauss@26056
   359
apply (subgoal_tac "#0 $< b $* (#1 $+ q $- q') ")
krauss@26056
   360
 prefer 2
krauss@26056
   361
 apply (erule zle_zless_trans)
krauss@26056
   362
 apply (simp add: zdiff_zmult_distrib2 zadd_zmult_distrib2 zadd_ac zcompare_rls)
krauss@26056
   363
 apply (erule zle_zless_trans)
krauss@26056
   364
 apply (simp add: ); 
krauss@26056
   365
apply (subgoal_tac "b $* q' $< b $* (#1 $+ q)")
krauss@26056
   366
 prefer 2 
krauss@26056
   367
 apply (simp add: zdiff_zmult_distrib2 zadd_zmult_distrib2 zadd_ac zcompare_rls)
krauss@26056
   368
apply (auto elim: zless_asym
krauss@26056
   369
        simp add: zmult_zless_cancel1 zless_add1_iff_zle zadd_ac zcompare_rls)
krauss@26056
   370
done
krauss@26056
   371
krauss@26056
   372
lemma unique_quotient_lemma_neg:
krauss@26056
   373
     "[| b$*q' $+ r' $<= b$*q $+ r;  r $<= #0;  b $< #0;  b $< r' |]  
krauss@26056
   374
      ==> q $<= q'"
krauss@26056
   375
apply (rule_tac b = "$-b" and r = "$-r'" and r' = "$-r" 
krauss@26056
   376
       in unique_quotient_lemma)
krauss@26056
   377
apply (auto simp del: zminus_zadd_distrib
krauss@26056
   378
            simp add: zminus_zadd_distrib [symmetric] zle_zminus zless_zminus)
krauss@26056
   379
done
krauss@26056
   380
krauss@26056
   381
krauss@26056
   382
lemma unique_quotient:
krauss@26056
   383
     "[| quorem (<a,b>, <q,r>);  quorem (<a,b>, <q',r'>);  b \<in> int; b ~= #0;  
krauss@26056
   384
         q \<in> int; q' \<in> int |] ==> q = q'"
krauss@26056
   385
apply (simp add: split_ifs quorem_def neq_iff_zless)
krauss@26056
   386
apply safe
krauss@26056
   387
apply simp_all
krauss@26056
   388
apply (blast intro: zle_anti_sym
krauss@26056
   389
             dest: zle_eq_refl [THEN unique_quotient_lemma] 
krauss@26056
   390
                   zle_eq_refl [THEN unique_quotient_lemma_neg] sym)+
krauss@26056
   391
done
krauss@26056
   392
krauss@26056
   393
lemma unique_remainder:
krauss@26056
   394
     "[| quorem (<a,b>, <q,r>);  quorem (<a,b>, <q',r'>);  b \<in> int; b ~= #0;  
krauss@26056
   395
         q \<in> int; q' \<in> int;  
krauss@26056
   396
         r \<in> int; r' \<in> int |] ==> r = r'"
krauss@26056
   397
apply (subgoal_tac "q = q'")
krauss@26056
   398
 prefer 2 apply (blast intro: unique_quotient)
krauss@26056
   399
apply (simp add: quorem_def)
krauss@26056
   400
done
krauss@26056
   401
krauss@26056
   402
krauss@26056
   403
subsection{*Correctness of posDivAlg, 
krauss@26056
   404
           the Division Algorithm for @{text "a\<ge>0"} and @{text "b>0"} *}
krauss@26056
   405
krauss@26056
   406
lemma adjust_eq [simp]:
krauss@26056
   407
     "adjust(b, <q,r>) = (let diff = r$-b in  
krauss@26056
   408
                          if #0 $<= diff then <#2$*q $+ #1,diff>   
krauss@26056
   409
                                         else <#2$*q,r>)"
krauss@26056
   410
by (simp add: Let_def adjust_def)
krauss@26056
   411
krauss@26056
   412
krauss@26056
   413
lemma posDivAlg_termination:
krauss@26056
   414
     "[| #0 $< b; ~ a $< b |]    
krauss@26056
   415
      ==> nat_of(a $- #2 $\<times> b $+ #1) < nat_of(a $- b $+ #1)"
krauss@26056
   416
apply (simp (no_asm) add: zless_nat_conj)
krauss@26056
   417
apply (simp add: not_zless_iff_zle zless_add1_iff_zle zcompare_rls)
krauss@26056
   418
done
krauss@26056
   419
krauss@26056
   420
lemmas posDivAlg_unfold = def_wfrec [OF posDivAlg_def wf_measure]
krauss@26056
   421
krauss@26056
   422
lemma posDivAlg_eqn:
krauss@26056
   423
     "[| #0 $< b; a \<in> int; b \<in> int |] ==>  
krauss@26056
   424
      posDivAlg(<a,b>) =       
krauss@26056
   425
       (if a$<b then <#0,a> else adjust(b, posDivAlg (<a, #2$*b>)))"
krauss@26056
   426
apply (rule posDivAlg_unfold [THEN trans])
krauss@26056
   427
apply (simp add: vimage_iff not_zless_iff_zle [THEN iff_sym])
krauss@26056
   428
apply (blast intro: posDivAlg_termination)
krauss@26056
   429
done
krauss@26056
   430
krauss@26056
   431
lemma posDivAlg_induct_lemma [rule_format]:
krauss@26056
   432
  assumes prem:
krauss@26056
   433
        "!!a b. [| a \<in> int; b \<in> int;  
krauss@26056
   434
                   ~ (a $< b | b $<= #0) --> P(<a, #2 $* b>) |] ==> P(<a,b>)"
krauss@26056
   435
  shows "<u,v> \<in> int*int --> P(<u,v>)"
krauss@26056
   436
apply (rule_tac a = "<u,v>" in wf_induct)
krauss@26056
   437
apply (rule_tac A = "int*int" and f = "%<a,b>.nat_of (a $- b $+ #1)" 
krauss@26056
   438
       in wf_measure)
krauss@26056
   439
apply clarify
krauss@26056
   440
apply (rule prem)
krauss@26056
   441
apply (drule_tac [3] x = "<xa, #2 $\<times> y>" in spec)
krauss@26056
   442
apply auto
krauss@26056
   443
apply (simp add: not_zle_iff_zless posDivAlg_termination)
krauss@26056
   444
done
krauss@26056
   445
krauss@26056
   446
krauss@26056
   447
lemma posDivAlg_induct [consumes 2]:
krauss@26056
   448
  assumes u_int: "u \<in> int"
krauss@26056
   449
      and v_int: "v \<in> int"
krauss@26056
   450
      and ih: "!!a b. [| a \<in> int; b \<in> int;
krauss@26056
   451
                     ~ (a $< b | b $<= #0) --> P(a, #2 $* b) |] ==> P(a,b)"
krauss@26056
   452
  shows "P(u,v)"
krauss@26056
   453
apply (subgoal_tac "(%<x,y>. P (x,y)) (<u,v>)")
krauss@26056
   454
apply simp
krauss@26056
   455
apply (rule posDivAlg_induct_lemma)
krauss@26056
   456
apply (simp (no_asm_use))
krauss@26056
   457
apply (rule ih)
krauss@26056
   458
apply (auto simp add: u_int v_int)
krauss@26056
   459
done
krauss@26056
   460
krauss@26056
   461
(*FIXME: use intify in integ_of so that we always have integ_of w \<in> int.
krauss@26056
   462
    then this rewrite can work for ALL constants!!*)
krauss@26056
   463
lemma intify_eq_0_iff_zle: "intify(m) = #0 <-> (m $<= #0 & #0 $<= m)"
krauss@26056
   464
apply (simp (no_asm) add: int_eq_iff_zle)
krauss@26056
   465
done
krauss@26056
   466
krauss@26056
   467
krauss@26056
   468
subsection{* Some convenient biconditionals for products of signs *}
krauss@26056
   469
krauss@26056
   470
lemma zmult_pos: "[| #0 $< i; #0 $< j |] ==> #0 $< i $* j"
krauss@26056
   471
apply (drule zmult_zless_mono1)
krauss@26056
   472
apply auto
krauss@26056
   473
done
krauss@26056
   474
krauss@26056
   475
lemma zmult_neg: "[| i $< #0; j $< #0 |] ==> #0 $< i $* j"
krauss@26056
   476
apply (drule zmult_zless_mono1_neg)
krauss@26056
   477
apply auto
krauss@26056
   478
done
krauss@26056
   479
krauss@26056
   480
lemma zmult_pos_neg: "[| #0 $< i; j $< #0 |] ==> i $* j $< #0"
krauss@26056
   481
apply (drule zmult_zless_mono1_neg)
krauss@26056
   482
apply auto
krauss@26056
   483
done
krauss@26056
   484
krauss@26056
   485
(** Inequality reasoning **)
krauss@26056
   486
krauss@26056
   487
lemma int_0_less_lemma:
krauss@26056
   488
     "[| x \<in> int; y \<in> int |]  
krauss@26056
   489
      ==> (#0 $< x $* y) <-> (#0 $< x & #0 $< y | x $< #0 & y $< #0)"
krauss@26056
   490
apply (auto simp add: zle_def not_zless_iff_zle zmult_pos zmult_neg)
krauss@26056
   491
apply (rule ccontr) 
krauss@26056
   492
apply (rule_tac [2] ccontr) 
krauss@26056
   493
apply (auto simp add: zle_def not_zless_iff_zle)
krauss@26056
   494
apply (erule_tac P = "#0$< x$* y" in rev_mp)
krauss@26056
   495
apply (erule_tac [2] P = "#0$< x$* y" in rev_mp)
krauss@26056
   496
apply (drule zmult_pos_neg, assumption) 
krauss@26056
   497
 prefer 2
krauss@26056
   498
 apply (drule zmult_pos_neg, assumption) 
krauss@26056
   499
apply (auto dest: zless_not_sym simp add: zmult_commute)
krauss@26056
   500
done
krauss@26056
   501
krauss@26056
   502
lemma int_0_less_mult_iff:
krauss@26056
   503
     "(#0 $< x $* y) <-> (#0 $< x & #0 $< y | x $< #0 & y $< #0)"
krauss@26056
   504
apply (cut_tac x = "intify (x)" and y = "intify (y)" in int_0_less_lemma)
krauss@26056
   505
apply auto
krauss@26056
   506
done
krauss@26056
   507
krauss@26056
   508
lemma int_0_le_lemma:
krauss@26056
   509
     "[| x \<in> int; y \<in> int |]  
krauss@26056
   510
      ==> (#0 $<= x $* y) <-> (#0 $<= x & #0 $<= y | x $<= #0 & y $<= #0)"
krauss@26056
   511
by (auto simp add: zle_def not_zless_iff_zle int_0_less_mult_iff)
krauss@26056
   512
krauss@26056
   513
lemma int_0_le_mult_iff:
krauss@26056
   514
     "(#0 $<= x $* y) <-> ((#0 $<= x & #0 $<= y) | (x $<= #0 & y $<= #0))"
krauss@26056
   515
apply (cut_tac x = "intify (x)" and y = "intify (y)" in int_0_le_lemma)
krauss@26056
   516
apply auto
krauss@26056
   517
done
krauss@26056
   518
krauss@26056
   519
lemma zmult_less_0_iff:
krauss@26056
   520
     "(x $* y $< #0) <-> (#0 $< x & y $< #0 | x $< #0 & #0 $< y)"
krauss@26056
   521
apply (auto simp add: int_0_le_mult_iff not_zle_iff_zless [THEN iff_sym])
krauss@26056
   522
apply (auto dest: zless_not_sym simp add: not_zle_iff_zless)
krauss@26056
   523
done
krauss@26056
   524
krauss@26056
   525
lemma zmult_le_0_iff:
krauss@26056
   526
     "(x $* y $<= #0) <-> (#0 $<= x & y $<= #0 | x $<= #0 & #0 $<= y)"
krauss@26056
   527
by (auto dest: zless_not_sym
krauss@26056
   528
         simp add: int_0_less_mult_iff not_zless_iff_zle [THEN iff_sym])
krauss@26056
   529
krauss@26056
   530
krauss@26056
   531
(*Typechecking for posDivAlg*)
krauss@26056
   532
lemma posDivAlg_type [rule_format]:
krauss@26056
   533
     "[| a \<in> int; b \<in> int |] ==> posDivAlg(<a,b>) \<in> int * int"
krauss@26056
   534
apply (rule_tac u = "a" and v = "b" in posDivAlg_induct)
krauss@26056
   535
apply assumption+
krauss@26056
   536
apply (case_tac "#0 $< ba")
krauss@26056
   537
 apply (simp add: posDivAlg_eqn adjust_def integ_of_type 
krauss@26056
   538
             split add: split_if_asm)
krauss@26056
   539
 apply clarify
krauss@26056
   540
 apply (simp add: int_0_less_mult_iff not_zle_iff_zless)
krauss@26056
   541
apply (simp add: not_zless_iff_zle)
krauss@26056
   542
apply (subst posDivAlg_unfold)
krauss@26056
   543
apply simp
krauss@26056
   544
done
krauss@26056
   545
krauss@26056
   546
(*Correctness of posDivAlg: it computes quotients correctly*)
krauss@26056
   547
lemma posDivAlg_correct [rule_format]:
krauss@26056
   548
     "[| a \<in> int; b \<in> int |]  
krauss@26056
   549
      ==> #0 $<= a --> #0 $< b --> quorem (<a,b>, posDivAlg(<a,b>))"
krauss@26056
   550
apply (rule_tac u = "a" and v = "b" in posDivAlg_induct)
krauss@26056
   551
apply auto
krauss@26056
   552
   apply (simp_all add: quorem_def)
krauss@26056
   553
   txt{*base case: a<b*}
krauss@26056
   554
   apply (simp add: posDivAlg_eqn)
krauss@26056
   555
  apply (simp add: not_zless_iff_zle [THEN iff_sym])
krauss@26056
   556
 apply (simp add: int_0_less_mult_iff)
krauss@26056
   557
txt{*main argument*}
krauss@26056
   558
apply (subst posDivAlg_eqn)
krauss@26056
   559
apply (simp_all (no_asm_simp))
krauss@26056
   560
apply (erule splitE)
krauss@26056
   561
apply (rule posDivAlg_type)
krauss@26056
   562
apply (simp_all add: int_0_less_mult_iff)
krauss@26056
   563
apply (auto simp add: zadd_zmult_distrib2 Let_def)
krauss@26056
   564
txt{*now just linear arithmetic*}
krauss@26056
   565
apply (simp add: not_zle_iff_zless zdiff_zless_iff)
krauss@26056
   566
done
krauss@26056
   567
krauss@26056
   568
krauss@26056
   569
subsection{*Correctness of negDivAlg, the division algorithm for a<0 and b>0*}
krauss@26056
   570
krauss@26056
   571
lemma negDivAlg_termination:
krauss@26056
   572
     "[| #0 $< b; a $+ b $< #0 |] 
krauss@26056
   573
      ==> nat_of($- a $- #2 $* b) < nat_of($- a $- b)"
krauss@26056
   574
apply (simp (no_asm) add: zless_nat_conj)
krauss@26056
   575
apply (simp add: zcompare_rls not_zle_iff_zless zless_zdiff_iff [THEN iff_sym]
krauss@26056
   576
                 zless_zminus)
krauss@26056
   577
done
krauss@26056
   578
krauss@26056
   579
lemmas negDivAlg_unfold = def_wfrec [OF negDivAlg_def wf_measure]
krauss@26056
   580
krauss@26056
   581
lemma negDivAlg_eqn:
krauss@26056
   582
     "[| #0 $< b; a : int; b : int |] ==>  
krauss@26056
   583
      negDivAlg(<a,b>) =       
krauss@26056
   584
       (if #0 $<= a$+b then <#-1,a$+b>  
krauss@26056
   585
                       else adjust(b, negDivAlg (<a, #2$*b>)))"
krauss@26056
   586
apply (rule negDivAlg_unfold [THEN trans])
krauss@26056
   587
apply (simp (no_asm_simp) add: vimage_iff not_zless_iff_zle [THEN iff_sym])
krauss@26056
   588
apply (blast intro: negDivAlg_termination)
krauss@26056
   589
done
krauss@26056
   590
krauss@26056
   591
lemma negDivAlg_induct_lemma [rule_format]:
krauss@26056
   592
  assumes prem:
krauss@26056
   593
        "!!a b. [| a \<in> int; b \<in> int;  
krauss@26056
   594
                   ~ (#0 $<= a $+ b | b $<= #0) --> P(<a, #2 $* b>) |]  
krauss@26056
   595
                ==> P(<a,b>)"
krauss@26056
   596
  shows "<u,v> \<in> int*int --> P(<u,v>)"
krauss@26056
   597
apply (rule_tac a = "<u,v>" in wf_induct)
krauss@26056
   598
apply (rule_tac A = "int*int" and f = "%<a,b>.nat_of ($- a $- b)" 
krauss@26056
   599
       in wf_measure)
krauss@26056
   600
apply clarify
krauss@26056
   601
apply (rule prem)
krauss@26056
   602
apply (drule_tac [3] x = "<xa, #2 $\<times> y>" in spec)
krauss@26056
   603
apply auto
krauss@26056
   604
apply (simp add: not_zle_iff_zless negDivAlg_termination)
krauss@26056
   605
done
krauss@26056
   606
krauss@26056
   607
lemma negDivAlg_induct [consumes 2]:
krauss@26056
   608
  assumes u_int: "u \<in> int"
krauss@26056
   609
      and v_int: "v \<in> int"
krauss@26056
   610
      and ih: "!!a b. [| a \<in> int; b \<in> int;  
krauss@26056
   611
                         ~ (#0 $<= a $+ b | b $<= #0) --> P(a, #2 $* b) |]  
krauss@26056
   612
                      ==> P(a,b)"
krauss@26056
   613
  shows "P(u,v)"
krauss@26056
   614
apply (subgoal_tac " (%<x,y>. P (x,y)) (<u,v>)")
krauss@26056
   615
apply simp
krauss@26056
   616
apply (rule negDivAlg_induct_lemma)
krauss@26056
   617
apply (simp (no_asm_use))
krauss@26056
   618
apply (rule ih)
krauss@26056
   619
apply (auto simp add: u_int v_int)
krauss@26056
   620
done
krauss@26056
   621
krauss@26056
   622
krauss@26056
   623
(*Typechecking for negDivAlg*)
krauss@26056
   624
lemma negDivAlg_type:
krauss@26056
   625
     "[| a \<in> int; b \<in> int |] ==> negDivAlg(<a,b>) \<in> int * int"
krauss@26056
   626
apply (rule_tac u = "a" and v = "b" in negDivAlg_induct)
krauss@26056
   627
apply assumption+
krauss@26056
   628
apply (case_tac "#0 $< ba")
krauss@26056
   629
 apply (simp add: negDivAlg_eqn adjust_def integ_of_type 
krauss@26056
   630
             split add: split_if_asm)
krauss@26056
   631
 apply clarify
krauss@26056
   632
 apply (simp add: int_0_less_mult_iff not_zle_iff_zless)
krauss@26056
   633
apply (simp add: not_zless_iff_zle)
krauss@26056
   634
apply (subst negDivAlg_unfold)
krauss@26056
   635
apply simp
krauss@26056
   636
done
krauss@26056
   637
krauss@26056
   638
krauss@26056
   639
(*Correctness of negDivAlg: it computes quotients correctly
krauss@26056
   640
  It doesn't work if a=0 because the 0/b=0 rather than -1*)
krauss@26056
   641
lemma negDivAlg_correct [rule_format]:
krauss@26056
   642
     "[| a \<in> int; b \<in> int |]  
krauss@26056
   643
      ==> a $< #0 --> #0 $< b --> quorem (<a,b>, negDivAlg(<a,b>))"
krauss@26056
   644
apply (rule_tac u = "a" and v = "b" in negDivAlg_induct)
krauss@26056
   645
  apply auto
krauss@26056
   646
   apply (simp_all add: quorem_def)
krauss@26056
   647
   txt{*base case: @{term "0$<=a$+b"}*}
krauss@26056
   648
   apply (simp add: negDivAlg_eqn)
krauss@26056
   649
  apply (simp add: not_zless_iff_zle [THEN iff_sym])
krauss@26056
   650
 apply (simp add: int_0_less_mult_iff)
krauss@26056
   651
txt{*main argument*}
krauss@26056
   652
apply (subst negDivAlg_eqn)
krauss@26056
   653
apply (simp_all (no_asm_simp))
krauss@26056
   654
apply (erule splitE)
krauss@26056
   655
apply (rule negDivAlg_type)
krauss@26056
   656
apply (simp_all add: int_0_less_mult_iff)
krauss@26056
   657
apply (auto simp add: zadd_zmult_distrib2 Let_def)
krauss@26056
   658
txt{*now just linear arithmetic*}
krauss@26056
   659
apply (simp add: not_zle_iff_zless zdiff_zless_iff)
krauss@26056
   660
done
krauss@26056
   661
krauss@26056
   662
krauss@26056
   663
subsection{* Existence shown by proving the division algorithm to be correct *}
krauss@26056
   664
krauss@26056
   665
(*the case a=0*)
krauss@26056
   666
lemma quorem_0: "[|b \<noteq> #0;  b \<in> int|] ==> quorem (<#0,b>, <#0,#0>)"
krauss@26056
   667
by (force simp add: quorem_def neq_iff_zless)
krauss@26056
   668
krauss@26056
   669
lemma posDivAlg_zero_divisor: "posDivAlg(<a,#0>) = <#0,a>"
krauss@26056
   670
apply (subst posDivAlg_unfold)
krauss@26056
   671
apply simp
krauss@26056
   672
done
krauss@26056
   673
krauss@26056
   674
lemma posDivAlg_0 [simp]: "posDivAlg (<#0,b>) = <#0,#0>"
krauss@26056
   675
apply (subst posDivAlg_unfold)
krauss@26056
   676
apply (simp add: not_zle_iff_zless)
krauss@26056
   677
done
krauss@26056
   678
krauss@26056
   679
krauss@26056
   680
(*Needed below.  Actually it's an equivalence.*)
krauss@26056
   681
lemma linear_arith_lemma: "~ (#0 $<= #-1 $+ b) ==> (b $<= #0)"
krauss@26056
   682
apply (simp add: not_zle_iff_zless)
krauss@26056
   683
apply (drule zminus_zless_zminus [THEN iffD2])
krauss@26056
   684
apply (simp add: zadd_commute zless_add1_iff_zle zle_zminus)
krauss@26056
   685
done
krauss@26056
   686
krauss@26056
   687
lemma negDivAlg_minus1 [simp]: "negDivAlg (<#-1,b>) = <#-1, b$-#1>"
krauss@26056
   688
apply (subst negDivAlg_unfold)
krauss@26056
   689
apply (simp add: linear_arith_lemma integ_of_type vimage_iff)
krauss@26056
   690
done
krauss@26056
   691
krauss@26056
   692
lemma negateSnd_eq [simp]: "negateSnd (<q,r>) = <q, $-r>"
krauss@26056
   693
apply (unfold negateSnd_def)
krauss@26056
   694
apply auto
krauss@26056
   695
done
krauss@26056
   696
krauss@26056
   697
lemma negateSnd_type: "qr \<in> int * int ==> negateSnd (qr) \<in> int * int"
krauss@26056
   698
apply (unfold negateSnd_def)
krauss@26056
   699
apply auto
krauss@26056
   700
done
krauss@26056
   701
krauss@26056
   702
lemma quorem_neg:
krauss@26056
   703
     "[|quorem (<$-a,$-b>, qr);  a \<in> int;  b \<in> int;  qr \<in> int * int|]   
krauss@26056
   704
      ==> quorem (<a,b>, negateSnd(qr))"
krauss@26056
   705
apply clarify
krauss@26056
   706
apply (auto elim: zless_asym simp add: quorem_def zless_zminus)
krauss@26056
   707
txt{*linear arithmetic from here on*}
krauss@26056
   708
apply (simp_all add: zminus_equation [of a] zminus_zless)
krauss@26056
   709
apply (cut_tac [2] z = "b" and w = "#0" in zless_linear)
krauss@26056
   710
apply (cut_tac [1] z = "b" and w = "#0" in zless_linear)
krauss@26056
   711
apply auto
krauss@26056
   712
apply (blast dest: zle_zless_trans)+
krauss@26056
   713
done
krauss@26056
   714
krauss@26056
   715
lemma divAlg_correct:
krauss@26056
   716
     "[|b \<noteq> #0;  a \<in> int;  b \<in> int|] ==> quorem (<a,b>, divAlg(<a,b>))"
krauss@26056
   717
apply (auto simp add: quorem_0 divAlg_def)
krauss@26056
   718
apply (safe intro!: quorem_neg posDivAlg_correct negDivAlg_correct
krauss@26056
   719
                    posDivAlg_type negDivAlg_type) 
krauss@26056
   720
apply (auto simp add: quorem_def neq_iff_zless)
krauss@26056
   721
txt{*linear arithmetic from here on*}
krauss@26056
   722
apply (auto simp add: zle_def)
krauss@26056
   723
done
krauss@26056
   724
krauss@26056
   725
lemma divAlg_type: "[|a \<in> int;  b \<in> int|] ==> divAlg(<a,b>) \<in> int * int"
krauss@26056
   726
apply (auto simp add: divAlg_def)
krauss@26056
   727
apply (auto simp add: posDivAlg_type negDivAlg_type negateSnd_type)
krauss@26056
   728
done
krauss@26056
   729
krauss@26056
   730
krauss@26056
   731
(** intify cancellation **)
krauss@26056
   732
krauss@26056
   733
lemma zdiv_intify1 [simp]: "intify(x) zdiv y = x zdiv y"
krauss@26056
   734
apply (simp (no_asm) add: zdiv_def)
krauss@26056
   735
done
krauss@26056
   736
krauss@26056
   737
lemma zdiv_intify2 [simp]: "x zdiv intify(y) = x zdiv y"
krauss@26056
   738
apply (simp (no_asm) add: zdiv_def)
krauss@26056
   739
done
krauss@26056
   740
krauss@26056
   741
lemma zdiv_type [iff,TC]: "z zdiv w \<in> int"
krauss@26056
   742
apply (unfold zdiv_def)
krauss@26056
   743
apply (blast intro: fst_type divAlg_type)
krauss@26056
   744
done
krauss@26056
   745
krauss@26056
   746
lemma zmod_intify1 [simp]: "intify(x) zmod y = x zmod y"
krauss@26056
   747
apply (simp (no_asm) add: zmod_def)
krauss@26056
   748
done
krauss@26056
   749
krauss@26056
   750
lemma zmod_intify2 [simp]: "x zmod intify(y) = x zmod y"
krauss@26056
   751
apply (simp (no_asm) add: zmod_def)
krauss@26056
   752
done
krauss@26056
   753
krauss@26056
   754
lemma zmod_type [iff,TC]: "z zmod w \<in> int"
krauss@26056
   755
apply (unfold zmod_def)
krauss@26056
   756
apply (rule snd_type)
krauss@26056
   757
apply (blast intro: divAlg_type)
krauss@26056
   758
done
krauss@26056
   759
krauss@26056
   760
krauss@26056
   761
(** Arbitrary definitions for division by zero.  Useful to simplify 
krauss@26056
   762
    certain equations **)
krauss@26056
   763
krauss@26056
   764
lemma DIVISION_BY_ZERO_ZDIV: "a zdiv #0 = #0"
krauss@26056
   765
apply (simp (no_asm) add: zdiv_def divAlg_def posDivAlg_zero_divisor)
krauss@26056
   766
done  (*NOT for adding to default simpset*)
krauss@26056
   767
krauss@26056
   768
lemma DIVISION_BY_ZERO_ZMOD: "a zmod #0 = intify(a)"
krauss@26056
   769
apply (simp (no_asm) add: zmod_def divAlg_def posDivAlg_zero_divisor)
krauss@26056
   770
done  (*NOT for adding to default simpset*)
krauss@26056
   771
krauss@26056
   772
krauss@26056
   773
krauss@26056
   774
(** Basic laws about division and remainder **)
krauss@26056
   775
krauss@26056
   776
lemma raw_zmod_zdiv_equality:
krauss@26056
   777
     "[| a \<in> int; b \<in> int |] ==> a = b $* (a zdiv b) $+ (a zmod b)"
krauss@26056
   778
apply (case_tac "b = #0")
krauss@26056
   779
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
   780
apply (cut_tac a = "a" and b = "b" in divAlg_correct)
krauss@26056
   781
apply (auto simp add: quorem_def zdiv_def zmod_def split_def)
krauss@26056
   782
done
krauss@26056
   783
krauss@26056
   784
lemma zmod_zdiv_equality: "intify(a) = b $* (a zdiv b) $+ (a zmod b)"
krauss@26056
   785
apply (rule trans)
krauss@26056
   786
apply (rule_tac b = "intify (b)" in raw_zmod_zdiv_equality)
krauss@26056
   787
apply auto
krauss@26056
   788
done
krauss@26056
   789
krauss@26056
   790
lemma pos_mod: "#0 $< b ==> #0 $<= a zmod b & a zmod b $< b"
krauss@26056
   791
apply (cut_tac a = "intify (a)" and b = "intify (b)" in divAlg_correct)
krauss@26056
   792
apply (auto simp add: intify_eq_0_iff_zle quorem_def zmod_def split_def)
krauss@26056
   793
apply (blast dest: zle_zless_trans)+
krauss@26056
   794
done
krauss@26056
   795
krauss@26056
   796
lemmas pos_mod_sign = pos_mod [THEN conjunct1, standard]
krauss@26056
   797
and    pos_mod_bound = pos_mod [THEN conjunct2, standard]
krauss@26056
   798
krauss@26056
   799
lemma neg_mod: "b $< #0 ==> a zmod b $<= #0 & b $< a zmod b"
krauss@26056
   800
apply (cut_tac a = "intify (a)" and b = "intify (b)" in divAlg_correct)
krauss@26056
   801
apply (auto simp add: intify_eq_0_iff_zle quorem_def zmod_def split_def)
krauss@26056
   802
apply (blast dest: zle_zless_trans)
krauss@26056
   803
apply (blast dest: zless_trans)+
krauss@26056
   804
done
krauss@26056
   805
krauss@26056
   806
lemmas neg_mod_sign = neg_mod [THEN conjunct1, standard]
krauss@26056
   807
and    neg_mod_bound = neg_mod [THEN conjunct2, standard]
krauss@26056
   808
krauss@26056
   809
krauss@26056
   810
(** proving general properties of zdiv and zmod **)
krauss@26056
   811
krauss@26056
   812
lemma quorem_div_mod:
krauss@26056
   813
     "[|b \<noteq> #0;  a \<in> int;  b \<in> int |]  
krauss@26056
   814
      ==> quorem (<a,b>, <a zdiv b, a zmod b>)"
krauss@26056
   815
apply (cut_tac a = "a" and b = "b" in zmod_zdiv_equality)
krauss@26056
   816
apply (auto simp add: quorem_def neq_iff_zless pos_mod_sign pos_mod_bound 
krauss@26056
   817
                      neg_mod_sign neg_mod_bound)
krauss@26056
   818
done
krauss@26056
   819
krauss@26056
   820
(*Surely quorem(<a,b>,<q,r>) implies a \<in> int, but it doesn't matter*)
krauss@26056
   821
lemma quorem_div:
krauss@26056
   822
     "[| quorem(<a,b>,<q,r>);  b \<noteq> #0;  a \<in> int;  b \<in> int;  q \<in> int |]  
krauss@26056
   823
      ==> a zdiv b = q"
krauss@26056
   824
by (blast intro: quorem_div_mod [THEN unique_quotient])
krauss@26056
   825
krauss@26056
   826
lemma quorem_mod:
krauss@26056
   827
     "[| quorem(<a,b>,<q,r>); b \<noteq> #0; a \<in> int; b \<in> int; q \<in> int; r \<in> int |] 
krauss@26056
   828
      ==> a zmod b = r"
krauss@26056
   829
by (blast intro: quorem_div_mod [THEN unique_remainder])
krauss@26056
   830
krauss@26056
   831
lemma zdiv_pos_pos_trivial_raw:
krauss@26056
   832
     "[| a \<in> int;  b \<in> int;  #0 $<= a;  a $< b |] ==> a zdiv b = #0"
krauss@26056
   833
apply (rule quorem_div)
krauss@26056
   834
apply (auto simp add: quorem_def)
krauss@26056
   835
(*linear arithmetic*)
krauss@26056
   836
apply (blast dest: zle_zless_trans)+
krauss@26056
   837
done
krauss@26056
   838
krauss@26056
   839
lemma zdiv_pos_pos_trivial: "[| #0 $<= a;  a $< b |] ==> a zdiv b = #0"
krauss@26056
   840
apply (cut_tac a = "intify (a)" and b = "intify (b)" 
krauss@26056
   841
       in zdiv_pos_pos_trivial_raw)
krauss@26056
   842
apply auto
krauss@26056
   843
done
krauss@26056
   844
krauss@26056
   845
lemma zdiv_neg_neg_trivial_raw:
krauss@26056
   846
     "[| a \<in> int;  b \<in> int;  a $<= #0;  b $< a |] ==> a zdiv b = #0"
krauss@26056
   847
apply (rule_tac r = "a" in quorem_div)
krauss@26056
   848
apply (auto simp add: quorem_def)
krauss@26056
   849
(*linear arithmetic*)
krauss@26056
   850
apply (blast dest: zle_zless_trans zless_trans)+
krauss@26056
   851
done
krauss@26056
   852
krauss@26056
   853
lemma zdiv_neg_neg_trivial: "[| a $<= #0;  b $< a |] ==> a zdiv b = #0"
krauss@26056
   854
apply (cut_tac a = "intify (a)" and b = "intify (b)" 
krauss@26056
   855
       in zdiv_neg_neg_trivial_raw)
krauss@26056
   856
apply auto
krauss@26056
   857
done
krauss@26056
   858
krauss@26056
   859
lemma zadd_le_0_lemma: "[| a$+b $<= #0;  #0 $< a;  #0 $< b |] ==> False"
krauss@26056
   860
apply (drule_tac z' = "#0" and z = "b" in zadd_zless_mono)
krauss@26056
   861
apply (auto simp add: zle_def)
krauss@26056
   862
apply (blast dest: zless_trans)
krauss@26056
   863
done
krauss@26056
   864
krauss@26056
   865
lemma zdiv_pos_neg_trivial_raw:
krauss@26056
   866
     "[| a \<in> int;  b \<in> int;  #0 $< a;  a$+b $<= #0 |] ==> a zdiv b = #-1"
krauss@26056
   867
apply (rule_tac r = "a $+ b" in quorem_div)
krauss@26056
   868
apply (auto simp add: quorem_def)
krauss@26056
   869
(*linear arithmetic*)
krauss@26056
   870
apply (blast dest: zadd_le_0_lemma zle_zless_trans)+
krauss@26056
   871
done
krauss@26056
   872
krauss@26056
   873
lemma zdiv_pos_neg_trivial: "[| #0 $< a;  a$+b $<= #0 |] ==> a zdiv b = #-1"
krauss@26056
   874
apply (cut_tac a = "intify (a)" and b = "intify (b)" 
krauss@26056
   875
       in zdiv_pos_neg_trivial_raw)
krauss@26056
   876
apply auto
krauss@26056
   877
done
krauss@26056
   878
krauss@26056
   879
(*There is no zdiv_neg_pos_trivial because  #0 zdiv b = #0 would supersede it*)
krauss@26056
   880
krauss@26056
   881
krauss@26056
   882
lemma zmod_pos_pos_trivial_raw:
krauss@26056
   883
     "[| a \<in> int;  b \<in> int;  #0 $<= a;  a $< b |] ==> a zmod b = a"
krauss@26056
   884
apply (rule_tac q = "#0" in quorem_mod)
krauss@26056
   885
apply (auto simp add: quorem_def)
krauss@26056
   886
(*linear arithmetic*)
krauss@26056
   887
apply (blast dest: zle_zless_trans)+
krauss@26056
   888
done
krauss@26056
   889
krauss@26056
   890
lemma zmod_pos_pos_trivial: "[| #0 $<= a;  a $< b |] ==> a zmod b = intify(a)"
krauss@26056
   891
apply (cut_tac a = "intify (a)" and b = "intify (b)" 
krauss@26056
   892
       in zmod_pos_pos_trivial_raw)
krauss@26056
   893
apply auto
krauss@26056
   894
done
krauss@26056
   895
krauss@26056
   896
lemma zmod_neg_neg_trivial_raw:
krauss@26056
   897
     "[| a \<in> int;  b \<in> int;  a $<= #0;  b $< a |] ==> a zmod b = a"
krauss@26056
   898
apply (rule_tac q = "#0" in quorem_mod)
krauss@26056
   899
apply (auto simp add: quorem_def)
krauss@26056
   900
(*linear arithmetic*)
krauss@26056
   901
apply (blast dest: zle_zless_trans zless_trans)+
krauss@26056
   902
done
krauss@26056
   903
krauss@26056
   904
lemma zmod_neg_neg_trivial: "[| a $<= #0;  b $< a |] ==> a zmod b = intify(a)"
krauss@26056
   905
apply (cut_tac a = "intify (a)" and b = "intify (b)" 
krauss@26056
   906
       in zmod_neg_neg_trivial_raw)
krauss@26056
   907
apply auto
krauss@26056
   908
done
krauss@26056
   909
krauss@26056
   910
lemma zmod_pos_neg_trivial_raw:
krauss@26056
   911
     "[| a \<in> int;  b \<in> int;  #0 $< a;  a$+b $<= #0 |] ==> a zmod b = a$+b"
krauss@26056
   912
apply (rule_tac q = "#-1" in quorem_mod)
krauss@26056
   913
apply (auto simp add: quorem_def)
krauss@26056
   914
(*linear arithmetic*)
krauss@26056
   915
apply (blast dest: zadd_le_0_lemma zle_zless_trans)+
krauss@26056
   916
done
krauss@26056
   917
krauss@26056
   918
lemma zmod_pos_neg_trivial: "[| #0 $< a;  a$+b $<= #0 |] ==> a zmod b = a$+b"
krauss@26056
   919
apply (cut_tac a = "intify (a)" and b = "intify (b)" 
krauss@26056
   920
       in zmod_pos_neg_trivial_raw)
krauss@26056
   921
apply auto
krauss@26056
   922
done
krauss@26056
   923
krauss@26056
   924
(*There is no zmod_neg_pos_trivial...*)
krauss@26056
   925
krauss@26056
   926
krauss@26056
   927
(*Simpler laws such as -a zdiv b = -(a zdiv b) FAIL*)
krauss@26056
   928
krauss@26056
   929
lemma zdiv_zminus_zminus_raw:
krauss@26056
   930
     "[|a \<in> int;  b \<in> int|] ==> ($-a) zdiv ($-b) = a zdiv b"
krauss@26056
   931
apply (case_tac "b = #0")
krauss@26056
   932
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
   933
apply (subst quorem_div_mod [THEN quorem_neg, simplified, THEN quorem_div])
krauss@26056
   934
apply auto
krauss@26056
   935
done
krauss@26056
   936
krauss@26056
   937
lemma zdiv_zminus_zminus [simp]: "($-a) zdiv ($-b) = a zdiv b"
krauss@26056
   938
apply (cut_tac a = "intify (a)" and b = "intify (b)" in zdiv_zminus_zminus_raw)
krauss@26056
   939
apply auto
krauss@26056
   940
done
krauss@26056
   941
krauss@26056
   942
(*Simpler laws such as -a zmod b = -(a zmod b) FAIL*)
krauss@26056
   943
lemma zmod_zminus_zminus_raw:
krauss@26056
   944
     "[|a \<in> int;  b \<in> int|] ==> ($-a) zmod ($-b) = $- (a zmod b)"
krauss@26056
   945
apply (case_tac "b = #0")
krauss@26056
   946
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
   947
apply (subst quorem_div_mod [THEN quorem_neg, simplified, THEN quorem_mod])
krauss@26056
   948
apply auto
krauss@26056
   949
done
krauss@26056
   950
krauss@26056
   951
lemma zmod_zminus_zminus [simp]: "($-a) zmod ($-b) = $- (a zmod b)"
krauss@26056
   952
apply (cut_tac a = "intify (a)" and b = "intify (b)" in zmod_zminus_zminus_raw)
krauss@26056
   953
apply auto
krauss@26056
   954
done
krauss@26056
   955
krauss@26056
   956
krauss@26056
   957
subsection{* division of a number by itself *}
krauss@26056
   958
krauss@26056
   959
lemma self_quotient_aux1: "[| #0 $< a; a = r $+ a$*q; r $< a |] ==> #1 $<= q"
krauss@26056
   960
apply (subgoal_tac "#0 $< a$*q")
krauss@26056
   961
apply (cut_tac w = "#0" and z = "q" in add1_zle_iff)
krauss@26056
   962
apply (simp add: int_0_less_mult_iff)
krauss@26056
   963
apply (blast dest: zless_trans)
krauss@26056
   964
(*linear arithmetic...*)
krauss@26056
   965
apply (drule_tac t = "%x. x $- r" in subst_context)
krauss@26056
   966
apply (drule sym)
krauss@26056
   967
apply (simp add: zcompare_rls)
krauss@26056
   968
done
krauss@26056
   969
krauss@26056
   970
lemma self_quotient_aux2: "[| #0 $< a; a = r $+ a$*q; #0 $<= r |] ==> q $<= #1"
krauss@26056
   971
apply (subgoal_tac "#0 $<= a$* (#1$-q)")
krauss@26056
   972
 apply (simp add: int_0_le_mult_iff zcompare_rls)
krauss@26056
   973
 apply (blast dest: zle_zless_trans)
krauss@26056
   974
apply (simp add: zdiff_zmult_distrib2)
krauss@26056
   975
apply (drule_tac t = "%x. x $- a $* q" in subst_context)
krauss@26056
   976
apply (simp add: zcompare_rls)
krauss@26056
   977
done
krauss@26056
   978
krauss@26056
   979
lemma self_quotient:
krauss@26056
   980
     "[| quorem(<a,a>,<q,r>);  a \<in> int;  q \<in> int;  a \<noteq> #0|] ==> q = #1"
krauss@26056
   981
apply (simp add: split_ifs quorem_def neq_iff_zless)
krauss@26056
   982
apply (rule zle_anti_sym)
krauss@26056
   983
apply safe
krauss@26056
   984
apply auto
krauss@26056
   985
prefer 4 apply (blast dest: zless_trans)
krauss@26056
   986
apply (blast dest: zless_trans)
krauss@26056
   987
apply (rule_tac [3] a = "$-a" and r = "$-r" in self_quotient_aux1)
krauss@26056
   988
apply (rule_tac a = "$-a" and r = "$-r" in self_quotient_aux2)
krauss@26056
   989
apply (rule_tac [6] zminus_equation [THEN iffD1])
krauss@26056
   990
apply (rule_tac [2] zminus_equation [THEN iffD1])
krauss@26056
   991
apply (force intro: self_quotient_aux1 self_quotient_aux2
krauss@26056
   992
  simp add: zadd_commute zmult_zminus)+
krauss@26056
   993
done
krauss@26056
   994
krauss@26056
   995
lemma self_remainder:
krauss@26056
   996
     "[|quorem(<a,a>,<q,r>); a \<in> int; q \<in> int; r \<in> int; a \<noteq> #0|] ==> r = #0"
krauss@26056
   997
apply (frule self_quotient)
krauss@26056
   998
apply (auto simp add: quorem_def)
krauss@26056
   999
done
krauss@26056
  1000
krauss@26056
  1001
lemma zdiv_self_raw: "[|a \<noteq> #0; a \<in> int|] ==> a zdiv a = #1"
krauss@26056
  1002
apply (blast intro: quorem_div_mod [THEN self_quotient])
krauss@26056
  1003
done
krauss@26056
  1004
krauss@26056
  1005
lemma zdiv_self [simp]: "intify(a) \<noteq> #0 ==> a zdiv a = #1"
krauss@26056
  1006
apply (drule zdiv_self_raw)
krauss@26056
  1007
apply auto
krauss@26056
  1008
done
krauss@26056
  1009
krauss@26056
  1010
(*Here we have 0 zmod 0 = 0, also assumed by Knuth (who puts m zmod 0 = 0) *)
krauss@26056
  1011
lemma zmod_self_raw: "a \<in> int ==> a zmod a = #0"
krauss@26056
  1012
apply (case_tac "a = #0")
krauss@26056
  1013
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1014
apply (blast intro: quorem_div_mod [THEN self_remainder])
krauss@26056
  1015
done
krauss@26056
  1016
krauss@26056
  1017
lemma zmod_self [simp]: "a zmod a = #0"
krauss@26056
  1018
apply (cut_tac a = "intify (a)" in zmod_self_raw)
krauss@26056
  1019
apply auto
krauss@26056
  1020
done
krauss@26056
  1021
krauss@26056
  1022
krauss@26056
  1023
subsection{* Computation of division and remainder *}
krauss@26056
  1024
krauss@26056
  1025
lemma zdiv_zero [simp]: "#0 zdiv b = #0"
krauss@26056
  1026
apply (simp (no_asm) add: zdiv_def divAlg_def)
krauss@26056
  1027
done
krauss@26056
  1028
krauss@26056
  1029
lemma zdiv_eq_minus1: "#0 $< b ==> #-1 zdiv b = #-1"
krauss@26056
  1030
apply (simp (no_asm_simp) add: zdiv_def divAlg_def)
krauss@26056
  1031
done
krauss@26056
  1032
krauss@26056
  1033
lemma zmod_zero [simp]: "#0 zmod b = #0"
krauss@26056
  1034
apply (simp (no_asm) add: zmod_def divAlg_def)
krauss@26056
  1035
done
krauss@26056
  1036
krauss@26056
  1037
lemma zdiv_minus1: "#0 $< b ==> #-1 zdiv b = #-1"
krauss@26056
  1038
apply (simp (no_asm_simp) add: zdiv_def divAlg_def)
krauss@26056
  1039
done
krauss@26056
  1040
krauss@26056
  1041
lemma zmod_minus1: "#0 $< b ==> #-1 zmod b = b $- #1"
krauss@26056
  1042
apply (simp (no_asm_simp) add: zmod_def divAlg_def)
krauss@26056
  1043
done
krauss@26056
  1044
krauss@26056
  1045
(** a positive, b positive **)
krauss@26056
  1046
krauss@26056
  1047
lemma zdiv_pos_pos: "[| #0 $< a;  #0 $<= b |]  
krauss@26056
  1048
      ==> a zdiv b = fst (posDivAlg(<intify(a), intify(b)>))"
krauss@26056
  1049
apply (simp (no_asm_simp) add: zdiv_def divAlg_def)
krauss@26056
  1050
apply (auto simp add: zle_def)
krauss@26056
  1051
done
krauss@26056
  1052
krauss@26056
  1053
lemma zmod_pos_pos:
krauss@26056
  1054
     "[| #0 $< a;  #0 $<= b |]  
krauss@26056
  1055
      ==> a zmod b = snd (posDivAlg(<intify(a), intify(b)>))"
krauss@26056
  1056
apply (simp (no_asm_simp) add: zmod_def divAlg_def)
krauss@26056
  1057
apply (auto simp add: zle_def)
krauss@26056
  1058
done
krauss@26056
  1059
krauss@26056
  1060
(** a negative, b positive **)
krauss@26056
  1061
krauss@26056
  1062
lemma zdiv_neg_pos:
krauss@26056
  1063
     "[| a $< #0;  #0 $< b |]  
krauss@26056
  1064
      ==> a zdiv b = fst (negDivAlg(<intify(a), intify(b)>))"
krauss@26056
  1065
apply (simp (no_asm_simp) add: zdiv_def divAlg_def)
krauss@26056
  1066
apply (blast dest: zle_zless_trans)
krauss@26056
  1067
done
krauss@26056
  1068
krauss@26056
  1069
lemma zmod_neg_pos:
krauss@26056
  1070
     "[| a $< #0;  #0 $< b |]  
krauss@26056
  1071
      ==> a zmod b = snd (negDivAlg(<intify(a), intify(b)>))"
krauss@26056
  1072
apply (simp (no_asm_simp) add: zmod_def divAlg_def)
krauss@26056
  1073
apply (blast dest: zle_zless_trans)
krauss@26056
  1074
done
krauss@26056
  1075
krauss@26056
  1076
(** a positive, b negative **)
krauss@26056
  1077
krauss@26056
  1078
lemma zdiv_pos_neg:
krauss@26056
  1079
     "[| #0 $< a;  b $< #0 |]  
krauss@26056
  1080
      ==> a zdiv b = fst (negateSnd(negDivAlg (<$-a, $-b>)))"
krauss@26056
  1081
apply (simp (no_asm_simp) add: zdiv_def divAlg_def intify_eq_0_iff_zle)
krauss@26056
  1082
apply auto
krauss@26056
  1083
apply (blast dest: zle_zless_trans)+
krauss@26056
  1084
apply (blast dest: zless_trans)
krauss@26056
  1085
apply (blast intro: zless_imp_zle)
krauss@26056
  1086
done
krauss@26056
  1087
krauss@26056
  1088
lemma zmod_pos_neg:
krauss@26056
  1089
     "[| #0 $< a;  b $< #0 |]  
krauss@26056
  1090
      ==> a zmod b = snd (negateSnd(negDivAlg (<$-a, $-b>)))"
krauss@26056
  1091
apply (simp (no_asm_simp) add: zmod_def divAlg_def intify_eq_0_iff_zle)
krauss@26056
  1092
apply auto
krauss@26056
  1093
apply (blast dest: zle_zless_trans)+
krauss@26056
  1094
apply (blast dest: zless_trans)
krauss@26056
  1095
apply (blast intro: zless_imp_zle)
krauss@26056
  1096
done
krauss@26056
  1097
krauss@26056
  1098
(** a negative, b negative **)
krauss@26056
  1099
krauss@26056
  1100
lemma zdiv_neg_neg:
krauss@26056
  1101
     "[| a $< #0;  b $<= #0 |]  
krauss@26056
  1102
      ==> a zdiv b = fst (negateSnd(posDivAlg(<$-a, $-b>)))"
krauss@26056
  1103
apply (simp (no_asm_simp) add: zdiv_def divAlg_def)
krauss@26056
  1104
apply auto
krauss@26056
  1105
apply (blast dest!: zle_zless_trans)+
krauss@26056
  1106
done
krauss@26056
  1107
krauss@26056
  1108
lemma zmod_neg_neg:
krauss@26056
  1109
     "[| a $< #0;  b $<= #0 |]  
krauss@26056
  1110
      ==> a zmod b = snd (negateSnd(posDivAlg(<$-a, $-b>)))"
krauss@26056
  1111
apply (simp (no_asm_simp) add: zmod_def divAlg_def)
krauss@26056
  1112
apply auto
krauss@26056
  1113
apply (blast dest!: zle_zless_trans)+
krauss@26056
  1114
done
krauss@26056
  1115
krauss@26056
  1116
declare zdiv_pos_pos [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1117
declare zdiv_neg_pos [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1118
declare zdiv_pos_neg [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1119
declare zdiv_neg_neg [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1120
declare zmod_pos_pos [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1121
declare zmod_neg_pos [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1122
declare zmod_pos_neg [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1123
declare zmod_neg_neg [of "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1124
declare posDivAlg_eqn [of concl: "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1125
declare negDivAlg_eqn [of concl: "integ_of (v)" "integ_of (w)", standard, simp]
krauss@26056
  1126
krauss@26056
  1127
krauss@26056
  1128
(** Special-case simplification **)
krauss@26056
  1129
krauss@26056
  1130
lemma zmod_1 [simp]: "a zmod #1 = #0"
krauss@26056
  1131
apply (cut_tac a = "a" and b = "#1" in pos_mod_sign)
krauss@26056
  1132
apply (cut_tac [2] a = "a" and b = "#1" in pos_mod_bound)
krauss@26056
  1133
apply auto
krauss@26056
  1134
(*arithmetic*)
krauss@26056
  1135
apply (drule add1_zle_iff [THEN iffD2])
krauss@26056
  1136
apply (rule zle_anti_sym)
krauss@26056
  1137
apply auto
krauss@26056
  1138
done
krauss@26056
  1139
krauss@26056
  1140
lemma zdiv_1 [simp]: "a zdiv #1 = intify(a)"
krauss@26056
  1141
apply (cut_tac a = "a" and b = "#1" in zmod_zdiv_equality)
krauss@26056
  1142
apply auto
krauss@26056
  1143
done
krauss@26056
  1144
krauss@26056
  1145
lemma zmod_minus1_right [simp]: "a zmod #-1 = #0"
krauss@26056
  1146
apply (cut_tac a = "a" and b = "#-1" in neg_mod_sign)
krauss@26056
  1147
apply (cut_tac [2] a = "a" and b = "#-1" in neg_mod_bound)
krauss@26056
  1148
apply auto
krauss@26056
  1149
(*arithmetic*)
krauss@26056
  1150
apply (drule add1_zle_iff [THEN iffD2])
krauss@26056
  1151
apply (rule zle_anti_sym)
krauss@26056
  1152
apply auto
krauss@26056
  1153
done
krauss@26056
  1154
krauss@26056
  1155
lemma zdiv_minus1_right_raw: "a \<in> int ==> a zdiv #-1 = $-a"
krauss@26056
  1156
apply (cut_tac a = "a" and b = "#-1" in zmod_zdiv_equality)
krauss@26056
  1157
apply auto
krauss@26056
  1158
apply (rule equation_zminus [THEN iffD2])
krauss@26056
  1159
apply auto
krauss@26056
  1160
done
krauss@26056
  1161
krauss@26056
  1162
lemma zdiv_minus1_right: "a zdiv #-1 = $-a"
krauss@26056
  1163
apply (cut_tac a = "intify (a)" in zdiv_minus1_right_raw)
krauss@26056
  1164
apply auto
krauss@26056
  1165
done
krauss@26056
  1166
declare zdiv_minus1_right [simp]
krauss@26056
  1167
krauss@26056
  1168
krauss@26056
  1169
subsection{* Monotonicity in the first argument (divisor) *}
krauss@26056
  1170
krauss@26056
  1171
lemma zdiv_mono1: "[| a $<= a';  #0 $< b |] ==> a zdiv b $<= a' zdiv b"
krauss@26056
  1172
apply (cut_tac a = "a" and b = "b" in zmod_zdiv_equality)
krauss@26056
  1173
apply (cut_tac a = "a'" and b = "b" in zmod_zdiv_equality)
krauss@26056
  1174
apply (rule unique_quotient_lemma)
krauss@26056
  1175
apply (erule subst)
krauss@26056
  1176
apply (erule subst)
krauss@26056
  1177
apply (simp_all (no_asm_simp) add: pos_mod_sign pos_mod_bound)
krauss@26056
  1178
done
krauss@26056
  1179
krauss@26056
  1180
lemma zdiv_mono1_neg: "[| a $<= a';  b $< #0 |] ==> a' zdiv b $<= a zdiv b"
krauss@26056
  1181
apply (cut_tac a = "a" and b = "b" in zmod_zdiv_equality)
krauss@26056
  1182
apply (cut_tac a = "a'" and b = "b" in zmod_zdiv_equality)
krauss@26056
  1183
apply (rule unique_quotient_lemma_neg)
krauss@26056
  1184
apply (erule subst)
krauss@26056
  1185
apply (erule subst)
krauss@26056
  1186
apply (simp_all (no_asm_simp) add: neg_mod_sign neg_mod_bound)
krauss@26056
  1187
done
krauss@26056
  1188
krauss@26056
  1189
krauss@26056
  1190
subsection{* Monotonicity in the second argument (dividend) *}
krauss@26056
  1191
krauss@26056
  1192
lemma q_pos_lemma:
krauss@26056
  1193
     "[| #0 $<= b'$*q' $+ r'; r' $< b';  #0 $< b' |] ==> #0 $<= q'"
krauss@26056
  1194
apply (subgoal_tac "#0 $< b'$* (q' $+ #1)")
krauss@26056
  1195
 apply (simp add: int_0_less_mult_iff)
krauss@26056
  1196
 apply (blast dest: zless_trans intro: zless_add1_iff_zle [THEN iffD1])
krauss@26056
  1197
apply (simp add: zadd_zmult_distrib2)
krauss@26056
  1198
apply (erule zle_zless_trans)
krauss@26056
  1199
apply (erule zadd_zless_mono2)
krauss@26056
  1200
done
krauss@26056
  1201
krauss@26056
  1202
lemma zdiv_mono2_lemma:
krauss@26056
  1203
     "[| b$*q $+ r = b'$*q' $+ r';  #0 $<= b'$*q' $+ r';   
krauss@26056
  1204
         r' $< b';  #0 $<= r;  #0 $< b';  b' $<= b |]   
krauss@26056
  1205
      ==> q $<= q'"
krauss@26056
  1206
apply (frule q_pos_lemma, assumption+) 
krauss@26056
  1207
apply (subgoal_tac "b$*q $< b$* (q' $+ #1)")
krauss@26056
  1208
 apply (simp add: zmult_zless_cancel1)
krauss@26056
  1209
 apply (force dest: zless_add1_iff_zle [THEN iffD1] zless_trans zless_zle_trans)
krauss@26056
  1210
apply (subgoal_tac "b$*q = r' $- r $+ b'$*q'")
krauss@26056
  1211
 prefer 2 apply (simp add: zcompare_rls)
krauss@26056
  1212
apply (simp (no_asm_simp) add: zadd_zmult_distrib2)
krauss@26056
  1213
apply (subst zadd_commute [of "b $\<times> q'"], rule zadd_zless_mono)
krauss@26056
  1214
 prefer 2 apply (blast intro: zmult_zle_mono1)
krauss@26056
  1215
apply (subgoal_tac "r' $+ #0 $< b $+ r")
krauss@26056
  1216
 apply (simp add: zcompare_rls)
krauss@26056
  1217
apply (rule zadd_zless_mono)
krauss@26056
  1218
 apply auto
krauss@26056
  1219
apply (blast dest: zless_zle_trans)
krauss@26056
  1220
done
krauss@26056
  1221
krauss@26056
  1222
krauss@26056
  1223
lemma zdiv_mono2_raw:
krauss@26056
  1224
     "[| #0 $<= a;  #0 $< b';  b' $<= b;  a \<in> int |]   
krauss@26056
  1225
      ==> a zdiv b $<= a zdiv b'"
krauss@26056
  1226
apply (subgoal_tac "#0 $< b")
krauss@26056
  1227
 prefer 2 apply (blast dest: zless_zle_trans)
krauss@26056
  1228
apply (cut_tac a = "a" and b = "b" in zmod_zdiv_equality)
krauss@26056
  1229
apply (cut_tac a = "a" and b = "b'" in zmod_zdiv_equality)
krauss@26056
  1230
apply (rule zdiv_mono2_lemma)
krauss@26056
  1231
apply (erule subst)
krauss@26056
  1232
apply (erule subst)
krauss@26056
  1233
apply (simp_all add: pos_mod_sign pos_mod_bound)
krauss@26056
  1234
done
krauss@26056
  1235
krauss@26056
  1236
lemma zdiv_mono2:
krauss@26056
  1237
     "[| #0 $<= a;  #0 $< b';  b' $<= b |]   
krauss@26056
  1238
      ==> a zdiv b $<= a zdiv b'"
krauss@26056
  1239
apply (cut_tac a = "intify (a)" in zdiv_mono2_raw)
krauss@26056
  1240
apply auto
krauss@26056
  1241
done
krauss@26056
  1242
krauss@26056
  1243
lemma q_neg_lemma:
krauss@26056
  1244
     "[| b'$*q' $+ r' $< #0;  #0 $<= r';  #0 $< b' |] ==> q' $< #0"
krauss@26056
  1245
apply (subgoal_tac "b'$*q' $< #0")
krauss@26056
  1246
 prefer 2 apply (force intro: zle_zless_trans)
krauss@26056
  1247
apply (simp add: zmult_less_0_iff)
krauss@26056
  1248
apply (blast dest: zless_trans)
krauss@26056
  1249
done
krauss@26056
  1250
krauss@26056
  1251
krauss@26056
  1252
krauss@26056
  1253
lemma zdiv_mono2_neg_lemma:
krauss@26056
  1254
     "[| b$*q $+ r = b'$*q' $+ r';  b'$*q' $+ r' $< #0;   
krauss@26056
  1255
         r $< b;  #0 $<= r';  #0 $< b';  b' $<= b |]   
krauss@26056
  1256
      ==> q' $<= q"
krauss@26056
  1257
apply (subgoal_tac "#0 $< b")
krauss@26056
  1258
 prefer 2 apply (blast dest: zless_zle_trans)
krauss@26056
  1259
apply (frule q_neg_lemma, assumption+) 
krauss@26056
  1260
apply (subgoal_tac "b$*q' $< b$* (q $+ #1)")
krauss@26056
  1261
 apply (simp add: zmult_zless_cancel1)
krauss@26056
  1262
 apply (blast dest: zless_trans zless_add1_iff_zle [THEN iffD1])
krauss@26056
  1263
apply (simp (no_asm_simp) add: zadd_zmult_distrib2)
krauss@26056
  1264
apply (subgoal_tac "b$*q' $<= b'$*q'")
krauss@26056
  1265
 prefer 2
krauss@26056
  1266
 apply (simp add: zmult_zle_cancel2)
krauss@26056
  1267
 apply (blast dest: zless_trans)
krauss@26056
  1268
apply (subgoal_tac "b'$*q' $+ r $< b $+ (b$*q $+ r)")
krauss@26056
  1269
 prefer 2
krauss@26056
  1270
 apply (erule ssubst)
krauss@26056
  1271
 apply simp
krauss@26056
  1272
 apply (drule_tac w' = "r" and z' = "#0" in zadd_zless_mono)
krauss@26056
  1273
  apply (assumption)
krauss@26056
  1274
 apply simp
krauss@26056
  1275
apply (simp (no_asm_use) add: zadd_commute)
krauss@26056
  1276
apply (rule zle_zless_trans)
krauss@26056
  1277
 prefer 2 apply (assumption)
krauss@26056
  1278
apply (simp (no_asm_simp) add: zmult_zle_cancel2)
krauss@26056
  1279
apply (blast dest: zless_trans)
krauss@26056
  1280
done
krauss@26056
  1281
krauss@26056
  1282
lemma zdiv_mono2_neg_raw:
krauss@26056
  1283
     "[| a $< #0;  #0 $< b';  b' $<= b;  a \<in> int |]   
krauss@26056
  1284
      ==> a zdiv b' $<= a zdiv b"
krauss@26056
  1285
apply (subgoal_tac "#0 $< b")
krauss@26056
  1286
 prefer 2 apply (blast dest: zless_zle_trans)
krauss@26056
  1287
apply (cut_tac a = "a" and b = "b" in zmod_zdiv_equality)
krauss@26056
  1288
apply (cut_tac a = "a" and b = "b'" in zmod_zdiv_equality)
krauss@26056
  1289
apply (rule zdiv_mono2_neg_lemma)
krauss@26056
  1290
apply (erule subst)
krauss@26056
  1291
apply (erule subst)
krauss@26056
  1292
apply (simp_all add: pos_mod_sign pos_mod_bound)
krauss@26056
  1293
done
krauss@26056
  1294
krauss@26056
  1295
lemma zdiv_mono2_neg: "[| a $< #0;  #0 $< b';  b' $<= b |]   
krauss@26056
  1296
      ==> a zdiv b' $<= a zdiv b"
krauss@26056
  1297
apply (cut_tac a = "intify (a)" in zdiv_mono2_neg_raw)
krauss@26056
  1298
apply auto
krauss@26056
  1299
done
krauss@26056
  1300
krauss@26056
  1301
krauss@26056
  1302
krauss@26056
  1303
subsection{* More algebraic laws for zdiv and zmod *}
krauss@26056
  1304
krauss@26056
  1305
(** proving (a*b) zdiv c = a $* (b zdiv c) $+ a * (b zmod c) **)
krauss@26056
  1306
krauss@26056
  1307
lemma zmult1_lemma:
krauss@26056
  1308
     "[| quorem(<b,c>, <q,r>);  c \<in> int;  c \<noteq> #0 |]  
krauss@26056
  1309
      ==> quorem (<a$*b, c>, <a$*q $+ (a$*r) zdiv c, (a$*r) zmod c>)"
krauss@26056
  1310
apply (auto simp add: split_ifs quorem_def neq_iff_zless zadd_zmult_distrib2
krauss@26056
  1311
                      pos_mod_sign pos_mod_bound neg_mod_sign neg_mod_bound)
krauss@26056
  1312
apply (auto intro: raw_zmod_zdiv_equality) 
krauss@26056
  1313
done
krauss@26056
  1314
krauss@26056
  1315
lemma zdiv_zmult1_eq_raw:
krauss@26056
  1316
     "[|b \<in> int;  c \<in> int|]  
krauss@26056
  1317
      ==> (a$*b) zdiv c = a$*(b zdiv c) $+ a$*(b zmod c) zdiv c"
krauss@26056
  1318
apply (case_tac "c = #0")
krauss@26056
  1319
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1320
apply (rule quorem_div_mod [THEN zmult1_lemma, THEN quorem_div])
krauss@26056
  1321
apply auto
krauss@26056
  1322
done
krauss@26056
  1323
krauss@26056
  1324
lemma zdiv_zmult1_eq: "(a$*b) zdiv c = a$*(b zdiv c) $+ a$*(b zmod c) zdiv c"
krauss@26056
  1325
apply (cut_tac b = "intify (b)" and c = "intify (c)" in zdiv_zmult1_eq_raw)
krauss@26056
  1326
apply auto
krauss@26056
  1327
done
krauss@26056
  1328
krauss@26056
  1329
lemma zmod_zmult1_eq_raw:
krauss@26056
  1330
     "[|b \<in> int;  c \<in> int|] ==> (a$*b) zmod c = a$*(b zmod c) zmod c"
krauss@26056
  1331
apply (case_tac "c = #0")
krauss@26056
  1332
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1333
apply (rule quorem_div_mod [THEN zmult1_lemma, THEN quorem_mod])
krauss@26056
  1334
apply auto
krauss@26056
  1335
done
krauss@26056
  1336
krauss@26056
  1337
lemma zmod_zmult1_eq: "(a$*b) zmod c = a$*(b zmod c) zmod c"
krauss@26056
  1338
apply (cut_tac b = "intify (b)" and c = "intify (c)" in zmod_zmult1_eq_raw)
krauss@26056
  1339
apply auto
krauss@26056
  1340
done
krauss@26056
  1341
krauss@26056
  1342
lemma zmod_zmult1_eq': "(a$*b) zmod c = ((a zmod c) $* b) zmod c"
krauss@26056
  1343
apply (rule trans)
krauss@26056
  1344
apply (rule_tac b = " (b $* a) zmod c" in trans)
krauss@26056
  1345
apply (rule_tac [2] zmod_zmult1_eq)
krauss@26056
  1346
apply (simp_all (no_asm) add: zmult_commute)
krauss@26056
  1347
done
krauss@26056
  1348
krauss@26056
  1349
lemma zmod_zmult_distrib: "(a$*b) zmod c = ((a zmod c) $* (b zmod c)) zmod c"
krauss@26056
  1350
apply (rule zmod_zmult1_eq' [THEN trans])
krauss@26056
  1351
apply (rule zmod_zmult1_eq)
krauss@26056
  1352
done
krauss@26056
  1353
krauss@26056
  1354
lemma zdiv_zmult_self1 [simp]: "intify(b) \<noteq> #0 ==> (a$*b) zdiv b = intify(a)"
krauss@26056
  1355
apply (simp (no_asm_simp) add: zdiv_zmult1_eq)
krauss@26056
  1356
done
krauss@26056
  1357
krauss@26056
  1358
lemma zdiv_zmult_self2 [simp]: "intify(b) \<noteq> #0 ==> (b$*a) zdiv b = intify(a)"
krauss@26056
  1359
apply (subst zmult_commute , erule zdiv_zmult_self1)
krauss@26056
  1360
done
krauss@26056
  1361
krauss@26056
  1362
lemma zmod_zmult_self1 [simp]: "(a$*b) zmod b = #0"
krauss@26056
  1363
apply (simp (no_asm) add: zmod_zmult1_eq)
krauss@26056
  1364
done
krauss@26056
  1365
krauss@26056
  1366
lemma zmod_zmult_self2 [simp]: "(b$*a) zmod b = #0"
krauss@26056
  1367
apply (simp (no_asm) add: zmult_commute zmod_zmult1_eq)
krauss@26056
  1368
done
krauss@26056
  1369
krauss@26056
  1370
krauss@26056
  1371
(** proving (a$+b) zdiv c = 
krauss@26056
  1372
            a zdiv c $+ b zdiv c $+ ((a zmod c $+ b zmod c) zdiv c) **)
krauss@26056
  1373
krauss@26056
  1374
lemma zadd1_lemma:
krauss@26056
  1375
     "[| quorem(<a,c>, <aq,ar>);  quorem(<b,c>, <bq,br>);   
krauss@26056
  1376
         c \<in> int;  c \<noteq> #0 |]  
krauss@26056
  1377
      ==> quorem (<a$+b, c>, <aq $+ bq $+ (ar$+br) zdiv c, (ar$+br) zmod c>)"
krauss@26056
  1378
apply (auto simp add: split_ifs quorem_def neq_iff_zless zadd_zmult_distrib2
krauss@26056
  1379
                      pos_mod_sign pos_mod_bound neg_mod_sign neg_mod_bound)
krauss@26056
  1380
apply (auto intro: raw_zmod_zdiv_equality)
krauss@26056
  1381
done
krauss@26056
  1382
krauss@26056
  1383
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
krauss@26056
  1384
lemma zdiv_zadd1_eq_raw:
krauss@26056
  1385
     "[|a \<in> int; b \<in> int; c \<in> int|] ==>  
krauss@26056
  1386
      (a$+b) zdiv c = a zdiv c $+ b zdiv c $+ ((a zmod c $+ b zmod c) zdiv c)"
krauss@26056
  1387
apply (case_tac "c = #0")
krauss@26056
  1388
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1389
apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod,
krauss@26056
  1390
                                 THEN quorem_div])
krauss@26056
  1391
done
krauss@26056
  1392
krauss@26056
  1393
lemma zdiv_zadd1_eq:
krauss@26056
  1394
     "(a$+b) zdiv c = a zdiv c $+ b zdiv c $+ ((a zmod c $+ b zmod c) zdiv c)"
krauss@26056
  1395
apply (cut_tac a = "intify (a)" and b = "intify (b)" and c = "intify (c)" 
krauss@26056
  1396
       in zdiv_zadd1_eq_raw)
krauss@26056
  1397
apply auto
krauss@26056
  1398
done
krauss@26056
  1399
krauss@26056
  1400
lemma zmod_zadd1_eq_raw:
krauss@26056
  1401
     "[|a \<in> int; b \<in> int; c \<in> int|]   
krauss@26056
  1402
      ==> (a$+b) zmod c = (a zmod c $+ b zmod c) zmod c"
krauss@26056
  1403
apply (case_tac "c = #0")
krauss@26056
  1404
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1405
apply (blast intro: zadd1_lemma [OF quorem_div_mod quorem_div_mod, 
krauss@26056
  1406
                                 THEN quorem_mod])
krauss@26056
  1407
done
krauss@26056
  1408
krauss@26056
  1409
lemma zmod_zadd1_eq: "(a$+b) zmod c = (a zmod c $+ b zmod c) zmod c"
krauss@26056
  1410
apply (cut_tac a = "intify (a)" and b = "intify (b)" and c = "intify (c)" 
krauss@26056
  1411
       in zmod_zadd1_eq_raw)
krauss@26056
  1412
apply auto
krauss@26056
  1413
done
krauss@26056
  1414
krauss@26056
  1415
lemma zmod_div_trivial_raw:
krauss@26056
  1416
     "[|a \<in> int; b \<in> int|] ==> (a zmod b) zdiv b = #0"
krauss@26056
  1417
apply (case_tac "b = #0")
krauss@26056
  1418
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1419
apply (auto simp add: neq_iff_zless pos_mod_sign pos_mod_bound
krauss@26056
  1420
         zdiv_pos_pos_trivial neg_mod_sign neg_mod_bound zdiv_neg_neg_trivial)
krauss@26056
  1421
done
krauss@26056
  1422
krauss@26056
  1423
lemma zmod_div_trivial [simp]: "(a zmod b) zdiv b = #0"
krauss@26056
  1424
apply (cut_tac a = "intify (a)" and b = "intify (b)" in zmod_div_trivial_raw)
krauss@26056
  1425
apply auto
krauss@26056
  1426
done
krauss@26056
  1427
krauss@26056
  1428
lemma zmod_mod_trivial_raw:
krauss@26056
  1429
     "[|a \<in> int; b \<in> int|] ==> (a zmod b) zmod b = a zmod b"
krauss@26056
  1430
apply (case_tac "b = #0")
krauss@26056
  1431
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1432
apply (auto simp add: neq_iff_zless pos_mod_sign pos_mod_bound 
krauss@26056
  1433
       zmod_pos_pos_trivial neg_mod_sign neg_mod_bound zmod_neg_neg_trivial)
krauss@26056
  1434
done
krauss@26056
  1435
krauss@26056
  1436
lemma zmod_mod_trivial [simp]: "(a zmod b) zmod b = a zmod b"
krauss@26056
  1437
apply (cut_tac a = "intify (a)" and b = "intify (b)" in zmod_mod_trivial_raw)
krauss@26056
  1438
apply auto
krauss@26056
  1439
done
krauss@26056
  1440
krauss@26056
  1441
lemma zmod_zadd_left_eq: "(a$+b) zmod c = ((a zmod c) $+ b) zmod c"
krauss@26056
  1442
apply (rule trans [symmetric])
krauss@26056
  1443
apply (rule zmod_zadd1_eq)
krauss@26056
  1444
apply (simp (no_asm))
krauss@26056
  1445
apply (rule zmod_zadd1_eq [symmetric])
krauss@26056
  1446
done
krauss@26056
  1447
krauss@26056
  1448
lemma zmod_zadd_right_eq: "(a$+b) zmod c = (a $+ (b zmod c)) zmod c"
krauss@26056
  1449
apply (rule trans [symmetric])
krauss@26056
  1450
apply (rule zmod_zadd1_eq)
krauss@26056
  1451
apply (simp (no_asm))
krauss@26056
  1452
apply (rule zmod_zadd1_eq [symmetric])
krauss@26056
  1453
done
krauss@26056
  1454
krauss@26056
  1455
krauss@26056
  1456
lemma zdiv_zadd_self1 [simp]:
krauss@26056
  1457
     "intify(a) \<noteq> #0 ==> (a$+b) zdiv a = b zdiv a $+ #1"
krauss@26056
  1458
by (simp (no_asm_simp) add: zdiv_zadd1_eq)
krauss@26056
  1459
krauss@26056
  1460
lemma zdiv_zadd_self2 [simp]:
krauss@26056
  1461
     "intify(a) \<noteq> #0 ==> (b$+a) zdiv a = b zdiv a $+ #1"
krauss@26056
  1462
by (simp (no_asm_simp) add: zdiv_zadd1_eq)
krauss@26056
  1463
krauss@26056
  1464
lemma zmod_zadd_self1 [simp]: "(a$+b) zmod a = b zmod a"
krauss@26056
  1465
apply (case_tac "a = #0")
krauss@26056
  1466
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1467
apply (simp (no_asm_simp) add: zmod_zadd1_eq)
krauss@26056
  1468
done
krauss@26056
  1469
krauss@26056
  1470
lemma zmod_zadd_self2 [simp]: "(b$+a) zmod a = b zmod a"
krauss@26056
  1471
apply (case_tac "a = #0")
krauss@26056
  1472
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1473
apply (simp (no_asm_simp) add: zmod_zadd1_eq)
krauss@26056
  1474
done
krauss@26056
  1475
krauss@26056
  1476
krauss@26056
  1477
subsection{* proving  a zdiv (b*c) = (a zdiv b) zdiv c *}
krauss@26056
  1478
krauss@26056
  1479
(*The condition c>0 seems necessary.  Consider that 7 zdiv ~6 = ~2 but
krauss@26056
  1480
  7 zdiv 2 zdiv ~3 = 3 zdiv ~3 = ~1.  The subcase (a zdiv b) zmod c = 0 seems
krauss@26056
  1481
  to cause particular problems.*)
krauss@26056
  1482
krauss@26056
  1483
(** first, four lemmas to bound the remainder for the cases b<0 and b>0 **)
krauss@26056
  1484
krauss@26056
  1485
lemma zdiv_zmult2_aux1:
krauss@26056
  1486
     "[| #0 $< c;  b $< r;  r $<= #0 |] ==> b$*c $< b$*(q zmod c) $+ r"
krauss@26056
  1487
apply (subgoal_tac "b $* (c $- q zmod c) $< r $* #1")
krauss@26056
  1488
apply (simp add: zdiff_zmult_distrib2 zadd_commute zcompare_rls)
krauss@26056
  1489
apply (rule zle_zless_trans)
krauss@26056
  1490
apply (erule_tac [2] zmult_zless_mono1)
krauss@26056
  1491
apply (rule zmult_zle_mono2_neg)
krauss@26056
  1492
apply (auto simp add: zcompare_rls zadd_commute add1_zle_iff pos_mod_bound)
krauss@26056
  1493
apply (blast intro: zless_imp_zle dest: zless_zle_trans)
krauss@26056
  1494
done
krauss@26056
  1495
krauss@26056
  1496
lemma zdiv_zmult2_aux2:
krauss@26056
  1497
     "[| #0 $< c;   b $< r;  r $<= #0 |] ==> b $* (q zmod c) $+ r $<= #0"
krauss@26056
  1498
apply (subgoal_tac "b $* (q zmod c) $<= #0")
krauss@26056
  1499
 prefer 2
krauss@26056
  1500
 apply (simp add: zmult_le_0_iff pos_mod_sign) 
krauss@26056
  1501
 apply (blast intro: zless_imp_zle dest: zless_zle_trans)
krauss@26056
  1502
(*arithmetic*)
krauss@26056
  1503
apply (drule zadd_zle_mono)
krauss@26056
  1504
apply assumption
krauss@26056
  1505
apply (simp add: zadd_commute)
krauss@26056
  1506
done
krauss@26056
  1507
krauss@26056
  1508
lemma zdiv_zmult2_aux3:
krauss@26056
  1509
     "[| #0 $< c;  #0 $<= r;  r $< b |] ==> #0 $<= b $* (q zmod c) $+ r"
krauss@26056
  1510
apply (subgoal_tac "#0 $<= b $* (q zmod c)")
krauss@26056
  1511
 prefer 2
krauss@26056
  1512
 apply (simp add: int_0_le_mult_iff pos_mod_sign) 
krauss@26056
  1513
 apply (blast intro: zless_imp_zle dest: zle_zless_trans)
krauss@26056
  1514
(*arithmetic*)
krauss@26056
  1515
apply (drule zadd_zle_mono)
krauss@26056
  1516
apply assumption
krauss@26056
  1517
apply (simp add: zadd_commute)
krauss@26056
  1518
done
krauss@26056
  1519
krauss@26056
  1520
lemma zdiv_zmult2_aux4:
krauss@26056
  1521
     "[| #0 $< c; #0 $<= r; r $< b |] ==> b $* (q zmod c) $+ r $< b $* c"
krauss@26056
  1522
apply (subgoal_tac "r $* #1 $< b $* (c $- q zmod c)")
krauss@26056
  1523
apply (simp add: zdiff_zmult_distrib2 zadd_commute zcompare_rls)
krauss@26056
  1524
apply (rule zless_zle_trans)
krauss@26056
  1525
apply (erule zmult_zless_mono1)
krauss@26056
  1526
apply (rule_tac [2] zmult_zle_mono2)
krauss@26056
  1527
apply (auto simp add: zcompare_rls zadd_commute add1_zle_iff pos_mod_bound)
krauss@26056
  1528
apply (blast intro: zless_imp_zle dest: zle_zless_trans)
krauss@26056
  1529
done
krauss@26056
  1530
krauss@26056
  1531
lemma zdiv_zmult2_lemma:
krauss@26056
  1532
     "[| quorem (<a,b>, <q,r>);  a \<in> int;  b \<in> int;  b \<noteq> #0;  #0 $< c |]  
krauss@26056
  1533
      ==> quorem (<a,b$*c>, <q zdiv c, b$*(q zmod c) $+ r>)"
krauss@26056
  1534
apply (auto simp add: zmult_ac zmod_zdiv_equality [symmetric] quorem_def
krauss@26056
  1535
               neq_iff_zless int_0_less_mult_iff 
krauss@26056
  1536
               zadd_zmult_distrib2 [symmetric] zdiv_zmult2_aux1 zdiv_zmult2_aux2
krauss@26056
  1537
               zdiv_zmult2_aux3 zdiv_zmult2_aux4)
krauss@26056
  1538
apply (blast dest: zless_trans)+
krauss@26056
  1539
done
krauss@26056
  1540
krauss@26056
  1541
lemma zdiv_zmult2_eq_raw:
krauss@26056
  1542
     "[|#0 $< c;  a \<in> int;  b \<in> int|] ==> a zdiv (b$*c) = (a zdiv b) zdiv c"
krauss@26056
  1543
apply (case_tac "b = #0")
krauss@26056
  1544
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1545
apply (rule quorem_div_mod [THEN zdiv_zmult2_lemma, THEN quorem_div])
krauss@26056
  1546
apply (auto simp add: intify_eq_0_iff_zle)
krauss@26056
  1547
apply (blast dest: zle_zless_trans)
krauss@26056
  1548
done
krauss@26056
  1549
krauss@26056
  1550
lemma zdiv_zmult2_eq: "#0 $< c ==> a zdiv (b$*c) = (a zdiv b) zdiv c"
krauss@26056
  1551
apply (cut_tac a = "intify (a)" and b = "intify (b)" in zdiv_zmult2_eq_raw)
krauss@26056
  1552
apply auto
krauss@26056
  1553
done
krauss@26056
  1554
krauss@26056
  1555
lemma zmod_zmult2_eq_raw:
krauss@26056
  1556
     "[|#0 $< c;  a \<in> int;  b \<in> int|]  
krauss@26056
  1557
      ==> a zmod (b$*c) = b$*(a zdiv b zmod c) $+ a zmod b"
krauss@26056
  1558
apply (case_tac "b = #0")
krauss@26056
  1559
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1560
apply (rule quorem_div_mod [THEN zdiv_zmult2_lemma, THEN quorem_mod])
krauss@26056
  1561
apply (auto simp add: intify_eq_0_iff_zle)
krauss@26056
  1562
apply (blast dest: zle_zless_trans)
krauss@26056
  1563
done
krauss@26056
  1564
krauss@26056
  1565
lemma zmod_zmult2_eq:
krauss@26056
  1566
     "#0 $< c ==> a zmod (b$*c) = b$*(a zdiv b zmod c) $+ a zmod b"
krauss@26056
  1567
apply (cut_tac a = "intify (a)" and b = "intify (b)" in zmod_zmult2_eq_raw)
krauss@26056
  1568
apply auto
krauss@26056
  1569
done
krauss@26056
  1570
krauss@26056
  1571
subsection{* Cancellation of common factors in "zdiv" *}
krauss@26056
  1572
krauss@26056
  1573
lemma zdiv_zmult_zmult1_aux1:
krauss@26056
  1574
     "[| #0 $< b;  intify(c) \<noteq> #0 |] ==> (c$*a) zdiv (c$*b) = a zdiv b"
krauss@26056
  1575
apply (subst zdiv_zmult2_eq)
krauss@26056
  1576
apply auto
krauss@26056
  1577
done
krauss@26056
  1578
krauss@26056
  1579
lemma zdiv_zmult_zmult1_aux2:
krauss@26056
  1580
     "[| b $< #0;  intify(c) \<noteq> #0 |] ==> (c$*a) zdiv (c$*b) = a zdiv b"
krauss@26056
  1581
apply (subgoal_tac " (c $* ($-a)) zdiv (c $* ($-b)) = ($-a) zdiv ($-b)")
krauss@26056
  1582
apply (rule_tac [2] zdiv_zmult_zmult1_aux1)
krauss@26056
  1583
apply auto
krauss@26056
  1584
done
krauss@26056
  1585
krauss@26056
  1586
lemma zdiv_zmult_zmult1_raw:
krauss@26056
  1587
     "[|intify(c) \<noteq> #0; b \<in> int|] ==> (c$*a) zdiv (c$*b) = a zdiv b"
krauss@26056
  1588
apply (case_tac "b = #0")
krauss@26056
  1589
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1590
apply (auto simp add: neq_iff_zless [of b]
krauss@26056
  1591
  zdiv_zmult_zmult1_aux1 zdiv_zmult_zmult1_aux2)
krauss@26056
  1592
done
krauss@26056
  1593
krauss@26056
  1594
lemma zdiv_zmult_zmult1: "intify(c) \<noteq> #0 ==> (c$*a) zdiv (c$*b) = a zdiv b"
krauss@26056
  1595
apply (cut_tac b = "intify (b)" in zdiv_zmult_zmult1_raw)
krauss@26056
  1596
apply auto
krauss@26056
  1597
done
krauss@26056
  1598
krauss@26056
  1599
lemma zdiv_zmult_zmult2: "intify(c) \<noteq> #0 ==> (a$*c) zdiv (b$*c) = a zdiv b"
krauss@26056
  1600
apply (drule zdiv_zmult_zmult1)
krauss@26056
  1601
apply (auto simp add: zmult_commute)
krauss@26056
  1602
done
krauss@26056
  1603
krauss@26056
  1604
krauss@26056
  1605
subsection{* Distribution of factors over "zmod" *}
krauss@26056
  1606
krauss@26056
  1607
lemma zmod_zmult_zmult1_aux1:
krauss@26056
  1608
     "[| #0 $< b;  intify(c) \<noteq> #0 |]  
krauss@26056
  1609
      ==> (c$*a) zmod (c$*b) = c $* (a zmod b)"
krauss@26056
  1610
apply (subst zmod_zmult2_eq)
krauss@26056
  1611
apply auto
krauss@26056
  1612
done
krauss@26056
  1613
krauss@26056
  1614
lemma zmod_zmult_zmult1_aux2:
krauss@26056
  1615
     "[| b $< #0;  intify(c) \<noteq> #0 |]  
krauss@26056
  1616
      ==> (c$*a) zmod (c$*b) = c $* (a zmod b)"
krauss@26056
  1617
apply (subgoal_tac " (c $* ($-a)) zmod (c $* ($-b)) = c $* (($-a) zmod ($-b))")
krauss@26056
  1618
apply (rule_tac [2] zmod_zmult_zmult1_aux1)
krauss@26056
  1619
apply auto
krauss@26056
  1620
done
krauss@26056
  1621
krauss@26056
  1622
lemma zmod_zmult_zmult1_raw:
krauss@26056
  1623
     "[|b \<in> int; c \<in> int|] ==> (c$*a) zmod (c$*b) = c $* (a zmod b)"
krauss@26056
  1624
apply (case_tac "b = #0")
krauss@26056
  1625
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1626
apply (case_tac "c = #0")
krauss@26056
  1627
 apply (simp add: DIVISION_BY_ZERO_ZDIV DIVISION_BY_ZERO_ZMOD) 
krauss@26056
  1628
apply (auto simp add: neq_iff_zless [of b]
krauss@26056
  1629
  zmod_zmult_zmult1_aux1 zmod_zmult_zmult1_aux2)
krauss@26056
  1630
done
krauss@26056
  1631
krauss@26056
  1632
lemma zmod_zmult_zmult1: "(c$*a) zmod (c$*b) = c $* (a zmod b)"
krauss@26056
  1633
apply (cut_tac b = "intify (b)" and c = "intify (c)" in zmod_zmult_zmult1_raw)
krauss@26056
  1634
apply auto
krauss@26056
  1635
done
krauss@26056
  1636
krauss@26056
  1637
lemma zmod_zmult_zmult2: "(a$*c) zmod (b$*c) = (a zmod b) $* c"
krauss@26056
  1638
apply (cut_tac c = "c" in zmod_zmult_zmult1)
krauss@26056
  1639
apply (auto simp add: zmult_commute)
krauss@26056
  1640
done
krauss@26056
  1641
krauss@26056
  1642
krauss@26056
  1643
(** Quotients of signs **)
krauss@26056
  1644
krauss@26056
  1645
lemma zdiv_neg_pos_less0: "[| a $< #0;  #0 $< b |] ==> a zdiv b $< #0"
krauss@26056
  1646
apply (subgoal_tac "a zdiv b $<= #-1")
krauss@26056
  1647
apply (erule zle_zless_trans)
krauss@26056
  1648
apply (simp (no_asm))
krauss@26056
  1649
apply (rule zle_trans)
krauss@26056
  1650
apply (rule_tac a' = "#-1" in zdiv_mono1)
krauss@26056
  1651
apply (rule zless_add1_iff_zle [THEN iffD1])
krauss@26056
  1652
apply (simp (no_asm))
krauss@26056
  1653
apply (auto simp add: zdiv_minus1)
krauss@26056
  1654
done
krauss@26056
  1655
krauss@26056
  1656
lemma zdiv_nonneg_neg_le0: "[| #0 $<= a;  b $< #0 |] ==> a zdiv b $<= #0"
krauss@26056
  1657
apply (drule zdiv_mono1_neg)
krauss@26056
  1658
apply auto
krauss@26056
  1659
done
krauss@26056
  1660
krauss@26056
  1661
lemma pos_imp_zdiv_nonneg_iff: "#0 $< b ==> (#0 $<= a zdiv b) <-> (#0 $<= a)"
krauss@26056
  1662
apply auto
krauss@26056
  1663
apply (drule_tac [2] zdiv_mono1)
krauss@26056
  1664
apply (auto simp add: neq_iff_zless)
krauss@26056
  1665
apply (simp (no_asm_use) add: not_zless_iff_zle [THEN iff_sym])
krauss@26056
  1666
apply (blast intro: zdiv_neg_pos_less0)
krauss@26056
  1667
done
krauss@26056
  1668
krauss@26056
  1669
lemma neg_imp_zdiv_nonneg_iff: "b $< #0 ==> (#0 $<= a zdiv b) <-> (a $<= #0)"
krauss@26056
  1670
apply (subst zdiv_zminus_zminus [symmetric])
krauss@26056
  1671
apply (rule iff_trans)
krauss@26056
  1672
apply (rule pos_imp_zdiv_nonneg_iff)
krauss@26056
  1673
apply auto
krauss@26056
  1674
done
krauss@26056
  1675
krauss@26056
  1676
(*But not (a zdiv b $<= 0 iff a$<=0); consider a=1, b=2 when a zdiv b = 0.*)
krauss@26056
  1677
lemma pos_imp_zdiv_neg_iff: "#0 $< b ==> (a zdiv b $< #0) <-> (a $< #0)"
krauss@26056
  1678
apply (simp (no_asm_simp) add: not_zle_iff_zless [THEN iff_sym])
krauss@26056
  1679
apply (erule pos_imp_zdiv_nonneg_iff)
krauss@26056
  1680
done
krauss@26056
  1681
krauss@26056
  1682
(*Again the law fails for $<=: consider a = -1, b = -2 when a zdiv b = 0*)
krauss@26056
  1683
lemma neg_imp_zdiv_neg_iff: "b $< #0 ==> (a zdiv b $< #0) <-> (#0 $< a)"
krauss@26056
  1684
apply (simp (no_asm_simp) add: not_zle_iff_zless [THEN iff_sym])
krauss@26056
  1685
apply (erule neg_imp_zdiv_nonneg_iff)
krauss@26056
  1686
done
krauss@26056
  1687
krauss@26056
  1688
(*
krauss@26056
  1689
 THESE REMAIN TO BE CONVERTED -- but aren't that useful!
krauss@26056
  1690
krauss@26056
  1691
 subsection{* Speeding up the division algorithm with shifting *}
krauss@26056
  1692
krauss@26056
  1693
 (** computing "zdiv" by shifting **)
krauss@26056
  1694
krauss@26056
  1695
 lemma pos_zdiv_mult_2: "#0 $<= a ==> (#1 $+ #2$*b) zdiv (#2$*a) = b zdiv a"
krauss@26056
  1696
 apply (case_tac "a = #0")
krauss@26056
  1697
 apply (subgoal_tac "#1 $<= a")
krauss@26056
  1698
  apply (arith_tac 2)
krauss@26056
  1699
 apply (subgoal_tac "#1 $< a $* #2")
krauss@26056
  1700
  apply (arith_tac 2)
krauss@26056
  1701
 apply (subgoal_tac "#2$* (#1 $+ b zmod a) $<= #2$*a")
krauss@26056
  1702
  apply (rule_tac [2] zmult_zle_mono2)
krauss@26056
  1703
 apply (auto simp add: zadd_commute zmult_commute add1_zle_iff pos_mod_bound)
krauss@26056
  1704
 apply (subst zdiv_zadd1_eq)
krauss@26056
  1705
 apply (simp (no_asm_simp) add: zdiv_zmult_zmult2 zmod_zmult_zmult2 zdiv_pos_pos_trivial)
krauss@26056
  1706
 apply (subst zdiv_pos_pos_trivial)
krauss@26056
  1707
 apply (simp (no_asm_simp) add: [zmod_pos_pos_trivial pos_mod_sign [THEN zadd_zle_mono1] RSN (2,zle_trans) ])
krauss@26056
  1708
 apply (auto simp add: zmod_pos_pos_trivial)
krauss@26056
  1709
 apply (subgoal_tac "#0 $<= b zmod a")
krauss@26056
  1710
  apply (asm_simp_tac (simpset () add: pos_mod_sign) 2)
krauss@26056
  1711
 apply arith
krauss@26056
  1712
 done
krauss@26056
  1713
krauss@26056
  1714
krauss@26056
  1715
 lemma neg_zdiv_mult_2: "a $<= #0 ==> (#1 $+ #2$*b) zdiv (#2$*a) <-> (b$+#1) zdiv a"
krauss@26056
  1716
 apply (subgoal_tac " (#1 $+ #2$* ($-b-#1)) zdiv (#2 $* ($-a)) <-> ($-b-#1) zdiv ($-a)")
krauss@26056
  1717
 apply (rule_tac [2] pos_zdiv_mult_2)
krauss@26056
  1718
 apply (auto simp add: zmult_zminus_right)
krauss@26056
  1719
 apply (subgoal_tac " (#-1 - (#2 $* b)) = - (#1 $+ (#2 $* b))")
krauss@26056
  1720
 apply (Simp_tac 2)
krauss@26056
  1721
 apply (asm_full_simp_tac (HOL_ss add: zdiv_zminus_zminus zdiff_def zminus_zadd_distrib [symmetric])
krauss@26056
  1722
 done
krauss@26056
  1723
krauss@26056
  1724
krauss@26056
  1725
 (*Not clear why this must be proved separately; probably integ_of causes
krauss@26056
  1726
   simplification problems*)
krauss@26056
  1727
 lemma lemma: "~ #0 $<= x ==> x $<= #0"
krauss@26056
  1728
 apply auto
krauss@26056
  1729
 done
krauss@26056
  1730
krauss@26056
  1731
 lemma zdiv_integ_of_BIT: "integ_of (v BIT b) zdiv integ_of (w BIT False) =  
krauss@26056
  1732
           (if ~b | #0 $<= integ_of w                    
krauss@26056
  1733
            then integ_of v zdiv (integ_of w)     
krauss@26056
  1734
            else (integ_of v $+ #1) zdiv (integ_of w))"
krauss@26056
  1735
 apply (simp_tac (simpset_of Int.thy add: zadd_assoc integ_of_BIT)
krauss@26056
  1736
 apply (simp (no_asm_simp) del: bin_arith_extra_simps@bin_rel_simps add: zdiv_zmult_zmult1 pos_zdiv_mult_2 lemma neg_zdiv_mult_2)
krauss@26056
  1737
 done
krauss@26056
  1738
krauss@26056
  1739
 declare zdiv_integ_of_BIT [simp]
krauss@26056
  1740
krauss@26056
  1741
krauss@26056
  1742
 (** computing "zmod" by shifting (proofs resemble those for "zdiv") **)
krauss@26056
  1743
krauss@26056
  1744
 lemma pos_zmod_mult_2: "#0 $<= a ==> (#1 $+ #2$*b) zmod (#2$*a) = #1 $+ #2 $* (b zmod a)"
krauss@26056
  1745
 apply (case_tac "a = #0")
krauss@26056
  1746
 apply (subgoal_tac "#1 $<= a")
krauss@26056
  1747
  apply (arith_tac 2)
krauss@26056
  1748
 apply (subgoal_tac "#1 $< a $* #2")
krauss@26056
  1749
  apply (arith_tac 2)
krauss@26056
  1750
 apply (subgoal_tac "#2$* (#1 $+ b zmod a) $<= #2$*a")
krauss@26056
  1751
  apply (rule_tac [2] zmult_zle_mono2)
krauss@26056
  1752
 apply (auto simp add: zadd_commute zmult_commute add1_zle_iff pos_mod_bound)
krauss@26056
  1753
 apply (subst zmod_zadd1_eq)
krauss@26056
  1754
 apply (simp (no_asm_simp) add: zmod_zmult_zmult2 zmod_pos_pos_trivial)
krauss@26056
  1755
 apply (rule zmod_pos_pos_trivial)
krauss@26056
  1756
 apply (simp (no_asm_simp) # add: [zmod_pos_pos_trivial pos_mod_sign [THEN zadd_zle_mono1] RSN (2,zle_trans) ])
krauss@26056
  1757
 apply (auto simp add: zmod_pos_pos_trivial)
krauss@26056
  1758
 apply (subgoal_tac "#0 $<= b zmod a")
krauss@26056
  1759
  apply (asm_simp_tac (simpset () add: pos_mod_sign) 2)
krauss@26056
  1760
 apply arith
krauss@26056
  1761
 done
krauss@26056
  1762
krauss@26056
  1763
krauss@26056
  1764
 lemma neg_zmod_mult_2: "a $<= #0 ==> (#1 $+ #2$*b) zmod (#2$*a) = #2 $* ((b$+#1) zmod a) - #1"
krauss@26056
  1765
 apply (subgoal_tac " (#1 $+ #2$* ($-b-#1)) zmod (#2$* ($-a)) = #1 $+ #2$* (($-b-#1) zmod ($-a))")
krauss@26056
  1766
 apply (rule_tac [2] pos_zmod_mult_2)
krauss@26056
  1767
 apply (auto simp add: zmult_zminus_right)
krauss@26056
  1768
 apply (subgoal_tac " (#-1 - (#2 $* b)) = - (#1 $+ (#2 $* b))")
krauss@26056
  1769
 apply (Simp_tac 2)
krauss@26056
  1770
 apply (asm_full_simp_tac (HOL_ss add: zmod_zminus_zminus zdiff_def zminus_zadd_distrib [symmetric])
krauss@26056
  1771
 apply (dtac (zminus_equation [THEN iffD1, symmetric])
krauss@26056
  1772
 apply auto
krauss@26056
  1773
 done
krauss@26056
  1774
krauss@26056
  1775
 lemma zmod_integ_of_BIT: "integ_of (v BIT b) zmod integ_of (w BIT False) =  
krauss@26056
  1776
           (if b then  
krauss@26056
  1777
                 if #0 $<= integ_of w  
krauss@26056
  1778
                 then #2 $* (integ_of v zmod integ_of w) $+ #1     
krauss@26056
  1779
                 else #2 $* ((integ_of v $+ #1) zmod integ_of w) - #1   
krauss@26056
  1780
            else #2 $* (integ_of v zmod integ_of w))"
krauss@26056
  1781
 apply (simp_tac (simpset_of Int.thy add: zadd_assoc integ_of_BIT)
krauss@26056
  1782
 apply (simp (no_asm_simp) del: bin_arith_extra_simps@bin_rel_simps add: zmod_zmult_zmult1 pos_zmod_mult_2 lemma neg_zmod_mult_2)
krauss@26056
  1783
 done
krauss@26056
  1784
krauss@26056
  1785
 declare zmod_integ_of_BIT [simp]
krauss@26056
  1786
*)
krauss@26056
  1787
krauss@26056
  1788
end
krauss@26056
  1789