shithub: MicroHs

ref: b82c264918bc202f5d0756cd860598cac3f3a6a2
dir: /lib/Data/Integral.hs/

View raw version
-- Copyright 2023 Lennart Augustsson
-- See LICENSE file for full license.
module Data.Integral(module Data.Integral) where
import Primitives
import Data.Bool
import Data.Eq
import Data.Integer_Type
import Data.Num

infixl 7 `quot`,`rem`

class {-(Real a, Enum a) => -} (Eq a, Num 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

  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)

gcd :: forall a . (Integral a) => a -> a -> a
gcd 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 -> a
lcm x y =
  if x == 0 || y == 0 then
    0
  else
    abs ((x `quot` (gcd x y)) * y)