-- Copyright 2023 Lennart Augustsson-- See LICENSE file for full license.module Data.Integral(module Data.Integral) whereimport qualified Prelude() -- do not import Preludeimport Primitivesimport Control.Errorimport Data.Boolimport Data.Enum_Classimport Data.Eqimport Data.Integer_Typeimport Data.Numimport Data.Ordimport Data.Realimport {-# SOURCE #-} Data.Typeableinfixl 7 `quot`,`rem`infixl 7 `div`,`mod`-- XXX Importing Data.Enum causes an import cycle.-- I don't really see the point of Enum as a superclass.class (Real a, Enum a) => Integral a where quot :: a -> a -> a rem :: a -> a -> a div :: a -> a -> a mod :: a -> a -> a quotRem :: a -> a -> (a, a) divMod :: a -> a -> (a, a) toInteger :: a -> Integer -- Used by fromIntegral to avoid going through Integer. -- Must be the same as fromInteger . toInteger. _fromIntegral :: forall b . Num b => a -> b n `quot` d = q where (q,r) = quotRem n d n `rem` d = r where (q,r) = quotRem n d n `div` d = q where (q,r) = divMod n d n `mod` d = r where (q,r) = divMod n d divMod n d = if signum r == negate (signum d) then (q - 1, r + d) else qr where qr@(q,r) = quotRem n d quotRem n d = (quot n d, rem n d) _fromIntegral x = fromInteger (toInteger x)gcd :: forall a . (Integral a) => a -> a -> agcd x y = gcd' (abs x) (abs y) where gcd' a b = if b == 0 then a else gcd' b (a `rem` b)lcm :: forall a . (Integral a) => a -> a -> alcm x y = if x == 0 || y == 0 then 0 else abs ((x `quot` gcd x y) * y)even :: forall a . (Integral a) => a -> Booleven n = n `rem` 2 == 0odd :: forall a . (Integral a) => a -> Boolodd n = not (even n)infixr 8 ^(^) :: forall a b . (Num a, Integral b, Ord b) => a -> b -> ax0 ^ y0 | y0 < 0 = error "Data.Integral.^: negative exponent" | otherwise = pow x0 y0 -- This does not do the minimal number of multiplications, but it's simple. where pow x y | y == 0 = 1 | even y = pow (x * x) (y `quot` 2) | otherwise = x * pow (x * x) (y `quot` 2)fromIntegral :: forall a b . (Integral a, Num b) => a -> bfromIntegral = _fromIntegral