2
module Data.Integer where
8
open Nat using (Nat; suc; zero)
24
neg : Nat -> Int -- neg n = -(n + 1)
26
infix 40 _==_ _<_ _>_ _≤_ _≥_
36
_+_ : Int -> Int -> Int
37
pos n + pos m = pos (n +' m)
38
neg n + neg m = neg (n +' m +' 1)
40
! m <' n => pos (n -' m -' 1)
41
! otherwise neg (m -' n)
42
neg n + pos m = pos m + neg n
44
_-_ : Int -> Int -> Int
51
_*_ : Int -> Int -> Int
54
pos n * pos m = pos (n *' m)
55
neg n * neg m = pos (suc n *' suc m)
56
pos n * neg m = neg (n *' suc m -' 1)
57
neg n * pos m = neg (suc n *' m -' 1)
59
div : Int -> Int -> Int
61
div (pos n) (pos m) = pos (div' n m)
62
div (neg n) (neg m) = pos (div' (suc n) (suc m))
63
div (pos 0) (neg _) = pos 0
64
div (pos (suc n)) (neg m) = neg (div' n (suc m))
65
div (neg n) (pos (suc m)) = div (pos (suc n)) (neg m)
67
mod : Int -> Int -> Int
69
mod (pos n) (pos m) = pos (mod' n m)
70
mod (neg n) (pos m) = adjust (mod' (suc n) m)
74
adjust n = pos (m -' n)
75
mod n (neg m) = adjust (mod n (pos (suc m)))
78
adjust (pos 0) = pos 0
79
adjust (neg n) = neg n -- impossible
82
gcd : Int -> Int -> Int
83
gcd a b = pos (gcd' ! a ! ! b !)
85
lcm : Int -> Int -> Int
86
lcm a b = pos (lcm' ! a ! ! b !)
88
_==_ : Int -> Int -> Bool
89
pos n == pos m = n ==' m
90
neg n == neg m = n ==' m
91
pos _ == neg _ = false
92
neg _ == pos _ = false
94
_<_ : Int -> Int -> Bool
97
pos n < pos m = n <' m
98
neg n < neg m = m <' n
100
_≤_ : Int -> Int -> Bool
101
x ≤ y = x == y || x < y