src/HOL/Integ/IntDiv.ML
author paulson
Mon, 19 Jul 1999 15:30:59 +0200
changeset 7035 d1b7a2372b31
parent 6999 73f681047e5f
child 7074 e0730ffaafcc
permissions -rw-r--r--
many new laws about div and mod
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     1
(*  Title:      HOL/IntDiv.ML
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     2
    ID:         $Id$
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     4
    Copyright   1999  University of Cambridge
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     5
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     6
The division operators div, mod and the divides relation "dvd"
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
     7
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
     8
Here is the division algorithm in ML:
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
     9
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    10
    fun posDivAlg (a,b) =
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    11
      if a<b then (0,a)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    12
      else let val (q,r) = posDivAlg(a, 2*b)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    13
	       in  if 0<=r-b then (2*q+1, r-b) else (2*q, r)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    14
	   end;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    15
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    16
    fun negDivAlg (a,b) =
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    17
      if 0<=a+b then (~1,a+b)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    18
      else let val (q,r) = negDivAlg(a, 2*b)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    19
	       in  if 0<=r-b then (2*q+1, r-b) else (2*q, r)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    20
	   end;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    21
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    22
    fun negateSnd (q,r:int) = (q,~r);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    23
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    24
    fun divAlg (a,b) = if 0<=a then 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    25
			  if b>0 then posDivAlg (a,b) 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    26
			   else if a=0 then (0,0)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    27
				else negateSnd (negDivAlg (~a,~b))
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    28
		       else 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    29
			  if 0<b then negDivAlg (a,b)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
    30
			  else        negateSnd (posDivAlg (~a,~b));
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    31
*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    32
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    33
Addsimps [zless_nat_conj];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    34
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    35
(*** Uniqueness and monotonicity of quotients and remainders ***)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    36
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    37
Goal "[| r' + b*q' <= r + b*q;  #0 <= r';  #0 < b;  r < b |] \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    38
\     ==> q' <= (q::int)";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    39
by (subgoal_tac "r' + b * (q'-q) <= r" 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    40
by (simp_tac (simpset() addsimps zcompare_rls@[zdiff_zmult_distrib2]) 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    41
by (subgoal_tac "#0 < b * (#1 + q - q')" 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    42
by (etac order_le_less_trans 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    43
by (full_simp_tac (simpset() addsimps zcompare_rls@[zdiff_zmult_distrib2,
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    44
						    zadd_zmult_distrib2]) 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    45
by (subgoal_tac "b * q' < b * (#1 + q)" 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    46
by (full_simp_tac (simpset() addsimps zcompare_rls@[zdiff_zmult_distrib2,
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    47
						    zadd_zmult_distrib2]) 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    48
by (Asm_full_simp_tac 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    49
qed "unique_quotient_lemma";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    50
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    51
Goal "[| r' + b*q' <= r + b*q;  r <= #0;  b < #0;  b < r' |] \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    52
\     ==> q <= (q'::int)";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    53
by (res_inst_tac [("b", "-b"), ("r", "-r'"), ("r'", "-r")] 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    54
    unique_quotient_lemma 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    55
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    56
	      simpset() addsimps zcompare_rls@[zmult_zminus_right])); 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    57
qed "unique_quotient_lemma_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    58
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    59
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    60
Goal "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  b ~= #0 |] \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    61
\     ==> q = q'";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    62
by (asm_full_simp_tac 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    63
    (simpset() addsimps split_ifs@[quorem_def, linorder_neq_iff]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    64
by Safe_tac; 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    65
by (ALLGOALS Asm_full_simp_tac);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    66
by (REPEAT 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    67
    (blast_tac (claset() addIs [order_antisym]
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    68
			 addDs [order_eq_refl RS unique_quotient_lemma, 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    69
				order_eq_refl RS unique_quotient_lemma_neg,
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    70
				sym]) 1));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    71
qed "unique_quotient";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    72
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    73
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    74
Goal "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  b ~= #0 |] \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    75
\     ==> r = r'";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    76
by (subgoal_tac "q = q'" 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    77
by (blast_tac (claset() addIs [unique_quotient]) 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    78
by (asm_full_simp_tac (simpset() addsimps [quorem_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    79
qed "unique_remainder";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    80
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    81
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    82
(*** Correctness of posDivAlg, the division algorithm for a>=0 and b>0 ***)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    83
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    84
(*Unfold all "let"s involving constants*)
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    85
Addsimps [read_instantiate_sg (sign_of IntDiv.thy)
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    86
	          [("s", "number_of ?v")] Let_def];
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    87
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    88
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    89
Goal "adjust a b (q,r) = (let diff = r-b in \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    90
\                         if #0 <= diff then (#2*q + #1, diff)  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    91
\                                       else (#2*q, r))";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
    92
by (simp_tac (simpset() addsimps [Let_def,adjust_def]) 1);
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    93
qed "adjust_eq";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    94
Addsimps [adjust_eq];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    95
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    96
(*Proving posDivAlg's termination condition*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    97
val [tc] = posDivAlg.tcs;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    98
goalw_cterm [] (cterm_of (sign_of thy) (HOLogic.mk_Trueprop tc));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    99
by (auto_tac (claset(), simpset() addsimps [zmult_2_right]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   100
val lemma = result();
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   101
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   102
(* removing the termination condition from the generated theorems *)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   103
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   104
bind_thm ("posDivAlg_raw_eqn", lemma RS hd posDivAlg.rules);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   105
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   106
(**use with simproc to avoid re-proving the premise*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   107
Goal "#0 < b ==> \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   108
\     posDivAlg (a,b) =      \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   109
\      (if a<b then (#0,a) else adjust a b (posDivAlg(a, #2*b)))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   110
by (rtac (posDivAlg_raw_eqn RS trans) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   111
by (Asm_simp_tac 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   112
qed "posDivAlg_eqn";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   113
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   114
val posDivAlg_induct = lemma RS posDivAlg.induct;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   115
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   116
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   117
(*Correctness of posDivAlg: it computes quotients correctly*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   118
Goal "#0 <= a --> #0 < b --> quorem ((a, b), posDivAlg (a, b))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   119
by (res_inst_tac [("u", "a"), ("v", "b")] posDivAlg_induct 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   120
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   121
by (ALLGOALS 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   122
    (asm_full_simp_tac (simpset() addsimps [quorem_def,
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   123
					    pos_imp_zmult_pos_iff])));
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   124
(*base case: a<b*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   125
by (asm_full_simp_tac (simpset() addsimps [posDivAlg_eqn]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   126
(*main argument*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   127
by (stac posDivAlg_eqn 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   128
by (ALLGOALS Asm_simp_tac);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   129
by (etac splitE 1);
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   130
by (auto_tac (claset(), simpset() addsimps [Let_def]));
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   131
(*the "add one" case*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   132
by (asm_full_simp_tac (simpset() addsimps [zadd_zmult_distrib2]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   133
(*the "just double" case*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   134
by (asm_full_simp_tac (simpset() addsimps zcompare_rls@[zmult_2_right]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   135
qed_spec_mp "posDivAlg_correct";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   136
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   137
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   138
(*** Correctness of negDivAlg, the division algorithm for a<0 and b>0 ***)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   139
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   140
(*Proving negDivAlg's termination condition*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   141
val [tc] = negDivAlg.tcs;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   142
goalw_cterm [] (cterm_of (sign_of thy) (HOLogic.mk_Trueprop tc));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   143
by (auto_tac (claset(), simpset() addsimps [zmult_2_right]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   144
val lemma = result();
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   145
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   146
(* removing the termination condition from the generated theorems *)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   147
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   148
bind_thm ("negDivAlg_raw_eqn", lemma RS hd negDivAlg.rules);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   149
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   150
(**use with simproc to avoid re-proving the premise*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   151
Goal "#0 < b ==> \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   152
\     negDivAlg (a,b) =      \
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   153
\      (if #0<=a+b then (#-1,a+b) else adjust a b (negDivAlg(a, #2*b)))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   154
by (rtac (negDivAlg_raw_eqn RS trans) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   155
by (Asm_simp_tac 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   156
qed "negDivAlg_eqn";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   157
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   158
val negDivAlg_induct = lemma RS negDivAlg.induct;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   159
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   160
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   161
(*Correctness of negDivAlg: it computes quotients correctly
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   162
  It doesn't work if a=0 because the 0/b=0 rather than -1*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   163
Goal "a < #0 --> #0 < b --> quorem ((a, b), negDivAlg (a, b))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   164
by (res_inst_tac [("u", "a"), ("v", "b")] negDivAlg_induct 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   165
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   166
by (ALLGOALS 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   167
    (asm_full_simp_tac (simpset() addsimps [quorem_def,
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   168
					    pos_imp_zmult_pos_iff])));
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   169
(*base case: 0<=a+b*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   170
by (asm_full_simp_tac (simpset() addsimps [negDivAlg_eqn]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   171
(*main argument*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   172
by (stac negDivAlg_eqn 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   173
by (ALLGOALS Asm_simp_tac);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   174
by (etac splitE 1);
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   175
by (auto_tac (claset(), simpset() addsimps [Let_def]));
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   176
(*the "add one" case*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   177
by (asm_full_simp_tac (simpset() addsimps [zadd_zmult_distrib2]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   178
(*the "just double" case*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   179
by (asm_full_simp_tac (simpset() addsimps zcompare_rls@[zmult_2_right]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   180
qed_spec_mp "negDivAlg_correct";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   181
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   182
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   183
(*** Existence shown by proving the division algorithm to be correct ***)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   184
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   185
(*the case a=0*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   186
Goal "b ~= #0 ==> quorem ((#0,b), (#0,#0))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   187
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   188
	      simpset() addsimps [quorem_def, linorder_neq_iff]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   189
qed "quorem_0";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   190
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   191
Goal "posDivAlg (#0, b) = (#0, #0)";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   192
by (stac posDivAlg_raw_eqn 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   193
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   194
qed "posDivAlg_0";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   195
Addsimps [posDivAlg_0];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   196
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   197
Goal "negDivAlg (#-1, b) = (#-1, b-#1)";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   198
by (stac negDivAlg_raw_eqn 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   199
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   200
qed "negDivAlg_minus1";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   201
Addsimps [negDivAlg_minus1];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   202
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   203
Goalw [negateSnd_def] "negateSnd(q,r) = (q,-r)";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   204
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   205
qed "negateSnd_eq";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   206
Addsimps [negateSnd_eq];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   207
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   208
Goal "quorem ((-a,-b), qr) ==> quorem ((a,b), negateSnd qr)";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   209
by (auto_tac (claset(), simpset() addsimps split_ifs@[quorem_def]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   210
qed "quorem_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   211
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   212
Goal "b ~= #0 ==> quorem ((a,b), divAlg(a,b))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   213
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   214
	      simpset() addsimps [quorem_0, divAlg_def]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   215
by (REPEAT_FIRST (resolve_tac [quorem_neg, posDivAlg_correct,
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   216
			       negDivAlg_correct]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   217
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   218
	      simpset() addsimps [quorem_def, linorder_neq_iff]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   219
qed "divAlg_correct";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   220
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   221
(** Aribtrary definitions for division by zero.  Useful to simplify 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   222
    certain equations **)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   223
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   224
Goal "a div (#0::int) = #0";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   225
by (simp_tac (simpset() addsimps [div_def, divAlg_def, posDivAlg_raw_eqn]) 1);
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   226
qed "DIVISION_BY_ZERO_ZDIV";  (*NOT for adding to default simpset*)
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   227
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   228
Goal "a mod (#0::int) = a";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   229
by (simp_tac (simpset() addsimps [mod_def, divAlg_def, posDivAlg_raw_eqn]) 1);
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   230
qed "DIVISION_BY_ZERO_ZMOD";  (*NOT for adding to default simpset*)
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   231
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   232
fun zdiv_undefined_case_tac s i =
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   233
  case_tac s i THEN 
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   234
  asm_simp_tac (simpset() addsimps [DIVISION_BY_ZERO_ZDIV, 
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   235
				    DIVISION_BY_ZERO_ZMOD]) i;
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   236
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   237
(** Basic laws about division and remainder **)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   238
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   239
Goal "(a::int) = b * (a div b) + (a mod b)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   240
by (zdiv_undefined_case_tac "b = #0" 1);
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   241
by (cut_inst_tac [("a","a"),("b","b")] divAlg_correct 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   242
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   243
	      simpset() addsimps [quorem_def, div_def, mod_def]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   244
qed "zmod_zdiv_equality";  
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   245
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   246
Goal "(#0::int) < b ==> #0 <= a mod b & a mod b < b";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   247
by (cut_inst_tac [("a","a"),("b","b")] divAlg_correct 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   248
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   249
	      simpset() addsimps [quorem_def, mod_def]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   250
bind_thm ("pos_mod_sign", result() RS conjunct1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   251
bind_thm ("pos_mod_bound", result() RS conjunct2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   252
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   253
Goal "b < (#0::int) ==> a mod b <= #0 & b < a mod b";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   254
by (cut_inst_tac [("a","a"),("b","b")] divAlg_correct 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   255
by (auto_tac (claset(), 
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   256
	      simpset() addsimps [quorem_def, div_def, mod_def]));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   257
bind_thm ("neg_mod_sign", result() RS conjunct1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   258
bind_thm ("neg_mod_bound", result() RS conjunct2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   259
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   260
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   261
(** proving general properties of div and mod **)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   262
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   263
Goal "b ~= #0 ==> quorem ((a, b), (a div b, a mod b))";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   264
by (cut_inst_tac [("a","a"),("b","b")] zmod_zdiv_equality 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   265
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   266
    (claset(),
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   267
     simpset() addsimps [quorem_def, linorder_neq_iff, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   268
			 pos_mod_sign,pos_mod_bound,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   269
			 neg_mod_sign, neg_mod_bound]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   270
qed "quorem_div_mod";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   271
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   272
Goal "[| quorem((a,b),(q,r));  b ~= #0 |] ==> a div b = q";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   273
by (asm_simp_tac (simpset() addsimps [quorem_div_mod RS unique_quotient]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   274
qed "quorem_div";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   275
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   276
Goal "[| quorem((a,b),(q,r));  b ~= #0 |] ==> a mod b = r";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   277
by (asm_simp_tac (simpset() addsimps [quorem_div_mod RS unique_remainder]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   278
qed "quorem_mod";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   279
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   280
Goal "[| (#0::int) <= a;  a < b |] ==> a div b = #0";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   281
by (rtac quorem_div 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   282
by (auto_tac (claset(), simpset() addsimps [quorem_def]));
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   283
qed "div_pos_pos_trivial";
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   284
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   285
Goal "[| a <= (#0::int);  b < a |] ==> a div b = #0";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   286
by (rtac quorem_div 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   287
by (auto_tac (claset(), simpset() addsimps [quorem_def]));
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   288
qed "div_neg_neg_trivial";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   289
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   290
Goal "[| (#0::int) < a;  a+b <= #0 |] ==> a div b = #-1";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   291
by (rtac quorem_div 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   292
by (auto_tac (claset(), simpset() addsimps [quorem_def]));
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   293
qed "div_pos_neg_trivial";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   294
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   295
(*There is no div_neg_pos_trivial because  #0 div b = #0 would supersede it*)
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   296
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   297
Goal "[| (#0::int) <= a;  a < b |] ==> a mod b = a";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   298
by (rtac quorem_mod 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   299
by (auto_tac (claset(), simpset() addsimps [quorem_def]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   300
by (rtac zmult_0_right 1);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   301
qed "mod_pos_pos_trivial";
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   302
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   303
Goal "[| a <= (#0::int);  b < a |] ==> a mod b = a";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   304
by (rtac quorem_mod 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   305
by (auto_tac (claset(), simpset() addsimps [quorem_def]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   306
by (rtac zmult_0_right 1);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   307
qed "mod_neg_neg_trivial";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   308
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   309
Goal "[| (#0::int) < a;  a+b <= #0 |] ==> a mod b = a+b";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   310
by (res_inst_tac [("q","#-1")] quorem_mod 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   311
by (auto_tac (claset(), simpset() addsimps [quorem_def]));
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   312
qed "mod_pos_neg_trivial";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   313
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   314
(*There is no mod_neg_pos_trivial...*)
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   315
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   316
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   317
(*Simpler laws such as -a div b = -(a div b) FAIL*)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   318
Goal "(-a) div (-b) = a div (b::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   319
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   320
by (stac ((simplify(simpset()) (quorem_div_mod RS quorem_neg)) 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   321
	  RS quorem_div) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   322
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   323
qed "zdiv_zminus_zminus";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   324
Addsimps [zdiv_zminus_zminus];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   325
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   326
(*Simpler laws such as -a mod b = -(a mod b) FAIL*)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   327
Goal "(-a) mod (-b) = - (a mod (b::int))";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   328
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   329
by (stac ((simplify(simpset()) (quorem_div_mod RS quorem_neg)) 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   330
	  RS quorem_mod) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   331
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   332
qed "zmod_zminus_zminus";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   333
Addsimps [zmod_zminus_zminus];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   334
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   335
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   336
(*** division of a number by itself ***)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   337
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   338
Goal "[| (#0::int) < a; a = r + a*q; r < a |] ==> #1 <= q";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   339
by (subgoal_tac "#0 < a*q" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   340
by (arith_tac 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   341
by (asm_full_simp_tac (simpset() addsimps [pos_imp_zmult_pos_iff]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   342
val lemma1 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   343
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   344
Goal "[| (#0::int) < a; a = r + a*q; #0 <= r |] ==> q <= #1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   345
by (subgoal_tac "#0 <= a*(#1-q)" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   346
by (asm_simp_tac (simpset() addsimps [zdiff_zmult_distrib2]) 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   347
by (asm_full_simp_tac (simpset() addsimps [pos_imp_zmult_nonneg_iff]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   348
by (full_simp_tac (simpset() addsimps zcompare_rls) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   349
val lemma2 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   350
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   351
Goal "[| quorem((a,a),(q,r));  a ~= (#0::int) |] ==> q = #1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   352
by (asm_full_simp_tac 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   353
    (simpset() addsimps split_ifs@[quorem_def, linorder_neq_iff]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   354
by (rtac order_antisym 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   355
by Safe_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   356
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   357
by (res_inst_tac [("a", "-a"),("r", "-r")] lemma1 3);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   358
by (res_inst_tac [("a", "-a"),("r", "-r")] lemma2 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   359
by (auto_tac (claset() addIs [lemma1,lemma2], simpset()));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   360
qed "self_quotient";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   361
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   362
Goal "[| quorem((a,a),(q,r));  a ~= (#0::int) |] ==> r = #0";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   363
by (forward_tac [self_quotient] 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   364
by (assume_tac 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   365
by (asm_full_simp_tac (simpset() addsimps [quorem_def]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   366
qed "self_remainder";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   367
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   368
Goal "a ~= #0 ==> a div a = (#1::int)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   369
by (asm_simp_tac (simpset() addsimps [quorem_div_mod RS self_quotient]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   370
qed "zdiv_self";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   371
Addsimps [zdiv_self];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   372
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   373
(*Here we have 0 mod 0 = 0, also assumed by Knuth (who puts m mod 0 = 0) *)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   374
Goal "a mod a = (#0::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   375
by (zdiv_undefined_case_tac "a = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   376
by (asm_simp_tac (simpset() addsimps [quorem_div_mod RS self_remainder]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   377
qed "zmod_self";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   378
Addsimps [zmod_self];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   379
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   380
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   381
(*** Computation of division and remainder ***)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   382
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   383
Goal "(#0::int) div b = #0";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   384
by (simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   385
qed "div_zero";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   386
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   387
Goal "(#0::int) < b ==> #-1 div b = #-1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   388
by (asm_simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   389
qed "div_eq_minus1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   390
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   391
Goal "(#0::int) mod b = #0";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   392
by (simp_tac (simpset() addsimps [mod_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   393
qed "mod_zero";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   394
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   395
Addsimps [div_zero, mod_zero];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   396
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   397
Goal "(#0::int) < b ==> #-1 div b = #-1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   398
by (asm_simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   399
qed "div_minus1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   400
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   401
Goal "(#0::int) < b ==> #-1 mod b = b-#1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   402
by (asm_simp_tac (simpset() addsimps [mod_def, divAlg_def]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   403
qed "mod_minus1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   404
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   405
(** a positive, b positive **)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   406
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   407
Goal "[| #0 < a;  #0 <= b |] ==> a div b = fst (posDivAlg(a,b))";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   408
by (asm_simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   409
qed "div_pos_pos";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   410
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   411
Goal "[| #0 < a;  #0 <= b |] ==> a mod b = snd (posDivAlg(a,b))";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   412
by (asm_simp_tac (simpset() addsimps [mod_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   413
qed "mod_pos_pos";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   414
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   415
(** a negative, b positive **)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   416
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   417
Goal "[| a < #0;  #0 < b |] ==> a div b = fst (negDivAlg(a,b))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   418
by (asm_simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   419
qed "div_neg_pos";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   420
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   421
Goal "[| a < #0;  #0 < b |] ==> a mod b = snd (negDivAlg(a,b))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   422
by (asm_simp_tac (simpset() addsimps [mod_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   423
qed "mod_neg_pos";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   424
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   425
(** a positive, b negative **)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   426
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   427
Goal "[| #0 < a;  b < #0 |] ==> a div b = fst (negateSnd(negDivAlg(-a,-b)))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   428
by (asm_simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   429
qed "div_pos_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   430
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   431
Goal "[| #0 < a;  b < #0 |] ==> a mod b = snd (negateSnd(negDivAlg(-a,-b)))";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   432
by (asm_simp_tac (simpset() addsimps [mod_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   433
qed "mod_pos_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   434
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   435
(** a negative, b negative **)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   436
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   437
Goal "[| a < #0;  b <= #0 |] ==> a div b = fst (negateSnd(posDivAlg(-a,-b)))";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   438
by (asm_simp_tac (simpset() addsimps [div_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   439
qed "div_neg_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   440
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   441
Goal "[| a < #0;  b <= #0 |] ==> a mod b = snd (negateSnd(posDivAlg(-a,-b)))";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   442
by (asm_simp_tac (simpset() addsimps [mod_def, divAlg_def]) 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   443
qed "mod_neg_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   444
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   445
Addsimps (map (read_instantiate_sg (sign_of IntDiv.thy)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   446
	       [("a", "number_of ?v"), ("b", "number_of ?w")])
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   447
	  [div_pos_pos, div_neg_pos, div_pos_neg, div_neg_neg,
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   448
	   mod_pos_pos, mod_neg_pos, mod_pos_neg, mod_neg_neg,
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   449
	   posDivAlg_eqn, negDivAlg_eqn]);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   450
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   451
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   452
(** Special-case simplification **)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   453
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   454
Goal "a mod (#1::int) = #0";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   455
by (cut_inst_tac [("a","a"),("b","#1")] pos_mod_sign 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   456
by (cut_inst_tac [("a","a"),("b","#1")] pos_mod_bound 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   457
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   458
qed "zmod_1";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   459
Addsimps [zmod_1];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   460
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   461
Goal "a div (#1::int) = a";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   462
by (cut_inst_tac [("a","a"),("b","#1")] zmod_zdiv_equality 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   463
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   464
qed "zdiv_1";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   465
Addsimps [zdiv_1];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   466
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   467
Goal "a mod (#-1::int) = #0";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   468
by (cut_inst_tac [("a","a"),("b","#-1")] neg_mod_sign 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   469
by (cut_inst_tac [("a","a"),("b","#-1")] neg_mod_bound 2);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   470
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   471
qed "zmod_minus1";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   472
Addsimps [zmod_minus1];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   473
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   474
Goal "a div (#-1::int) = -a";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   475
by (cut_inst_tac [("a","a"),("b","#-1")] zmod_zdiv_equality 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   476
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   477
qed "zdiv_minus1";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   478
Addsimps [zdiv_minus1];
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   479
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   480
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   481
(*** Monotonicity in the first argument (divisor) ***)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   482
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   483
Goal "[| a <= a';  #0 < (b::int) |] ==> a div b <= a' div b";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   484
by (cut_inst_tac [("a","a"),("b","b")] zmod_zdiv_equality 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   485
by (cut_inst_tac [("a","a'"),("b","b")] zmod_zdiv_equality 1);
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   486
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   487
by (rtac unique_quotient_lemma 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   488
by (etac subst 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   489
by (etac subst 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   490
by (ALLGOALS (asm_simp_tac (simpset() addsimps [pos_mod_sign,pos_mod_bound])));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   491
qed "zdiv_mono1";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   492
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   493
Goal "[| a <= a';  (b::int) < #0 |] ==> a' div b <= a div b";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   494
by (cut_inst_tac [("a","a"),("b","b")] zmod_zdiv_equality 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   495
by (cut_inst_tac [("a","a'"),("b","b")] zmod_zdiv_equality 1);
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   496
by Auto_tac;
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   497
by (rtac unique_quotient_lemma_neg 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   498
by (etac subst 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   499
by (etac subst 1);
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   500
by (ALLGOALS (asm_simp_tac (simpset() addsimps [neg_mod_sign,neg_mod_bound])));
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   501
qed "zdiv_mono1_neg";
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   502
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   503
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   504
(*** Monotonicity in the second argument (dividend) ***)
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   505
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   506
Goal "[| r + b*q = r' + b'*q';  #0 <= r' + b'*q';  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   507
\        r' < b';  #0 <= r;  #0 < b';  b' <= b |]  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   508
\     ==> q <= (q'::int)";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   509
by (subgoal_tac "#0 <= q'" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   510
 by (subgoal_tac "#0 < b'*(q' + #1)" 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   511
  by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib2]) 3);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   512
 by (asm_full_simp_tac (simpset() addsimps [pos_imp_zmult_pos_iff]) 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   513
by (subgoal_tac "b*q < b*(q' + #1)" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   514
 by (Asm_full_simp_tac 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   515
by (subgoal_tac "b*q = r' - r + b'*q'" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   516
 by (simp_tac (simpset() addsimps zcompare_rls) 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   517
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib2]) 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   518
by (res_inst_tac [("z1","b'*q'")] (zadd_commute RS ssubst) 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   519
by (rtac zadd_zless_mono 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   520
by (arith_tac 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   521
by (rtac zmult_zle_mono1 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   522
by Auto_tac;
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   523
qed "zdiv_mono2_lemma";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   524
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   525
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   526
Goal "[| (#0::int) <= a;  #0 < b';  b' <= b |]  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   527
\     ==> a div b <= a div b'";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   528
by (subgoal_tac "b ~= #0" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   529
by (arith_tac 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   530
by (cut_inst_tac [("a","a"),("b","b")] zmod_zdiv_equality 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   531
by (cut_inst_tac [("a","a"),("b","b'")] zmod_zdiv_equality 1);
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   532
by Auto_tac;
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   533
by (rtac zdiv_mono2_lemma 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   534
by (etac subst 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   535
by (etac subst 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   536
by (ALLGOALS (asm_simp_tac (simpset() addsimps [pos_mod_sign,pos_mod_bound])));
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   537
qed "zdiv_mono2";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   538
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   539
Goal "[| r + b*q = r' + b'*q';  r' + b'*q' < #0;  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   540
\        r < b;  #0 <= r';  #0 < b';  b' <= b |]  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   541
\     ==> q' <= (q::int)";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   542
by (subgoal_tac "q' < #0" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   543
 by (subgoal_tac "b'*q' < #0" 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   544
  by (arith_tac 3);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   545
 by (asm_full_simp_tac (simpset() addsimps [pos_imp_zmult_neg_iff]) 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   546
by (subgoal_tac "b*q' < b*(q + #1)" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   547
 by (Asm_full_simp_tac 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   548
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib2]) 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   549
by (subgoal_tac "b*q' <= b'*q'" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   550
 by (asm_simp_tac (simpset() addsimps [zmult_zle_mono1_neg]) 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   551
by (subgoal_tac "b'*q' < b + b*q" 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   552
 by (Asm_simp_tac 2);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   553
by (arith_tac 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   554
qed "zdiv_mono2_neg_lemma";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   555
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   556
Goal "[| a < (#0::int);  #0 < b';  b' <= b |]  \
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   557
\     ==> a div b' <= a div b";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   558
by (cut_inst_tac [("a","a"),("b","b")] zmod_zdiv_equality 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   559
by (cut_inst_tac [("a","a"),("b","b'")] zmod_zdiv_equality 1);
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   560
by Auto_tac;
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   561
by (rtac zdiv_mono2_neg_lemma 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   562
by (etac subst 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   563
by (etac subst 1);
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   564
by (ALLGOALS (asm_simp_tac (simpset() addsimps [pos_mod_sign,pos_mod_bound])));
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   565
qed "zdiv_mono2_neg";
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   566
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   567
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   568
(*** More algebraic laws for div and mod ***)
6943
2cde117d2738 faster division algorithm; monotonicity of div in 2nd arg
paulson
parents: 6917
diff changeset
   569
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   570
(** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   571
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   572
Goal "[| quorem((b,c),(q,r));  c ~= #0 |] \
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   573
\     ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   574
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   575
    (claset(),
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   576
     simpset() addsimps split_ifs@
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   577
                        [quorem_def, linorder_neq_iff, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   578
			 zadd_zmult_distrib2,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   579
			 pos_mod_sign,pos_mod_bound,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   580
			 neg_mod_sign, neg_mod_bound]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   581
by (rtac (zmod_zdiv_equality RS trans) 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   582
by (rtac (zmod_zdiv_equality RS trans) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   583
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   584
val lemma = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   585
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   586
Goal "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   587
by (zdiv_undefined_case_tac "c = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   588
by (blast_tac (claset() addIs [quorem_div_mod RS lemma RS quorem_div]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   589
qed "zdiv_zmult1_eq";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   590
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   591
Goal "(a*b) mod c = a*(b mod c) mod (c::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   592
by (zdiv_undefined_case_tac "c = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   593
by (blast_tac (claset() addIs [quorem_div_mod RS lemma RS quorem_mod]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   594
qed "zmod_zmult1_eq";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   595
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   596
Goal "b ~= (#0::int) ==> (a*b) div b = a";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   597
by (asm_simp_tac (simpset() addsimps [zdiv_zmult1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   598
qed "zdiv_zmult_self1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   599
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   600
Goal "b ~= (#0::int) ==> (b*a) div b = a";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   601
by (asm_simp_tac (simpset() addsimps [zdiv_zmult1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   602
qed "zdiv_zmult_self2";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   603
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   604
Addsimps [zdiv_zmult_self1, zdiv_zmult_self2];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   605
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   606
Goal "(a*b) mod b = (#0::int)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   607
by (simp_tac (simpset() addsimps [zmod_zmult1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   608
qed "zmod_zmult_self1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   609
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   610
Goal "(b*a) mod b = (#0::int)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   611
by (simp_tac (simpset() addsimps [zmod_zmult1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   612
qed "zmod_zmult_self2";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   613
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   614
Addsimps [zmod_zmult_self1, zmod_zmult_self2];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   615
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   616
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   617
(** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   618
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   619
Goal "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  c ~= #0 |] \
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   620
\     ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   621
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   622
    (claset(),
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   623
     simpset() addsimps split_ifs@
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   624
                        [quorem_def, linorder_neq_iff, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   625
			 zadd_zmult_distrib2,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   626
			 pos_mod_sign,pos_mod_bound,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   627
			 neg_mod_sign, neg_mod_bound]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   628
by (rtac (zmod_zdiv_equality RS trans) 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   629
by (rtac (zmod_zdiv_equality RS trans) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   630
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   631
val lemma = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   632
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   633
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   634
Goal "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   635
by (zdiv_undefined_case_tac "c = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   636
by (blast_tac (claset() addIs [[quorem_div_mod,quorem_div_mod]
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   637
			       MRS lemma RS quorem_div]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   638
qed "zdiv_zadd1_eq";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   639
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   640
Goal "(a+b) mod (c::int) = (a mod c + b mod c) mod c";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   641
by (zdiv_undefined_case_tac "c = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   642
by (blast_tac (claset() addIs [[quorem_div_mod,quorem_div_mod]
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   643
			       MRS lemma RS quorem_mod]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   644
qed "zmod_zadd1_eq";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   645
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   646
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   647
Goal "(a mod b) div b = (#0::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   648
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   649
by (auto_tac (claset(), 
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   650
       simpset() addsimps [linorder_neq_iff, 
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   651
			   pos_mod_sign, pos_mod_bound, div_pos_pos_trivial, 
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   652
			   neg_mod_sign, neg_mod_bound, div_neg_neg_trivial]));
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   653
qed "mod_div_trivial";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   654
Addsimps [mod_div_trivial];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   655
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   656
Goal "(a mod b) mod b = a mod (b::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   657
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   658
by (auto_tac (claset(), 
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   659
       simpset() addsimps [linorder_neq_iff, 
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   660
			   pos_mod_sign, pos_mod_bound, mod_pos_pos_trivial, 
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   661
			   neg_mod_sign, neg_mod_bound, mod_neg_neg_trivial]));
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   662
qed "mod_mod_trivial";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   663
Addsimps [mod_mod_trivial];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   664
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   665
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   666
Goal "a ~= (#0::int) ==> (a+b) div a = b div a + #1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   667
by (asm_simp_tac (simpset() addsimps [zdiv_zadd1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   668
qed "zdiv_zadd_self1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   669
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   670
Goal "a ~= (#0::int) ==> (b+a) div a = b div a + #1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   671
by (asm_simp_tac (simpset() addsimps [zdiv_zadd1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   672
qed "zdiv_zadd_self2";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   673
Addsimps [zdiv_zadd_self1, zdiv_zadd_self2];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   674
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   675
Goal "(a+b) mod a = b mod (a::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   676
by (zdiv_undefined_case_tac "a = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   677
by (asm_simp_tac (simpset() addsimps [zmod_zadd1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   678
qed "zmod_zadd_self1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   679
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   680
Goal "(b+a) mod a = b mod (a::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   681
by (zdiv_undefined_case_tac "a = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   682
by (asm_simp_tac (simpset() addsimps [zmod_zadd1_eq]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   683
qed "zmod_zadd_self2";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   684
Addsimps [zmod_zadd_self1, zmod_zadd_self2];
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   685
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   686
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   687
(*** proving  a div (b*c) = (a div b) div c ***)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   688
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   689
(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   690
  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   691
  to cause particular problems.*)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   692
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   693
(** first, four lemmas to bound the remainder for the cases b<0 and b>0 **)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   694
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   695
Goal "[| (#0::int) < c;  b < r;  r <= #0 |] ==> b*c < r + b*(q mod c)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   696
by (subgoal_tac "b * (c - q mod c) < r * #1" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   697
by (asm_full_simp_tac (simpset() addsimps [zdiff_zmult_distrib2]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   698
by (rtac order_le_less_trans 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   699
by (etac zmult_zless_mono1 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   700
by (rtac zmult_zle_mono2_neg 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   701
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   702
    (claset(),
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   703
     simpset() addsimps zcompare_rls@[add1_zle_eq,pos_mod_bound]));
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   704
val lemma1 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   705
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   706
Goal "[| (#0::int) < c;   b < r;  r <= #0 |] ==> r + b * (q mod c) <= #0";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   707
by (subgoal_tac "b * (q mod c) <= #0" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   708
by (arith_tac 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   709
by (asm_simp_tac (simpset() addsimps [neg_imp_zmult_nonpos_iff, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   710
				      pos_mod_sign]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   711
val lemma2 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   712
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   713
Goal "[| (#0::int) < c;  #0 <= r;  r < b |] ==> #0 <= r + b * (q mod c)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   714
by (subgoal_tac "#0 <= b * (q mod c)" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   715
by (arith_tac 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   716
by (asm_simp_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   717
    (simpset() addsimps [pos_imp_zmult_nonneg_iff, pos_mod_sign]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   718
val lemma3 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   719
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   720
Goal "[| (#0::int) < c; #0 <= r; r < b |] ==> r + b * (q mod c) < b * c";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   721
by (subgoal_tac "r * #1 < b * (c - q mod c)" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   722
by (asm_full_simp_tac (simpset() addsimps [zdiff_zmult_distrib2]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   723
by (rtac order_less_le_trans 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   724
by (etac zmult_zless_mono1 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   725
by (rtac zmult_zle_mono2 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   726
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   727
    (claset(),
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   728
     simpset() addsimps zcompare_rls@[add1_zle_eq,pos_mod_bound]));
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   729
val lemma4 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   730
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   731
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   732
Goal "[| quorem ((a,b), (q,r));  b ~= #0;  #0 < c |] \
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   733
\     ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   734
by (auto_tac  (*SLOW*)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   735
    (claset(),
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   736
     simpset() addsimps split_ifs@
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   737
                        [quorem_def, linorder_neq_iff,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   738
			 pos_imp_zmult_pos_iff,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   739
			 neg_imp_zmult_pos_iff,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   740
			 zadd_zmult_distrib2 RS sym,
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   741
			 lemma1, lemma2, lemma3, lemma4]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   742
by (rtac (zmod_zdiv_equality RS trans) 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   743
by (rtac (zmod_zdiv_equality RS trans) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   744
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   745
val lemma = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   746
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   747
Goal "(#0::int) < c ==> a div (b*c) = (a div b) div c";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   748
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   749
by (force_tac (claset(),
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   750
	       simpset() addsimps [quorem_div_mod RS lemma RS quorem_div, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   751
				   zmult_eq_0_iff]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   752
qed "zdiv_zmult2_eq";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   753
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   754
Goal "[| (#0::int) < c |] \
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   755
\     ==> a mod (b*c) = b*(a div b mod c) + a mod b";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   756
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   757
by (force_tac (claset(),
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   758
	       simpset() addsimps [quorem_div_mod RS lemma RS quorem_mod, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   759
				   zmult_eq_0_iff]) 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   760
qed "zmod_zmult2_eq";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   761
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   762
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   763
(*** Cancellation of common factors in "div" ***)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   764
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   765
Goal "[| (#0::int) < b;  c ~= #0 |] ==> (c*a) div (c*b) = a div b";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   766
by (stac zdiv_zmult2_eq 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   767
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   768
val lemma1 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   769
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   770
Goal "[| b < (#0::int);  c ~= #0 |] ==> (c*a) div (c*b) = a div b";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   771
by (subgoal_tac "(c * -a) div (c * -b) = -a div -b" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   772
by (rtac lemma1 2);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   773
by Auto_tac;
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   774
val lemma2 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   775
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   776
Goal "c ~= (#0::int) ==> (c*a) div (c*b) = a div b";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   777
by (zdiv_undefined_case_tac "b = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   778
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   779
    (claset(), 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   780
     simpset() delsimps zmult_ac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   781
	       addsimps [read_instantiate [("x", "b")] linorder_neq_iff, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   782
			 lemma1, lemma2]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   783
qed "zdiv_zmult_zmult1";
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
   784
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   785
Goal "c ~= (#0::int) ==> (a*c) div (b*c) = a div b";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   786
by (dtac zdiv_zmult_zmult1 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   787
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   788
qed "zdiv_zmult_zmult2";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   789
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   790
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   791
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   792
(*** Distribution of factors over "mod" ***)
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   793
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   794
Goal "[| (#0::int) < b;  c ~= #0 |] ==> (c*a) mod (c*b) = c * (a mod b)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   795
by (stac zmod_zmult2_eq 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   796
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   797
val lemma1 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   798
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   799
Goal "[| b < (#0::int);  c ~= #0 |] ==> (c*a) mod (c*b) = c * (a mod b)";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   800
by (subgoal_tac "(c * -a) mod (c * -b) = c * (-a mod -b)" 1);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   801
by (rtac lemma1 2);
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   802
by (auto_tac (claset(), 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   803
	      simpset() addsimps [zmod_zminus_zminus]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   804
val lemma2 = result();
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   805
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   806
Goal "(c*a) mod (c*b) = (c::int) * (a mod b)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   807
by (zdiv_undefined_case_tac "b = #0" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   808
by (zdiv_undefined_case_tac "c = #0" 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   809
by (auto_tac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   810
    (claset(), 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   811
     simpset() delsimps zmult_ac
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   812
	       addsimps [read_instantiate [("x", "b")] linorder_neq_iff, 
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   813
			 lemma1, lemma2]));
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   814
qed "zmod_zmult_zmult1";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   815
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   816
Goal "(a*c) mod (b*c) = (a mod b) * (c::int)";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   817
by (cut_inst_tac [("c","c")] zmod_zmult_zmult1 1);
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   818
by Auto_tac;
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   819
qed "zmod_zmult_zmult2";
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   820
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   821
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   822
(*** Speeding up the division algorithm with shifting ***)
6992
8113992d3f45 many new theorems
paulson
parents: 6943
diff changeset
   823
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   824
(** computing "div" by shifting **)
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   825
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   826
Goal "(#0::int) <= a ==> (#1 + #2*b) div (#2*a) = b div a";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   827
by (zdiv_undefined_case_tac "a = #0" 1);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   828
by (subgoal_tac "#1 <= a" 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   829
by (arith_tac 2);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   830
by (subgoal_tac "#1 < a * #2" 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   831
by (dres_inst_tac [("i","#1"), ("k", "#2")] zmult_zle_mono1 2);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   832
by (subgoal_tac "#2*(#1 + b mod a) <= #2*a" 1);
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   833
by (rtac zmult_zle_mono2 2);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   834
by (auto_tac (claset(),
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   835
	      simpset() addsimps [add1_zle_eq,pos_mod_bound]));
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   836
by (stac zdiv_zadd1_eq 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   837
by (auto_tac (claset(),
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   838
	      simpset() addsimps [zdiv_zmult_zmult2, zmod_zmult_zmult2, 
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   839
				  div_pos_pos_trivial]));
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   840
by (stac div_pos_pos_trivial 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   841
by (asm_simp_tac (simpset() addsimps [zmult_2_right, mod_pos_pos_trivial, 
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   842
	   pos_mod_sign RS zadd_zle_mono1 RSN (2,order_trans)]) 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   843
by (auto_tac (claset(),
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   844
	      simpset() addsimps [mod_pos_pos_trivial]));
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   845
qed "pos_zdiv_times_2";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   846
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   847
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   848
Goal "a <= (#0::int) ==> (#1 + #2*b) div (#2*a) = (b+#1) div a";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   849
by (subgoal_tac "(#1 + #2*(-b-#1)) div (#2 * -a) = (-b-#1) div (-a)" 1);
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   850
by (rtac pos_zdiv_times_2 2);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   851
by Auto_tac;
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   852
by (subgoal_tac "(#-1 + - (b * #2)) = - (#1 + (b*#2))" 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   853
by (Simp_tac 2);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   854
by (asm_full_simp_tac (HOL_ss
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   855
		       addsimps [zdiv_zminus_zminus, zdiff_def,
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   856
				 zminus_zadd_distrib RS sym]) 1);
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   857
qed "neg_zdiv_times_2";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   858
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   859
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   860
(*Not clear why this must be proved separately; probably number_of causes
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   861
  simplification problems*)
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   862
Goal "~ #0 <= x ==> x <= (#0::int)";
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   863
by Auto_tac;
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   864
val lemma = result();
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   865
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   866
Goal "number_of (v BIT b) div number_of (w BIT False) = \
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   867
\         (if ~b | (#0::int) <= number_of w                   \
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   868
\          then number_of v div (number_of w)    \
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   869
\          else (number_of v + (#1::int)) div (number_of w))";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   870
by (simp_tac (simpset_of Int.thy
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   871
			 addsimps [zadd_assoc, number_of_BIT]) 1);
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   872
by (asm_simp_tac (simpset()
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   873
		  delsimps zmult_ac@bin_arith_extra_simps@bin_rel_simps
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   874
		  addsimps [zmult_2 RS sym, zdiv_zmult_zmult1,
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   875
			    pos_zdiv_times_2, lemma, neg_zdiv_times_2]) 1);
6999
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   876
qed "zdiv_number_of_BIT";
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   877
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   878
Addsimps [zdiv_number_of_BIT];
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   879
73f681047e5f optimization for division by powers of 2
paulson
parents: 6992
diff changeset
   880
7035
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   881
(** computing "mod" by shifting **)
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   882
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   883
Goal "(#0::int) <= a ==> (#1 + #2*b) mod (#2*a) = #1 + #2 * (b mod a)";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   884
by (zdiv_undefined_case_tac "a = #0" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   885
by (subgoal_tac "#1 <= a" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   886
by (arith_tac 2);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   887
by (subgoal_tac "#1 < a * #2" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   888
by (dres_inst_tac [("i","#1"), ("k", "#2")] zmult_zle_mono1 2);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   889
by (subgoal_tac "#2*(#1 + b mod a) <= #2*a" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   890
by (rtac zmult_zle_mono2 2);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   891
by (auto_tac (claset(),
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   892
	      simpset() addsimps [add1_zle_eq,pos_mod_bound]));
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   893
by (stac zmod_zadd1_eq 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   894
by (auto_tac (claset(),
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   895
	      simpset() addsimps [zmod_zmult_zmult2, zmod_zmult_zmult2, 
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   896
				  mod_pos_pos_trivial]));
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   897
by (rtac mod_pos_pos_trivial 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   898
by (asm_simp_tac (simpset() addsimps [zmult_2_right, mod_pos_pos_trivial, 
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   899
	   pos_mod_sign RS zadd_zle_mono1 RSN (2,order_trans)]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   900
by (auto_tac (claset(),
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   901
	      simpset() addsimps [mod_pos_pos_trivial]));
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   902
qed "pos_zmod_times_2";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   903
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   904
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   905
Goal "a <= (#0::int) ==> (#1 + #2*b) mod (#2*a) = #2 * ((b+#1) mod a) - #1";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   906
by (subgoal_tac 
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   907
    "(#1 + #2*(-b-#1)) mod (#2*-a) = #1 + #2*((-b-#1) mod (-a))" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   908
by (rtac pos_zmod_times_2 2);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   909
by Auto_tac;
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   910
by (subgoal_tac "(#-1 + - (b * #2)) = - (#1 + (b*#2))" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   911
by (Simp_tac 2);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   912
by (asm_full_simp_tac (HOL_ss
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   913
		       addsimps [zmod_zminus_zminus, zdiff_def,
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   914
				 zminus_zadd_distrib RS sym]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   915
bd (zminus_equation RS iffD1 RS sym) 1;
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   916
by Auto_tac;
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   917
qed "neg_zmod_times_2";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   918
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   919
Goal "number_of (v BIT b) mod number_of (w BIT False) = \
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   920
\         (if b then \
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   921
\               if (#0::int) <= number_of w \
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   922
\               then #2 * (number_of v mod number_of w) + #1    \
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   923
\               else #2 * ((number_of v + (#1::int)) mod number_of w) - #1  \
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   924
\          else #2 * (number_of v mod number_of w))";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   925
by (simp_tac (simpset_of Int.thy
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   926
			 addsimps [zadd_assoc, number_of_BIT]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   927
by (asm_simp_tac (simpset()
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   928
		  delsimps zmult_ac@bin_arith_extra_simps@bin_rel_simps
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   929
		  addsimps [zmult_2 RS sym, zmod_zmult_zmult1,
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   930
			    pos_zmod_times_2, lemma, neg_zmod_times_2]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   931
qed "zmod_number_of_BIT";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   932
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   933
Addsimps [zmod_number_of_BIT];
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   934
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   935
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   936
(** Quotients of signs **)
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   937
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   938
Goal "[| a < (#0::int);  #0 < b |] ==> a div b < #0";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   939
by (subgoal_tac "a div b <= #-1" 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   940
by (Force_tac 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   941
by (rtac order_trans 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   942
by (res_inst_tac [("a'","#-1")]  zdiv_mono1 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   943
by (auto_tac (claset(), simpset() addsimps [div_minus1]));
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   944
qed "div_neg_pos";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   945
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   946
Goal "[| (#0::int) <= a;  b < #0 |] ==> a div b <= #0";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   947
by (dtac zdiv_mono1_neg 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   948
by Auto_tac;
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   949
qed "div_nonneg_neg";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   950
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   951
Goal "(#0::int) < b ==> (#0 <= a div b) = (#0 <= a)";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   952
by Auto_tac;
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   953
by (dtac zdiv_mono1 2);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   954
by (auto_tac (claset(), simpset() addsimps [linorder_neq_iff]));
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   955
by (full_simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   956
by (blast_tac (claset() addIs [div_neg_pos]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   957
qed "pos_imp_zdiv_nonneg_iff";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   958
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   959
Goal "b < (#0::int) ==> (#0 <= a div b) = (a <= (#0::int))";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   960
by (stac (zdiv_zminus_zminus RS sym) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   961
by (stac pos_imp_zdiv_nonneg_iff 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   962
by Auto_tac;
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   963
qed "neg_imp_zdiv_nonneg_iff";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   964
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   965
(*But not (a div b <= 0 iff a<=0); consider a=1, b=2 when a div b = 0.*)
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   966
Goal "(#0::int) < b ==> (a div b < #0) = (a < #0)";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   967
by (asm_simp_tac (simpset() addsimps [linorder_not_le RS sym,
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   968
				      pos_imp_zdiv_nonneg_iff]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   969
qed "pos_imp_zdiv_neg_iff";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   970
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   971
(*Again the law fails for <=: consider a = -1, b = -2 when a div b = 0*)
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   972
Goal "b < (#0::int) ==> (a div b < #0) = (#0 < a)";
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   973
by (asm_simp_tac (simpset() addsimps [linorder_not_le RS sym,
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   974
				      neg_imp_zdiv_nonneg_iff]) 1);
d1b7a2372b31 many new laws about div and mod
paulson
parents: 6999
diff changeset
   975
qed "neg_imp_zdiv_neg_iff";