Export functions on Natural in Numeric.Natural
Zemyla
zemyla at gmail.com
Sun Mar 22 18:40:07 UTC 2020
Functions in GHC.Natural like naturalToWordMaybe and minusNaturalMaybe are
extremely useful, especially when trying to write provably total functions,
and I wish they were available in a Safe module like Numeric.Natural. I
also want a total function that does the following:
-- This function turns an Integer n into a pair of
-- (fromInteger (abs n), fromInteger (signum n)).
integerToNaturalSign :: Integer -> (Natural, Int)
#if 0
-- This is the reference version, except for the fact that numeric literals
don't exist yet
-- when GHC.Natural is being defined, and fromInteger isn't actually total.
integerToNaturalSign n = case compare n 0 of
LT -> (fromInteger (negate n), -1)
EQ -> (0, 0)
GT -> (fromInteger n, 1)
#elif !defined(MIN_VERSION_integer_gmp)
-- This is like the reference version, except using the newtype wrapper and
MagicHash.
integerToNaturalSign n = case compare n (intToInteger# 0#) of
LT -> (Natural (negateInteger n), I# (-1#))
EQ -> (Natural n, I# 0#)
GT -> (Natural n, I# 1#)
#else
-- We take advantage of the GMP functions and constructors.
integerToNaturalSign (Jn# bn) = (bigNatToNatural bn, I# (-1#))
integerToNaturalSign (S# i#) = case isTrue# (i# <# 0#) of
True -> (NatS# (int2Word# (negateInt# i#)), I# (-1#))
_ -> (NatS# (int2Word# i#), I# (i# ># 0#))
integerToNaturalSign (Jp# bn) = (bigNatToNatural bn, I# 1#)
#endif
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20200322/51c3288d/attachment.html>
More information about the Libraries
mailing list