on master help sign in
Partial support for Integral Classes.

Added a simple Integral Class to Classes supporting: + div + mod toInteger and other suitable methods need to be decided upon, defined, and implemented. Instances: + Nat uses total functiond + Int, Integer, Bits8-64 are partial.

commited8d233e49befdc9e58dd1392403390e18bbfda8 authorJan de Muijnck-Hughes <jfdm@st-andrews.ac.uk> dateWed 27 Nov 2013, 16:30 (13 years ago) parentec3daa4
2 files changed+97−19
libs/prelude/Prelude/Classes.idr+82−10 libs/prelude/Prelude/Nat.idr+15−9
libs/prelude/Prelude/Classes.idr+82−10
@@ -11,7 +11,7 @@ infixl 7 <<, >>
1111infixl 8 +,-
1212infixl 9 *,/
1313
14−-- ------------------------------------------------------- [ Numeric Operators ]
14+-- ------------------------------------------------------------- [ Boolean Ops ]
1515intToBool : Int -> Bool
1616intToBool 0 = False
1717intToBool x = True
@@ -19,6 +19,7 @@ intToBool x = True
1919boolOp : (a -> a -> Int) -> a -> a -> Bool
2020boolOp op x y = intToBool (op x y)
2121
22+-- ---------------------------------------------------------- [ Equality Class ]
2223-- | The Eq class defines inequality and equality.
2324class Eq a where
2425 (==) : a -> a -> Bool
@@ -47,11 +48,12 @@ instance Eq Bool where
4748 True == False = False
4849 False == True = False
4950 False == False = True
50−
51+
5152instance (Eq a, Eq b) => Eq (a, b) where
5253 (==) (a, c) (b, d) = (a == b) && (c == d)
5354
5455
56+-- ---------------------------------------------------------- [ Ordering Class ]
5557data Ordering = LT | EQ | GT
5658
5759instance Eq Ordering where
@@ -123,6 +125,7 @@ instance (Ord a, Ord b) => Ord (a, b) where
123125 then compare xl yl
124126 else compare xr yr
125127
128+-- --------------------------------------------------------- [ Numerical Class ]
126129-- | The Num class defines basic numerical arithmetic.
127130class Num a where
128131 (+) : a -> a -> a
@@ -234,17 +237,86 @@ instance Ord Bits64 where
234237 else if l > r then GT
235238 else EQ
236239
237−-- | Integer division.
238−partial
239−div : Integer -> Integer -> Integer
240−div = prim__sdivBigInt
241240
242−-- | Integer modulus.
243−partial
244−mod : Integer -> Integer -> Integer
245−mod = prim__sremBigInt
241+-- ------------------------------------------------------------- [ Fractionals ]
246242
247243-- | Fractional division of two Floats.
248244(/) : Float -> Float -> Float
249245(/) = prim__divFloat
250246
247+
248+-- --------------------------------------------------------------- [ Integrals ]
249+%default partial
250+
251+class Integral a where
252+ div : a -> a -> a
253+ mod : a -> a -> a
254+
255+-- ---------------------------------------------------------------- [ Integers ]
256+divBigInt : Integer -> Integer -> Integer
257+divBigInt = prim__sdivBigInt
258+
259+modBigInt : Integer -> Integer -> Integer
260+modBigInt = prim__sremBigInt
261+
262+instance Integral Integer where
263+ div = divBigInt
264+ mod = modBigInt
265+
266+-- --------------------------------------------------------------------- [ Int ]
267+
268+divInt : Int -> Int -> Int
269+divInt = prim__sdivInt
270+
271+modInt : Int -> Int -> Int
272+modInt = prim__sremInt
273+
274+instance Integral Int where
275+ div = divInt
276+ mod = modInt
277+
278+-- ------------------------------------------------------------------- [ Bits8 ]
279+divB8 : Bits8 -> Bits8 -> Bits8
280+divB8 = prim__sdivB8
281+
282+modB8 : Bits8 -> Bits8 -> Bits8
283+modB8 = prim__sremB8
284+
285+instance Integral Bits8 where
286+ div = divB8
287+ mod = modB8
288+
289+-- ------------------------------------------------------------------ [ Bits16 ]
290+divB16 : Bits16 -> Bits16 -> Bits16
291+divB16 = prim__sdivB16
292+
293+modB16 : Bits16 -> Bits16 -> Bits16
294+modB16 = prim__sremB16
295+
296+instance Integral Bits16 where
297+ div = divB16
298+ mod = modB16
299+
300+-- ------------------------------------------------------------------ [ Bits32 ]
301+divB32 : Bits32 -> Bits32 -> Bits32
302+divB32 = prim__sdivB32
303+
304+modB32 : Bits32 -> Bits32 -> Bits32
305+modB32 = prim__sremB32
306+
307+instance Integral Bits32 where
308+ div = divB32
309+ mod = modB32
310+
311+-- ------------------------------------------------------------------ [ Bits64 ]
312+divB64 : Bits64 -> Bits64 -> Bits64
313+divB64 = prim__sdivB64
314+
315+modB64 : Bits64 -> Bits64 -> Bits64
316+modB64 = prim__sremB64
317+
318+instance Integral Bits64 where
319+ div = divB64
320+ mod = modB64
321+
322+
libs/prelude/Prelude/Nat.idr+15−9
@@ -227,9 +227,10 @@ fact (S n) = (S n) * fact n
227227-- Division and modulus
228228--------------------------------------------------------------------------------
229229
230−total mod : Nat -> Nat -> Nat
231−mod left Z = left
232−mod left (S right) = mod' left left right
230+total
231+modNat : Nat -> Nat -> Nat
232+modNat left Z = left
233+modNat left (S right) = mod' left left right
233234 where
234235 total mod' : Nat -> Nat -> Nat -> Nat
235236 mod' Z centre right = centre
@@ -239,9 +240,10 @@ mod left (S right) = mod' left left right
239240 else
240241 mod' left (centre - (S right)) right
241242
242−total div : Nat -> Nat -> Nat
243−div left Z = S left -- div by zero
244−div left (S right) = div' left left right
243+total
244+divNat : Nat -> Nat -> Nat
245+divNat left Z = S left -- div by zero
246+divNat left (S right) = div' left left right
245247 where
246248 total div' : Nat -> Nat -> Nat -> Nat
247249 div' Z centre right = Z
@@ -251,11 +253,15 @@ div left (S right) = div' left left right
251253 else
252254 S (div' left (centre - (S right)) right)
253255
256+instance Integral Nat where
257+ div = divNat
258+ mod = modNat
259+
254260%assert_total
255261log2 : Nat -> Nat
256262log2 Z = Z
257263log2 (S Z) = Z
258−log2 n = S (log2 (n `div` 2))
264+log2 n = S (log2 (n `divNat` 2))
259265
260266--------------------------------------------------------------------------------
261267-- GCD and LCM
@@ -263,12 +269,12 @@ log2 n = S (log2 (n `div` 2))
263269%assert_total
264270gcd : Nat -> Nat -> Nat
265271gcd a Z = a
266−gcd a b = gcd b (a `mod` b)
272+gcd a b = gcd b (a `modNat` b)
267273
268274total lcm : Nat -> Nat -> Nat
269275lcm _ Z = Z
270276lcm Z _ = Z
271−lcm x y = div (x * y) (gcd x y)
277+lcm x y = divNat (x * y) (gcd x y)
272278
273279--------------------------------------------------------------------------------
274280-- Properties