module GHC.Natural
( Natural (NatS#, NatJ#)
, B.BigNat (..)
, mkNatural
, isValidNatural
, plusNatural
, minusNatural
, minusNaturalMaybe
, timesNatural
, negateNatural
, signumNatural
, quotRemNatural
, quotNatural
, remNatural
, gcdNatural
, lcmNatural
, andNatural
, orNatural
, xorNatural
, bitNatural
, testBitNatural
, popCountNatural
, shiftLNatural
, shiftRNatural
, naturalToInteger
, naturalToWord
, naturalToWordMaybe
, wordToNatural
, wordToNatural#
, naturalFromInteger
, powModNatural
)
where
import GHC.Prim
import GHC.Types
import GHC.Maybe
import GHC.Num.Natural (Natural)
import GHC.Num.Integer (Integer)
import qualified GHC.Num.BigNat as B
import qualified GHC.Num.Natural as N
import qualified GHC.Num.Integer as I
pattern NatS# :: Word# -> Natural
pattern NatS# w = N.NS w
pattern NatJ# :: B.BigNat -> Natural
pattern NatJ# b <- N.NB (B.BN# -> b)
where
NatJ# b = N.NB (B.unBigNat b)
int2Word :: Int -> Word
int2Word (I# i) = W# (int2Word# i)
word2Int :: Word -> Int
word2Int (W# w) = I# (word2Int# w)
mkNatural :: [Word] -> Natural
mkNatural = N.naturalFromWordList
isValidNatural :: Natural -> Bool
isValidNatural = N.naturalCheck
plusNatural :: Natural -> Natural -> Natural
plusNatural = N.naturalAdd
minusNatural :: Natural -> Natural -> Natural
minusNatural = N.naturalSubThrow
minusNaturalMaybe :: Natural -> Natural -> Maybe Natural
minusNaturalMaybe x y = case N.naturalSub x y of
(# (# #) | #) -> Nothing
(# | n #) -> Just n
timesNatural :: Natural -> Natural -> Natural
timesNatural = N.naturalMul
negateNatural :: Natural -> Natural
negateNatural = N.naturalNegate
signumNatural :: Natural -> Natural
signumNatural = N.naturalSignum
quotRemNatural :: Natural -> Natural -> (Natural, Natural)
quotRemNatural = N.naturalQuotRem
remNatural :: Natural -> Natural -> Natural
remNatural = N.naturalRem
quotNatural :: Natural -> Natural -> Natural
quotNatural = N.naturalQuot
gcdNatural :: Natural -> Natural -> Natural
gcdNatural = N.naturalGcd
lcmNatural :: Natural -> Natural -> Natural
lcmNatural = N.naturalLcm
andNatural :: Natural -> Natural -> Natural
andNatural = N.naturalAnd
orNatural :: Natural -> Natural -> Natural
orNatural = N.naturalOr
xorNatural :: Natural -> Natural -> Natural
xorNatural = N.naturalXor
bitNatural :: Int# -> Natural
bitNatural i = N.naturalBit# (int2Word# i)
testBitNatural :: Natural -> Int -> Bool
testBitNatural n i = N.naturalTestBit n (int2Word i)
popCountNatural :: Natural -> Int
popCountNatural n = word2Int (N.naturalPopCount n)
shiftLNatural :: Natural -> Int -> Natural
shiftLNatural n i = N.naturalShiftL n (int2Word i)
shiftRNatural :: Natural -> Int -> Natural
shiftRNatural n i = N.naturalShiftR n (int2Word i)
naturalToInteger :: Natural -> Integer
naturalToInteger = I.integerFromNatural
naturalToWord :: Natural -> Word
naturalToWord = N.naturalToWord
naturalFromInteger :: Integer -> Natural
naturalFromInteger = I.integerToNatural
wordToNatural :: Word -> Natural
wordToNatural = N.naturalFromWord
naturalToWordMaybe :: Natural -> Maybe Word
naturalToWordMaybe n = case N.naturalToWordMaybe# n of
(# | w #) -> Just (W# w)
(# (# #) | #) -> Nothing
wordToNatural# :: Word -> Natural
wordToNatural# = N.naturalFromWord
powModNatural :: Natural -> Natural -> Natural -> Natural
powModNatural = N.naturalPowMod