commit 441d32de3a512c9550c31d5f3f7b156bb9d8c64c
parent f4eb4c71824a5d2ac0d96385591fe1afe60c9860
Author: Robert Russell <robert@rr3.xyz>
Date: Sat, 26 Jul 2025 17:22:14 -0700
Fix Num Nat instance
Diffstat:
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/src/Naturals/Nat.hs b/src/Naturals/Nat.hs
@@ -4,8 +4,28 @@ module Naturals.Nat (
FromNat (..),
) where
+import Control.Exception
+
-- | Word-sized natural numbers.
-newtype Nat = Nat Word deriving newtype (Show, Eq, Ord, Enum, Bounded, Num, Real, Integral)
+newtype Nat = Nat Word deriving newtype (Show, Eq, Ord, Enum, Bounded, Real, Integral)
+
+instance Num Nat where
+ Nat x + Nat y = Nat (x + y)
+
+ Nat x - Nat y
+ | x >= y = Nat (x - y)
+ | otherwise = throw Underflow
+
+ Nat x * Nat y = Nat (x * y)
+
+ abs = id
+
+ signum (Nat 0) = Nat 0
+ signum _ = Nat 1
+
+ fromInteger n
+ | n >= 0 = Nat (fromInteger n)
+ | otherwise = throw Underflow
-- | Types supporting conversion to @Nat@.
--