<div dir="auto">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:<div dir="auto"><br></div><div dir="auto">-- This function turns an Integer n into a pair of</div><div dir="auto">-- (fromInteger (abs n), fromInteger (signum n)).</div><div dir="auto">integerToNaturalSign :: Integer -> (Natural, Int)</div><div dir="auto">#if 0</div><div dir="auto">-- This is the reference version, except for the fact that numeric literals don't exist yet</div><div dir="auto">-- when GHC.Natural is being defined, and fromInteger isn't actually total.</div><div dir="auto">integerToNaturalSign n = case compare n 0 of</div><div dir="auto">  LT -> (fromInteger (negate n), -1)</div><div dir="auto">  EQ -> (0, 0)</div><div dir="auto">  GT -> (fromInteger n, 1)</div><div dir="auto">#elif !defined(MIN_VERSION_integer_gmp)</div><div dir="auto">-- This is like the reference version, except using the newtype wrapper and MagicHash.</div><div dir="auto">integerToNaturalSign n = case compare n (intToInteger# 0#) of</div><div dir="auto">  LT -> (Natural (negateInteger n), I# (-1#))</div><div dir="auto">  EQ -> (Natural n, I# 0#)</div><div dir="auto">  GT -> (Natural n, I# 1#)</div><div dir="auto">#else</div><div dir="auto">-- We take advantage of the GMP functions and constructors.</div><div dir="auto">integerToNaturalSign (Jn# bn) = (bigNatToNatural bn, I# (-1#))</div><div dir="auto">integerToNaturalSign (S# i#) = case isTrue# (i# <# 0#) of</div><div dir="auto">  True -> (NatS# (int2Word# (negateInt# i#)), I# (-1#))</div><div dir="auto">  _    -> (NatS# (int2Word# i#), I# (i# ># 0#))</div><div dir="auto">integerToNaturalSign (Jp# bn) = (bigNatToNatural bn, I# 1#)</div><div dir="auto">#endif</div><div dir="auto"></div></div>