src/HOL/Integ/IntDiv.thy
author wenzelm
Wed, 05 Dec 2001 03:06:05 +0100
changeset 12371 80ca9058db95
parent 11868 56db9f3a6b3e
child 13183 c7290200b3f4
permissions -rw-r--r--
tuned;
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.thy
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"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     7
*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
     8
7706
da41066983e5 IntArith;
wenzelm
parents: 6993
diff changeset
     9
IntDiv = IntArith + Recdef + 
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    10
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    11
constdefs
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    12
  quorem :: "(int*int) * (int*int) => bool"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    13
    "quorem == %((a,b), (q,r)).
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    14
                      a = b*q + r &
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    15
                      (if 0 < b then 0<=r & r<b else b<r & r <= 0)"
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    16
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    17
  adjust :: "[int, int*int] => int*int"
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    18
    "adjust b == %(q,r). if 0 <= r-b then (2*q + 1, r-b)
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    19
                         else (2*q, r)"
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    20
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    21
(** the division algorithm **)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    22
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    23
(*for the case a>=0, b>0*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    24
consts posDivAlg :: "int*int => int*int"
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    25
recdef posDivAlg "inv_image less_than (%(a,b). nat(a - b + 1))"
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    26
    "posDivAlg (a,b) =
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    27
       (if (a<b | b<=0) then (0,a)
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    28
        else adjust b (posDivAlg(a, 2*b)))"
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    29
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    30
(*for the case a<0, b>0*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    31
consts negDivAlg :: "int*int => int*int"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    32
recdef negDivAlg "inv_image less_than (%(a,b). nat(- a - b))"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    33
    "negDivAlg (a,b) =
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    34
       (if (0<=a+b | b<=0) then (-1,a+b)
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    35
        else adjust b (negDivAlg(a, 2*b)))"
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    36
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    37
(*for the general case b~=0*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    38
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    39
constdefs
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    40
  negateSnd :: "int*int => int*int"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    41
    "negateSnd == %(q,r). (q,-r)"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    42
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    43
  (*The full division algorithm considers all possible signs for a, b
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    44
    including the special case a=0, b<0, because negDivAlg requires a<0*)
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    45
  divAlg :: "int*int => int*int"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    46
    "divAlg ==
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    47
       %(a,b). if 0<=a then
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    48
                  if 0<=b then posDivAlg (a,b)
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    49
                  else if a=0 then (0,0)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    50
                       else negateSnd (negDivAlg (-a,-b))
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    51
               else 
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11704
diff changeset
    52
                  if 0<b then negDivAlg (a,b)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    53
                  else         negateSnd (posDivAlg (-a,-b))"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    54
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    55
instance
8902
a705822f4e2a replaced {{ }} by { };
wenzelm
parents: 7706
diff changeset
    56
  int :: "Divides.div"        (*avoid clash with 'div' token*)
6917
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    57
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    58
defs
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    59
  div_def   "a div b == fst (divAlg (a,b))"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    60
  mod_def   "a mod b == snd (divAlg (a,b))"
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    61
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    62
eba301caceea Introduction of integer division algorithm
paulson
parents:
diff changeset
    63
end