Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use type families for comparison of numbers

  • Loading branch information...
commit 017cfca1902a66b1c0a5e119fff4c7325ec1fe53 1 parent 3aa4ad3
@Shimuuar authored
Showing with 12 additions and 13 deletions.
  1. +2 −3 Types/Number/Classes.hs
  2. +10 −10 Types/Number/Nat.hs
View
5 Types/Number/Classes.hs
@@ -62,11 +62,10 @@ class TypeInt n where
-- Comparison
----------------------------------------------------------------
--- | Type class for comparing two numbers. It's expected that for any
+-- | Type family for comparing two numbers. It's expected that for any
-- two valid 'n' and 'm' 'Compare n m' is equal to IsLess when 'n<m', IsEqual
-- when 'n=m' and IsGreater when 'n>m'.
-class CompareN n m where
- type Compare n m :: *
+type family Compare n m :: *
compareN :: CompareN n m => n -> m -> Compare n m
compareN _ _ = undefined
View
20 Types/Number/Nat.hs
@@ -158,16 +158,16 @@ type instance Join a IsLesser = IsLesser
type instance Join a IsGreater = IsGreater
-- Instances for comparison
-instance CompareN Z Z where type Compare Z Z = IsEqual
-instance Nat (O n) => CompareN (O n) Z where type Compare (O n) Z = IsGreater
-instance Nat (I n) => CompareN (I n) Z where type Compare (I n) Z = IsGreater
-instance Nat (O n) => CompareN Z (O n) where type Compare Z (O n) = IsLesser
-instance Nat (I n) => CompareN Z (I n) where type Compare Z (I n) = IsLesser
-
-instance (Nat (O n), Nat (O m)) => CompareN (O n) (O m) where type Compare (O n) (O m) = Compare n m
-instance (Nat (O n), Nat (I m)) => CompareN (O n) (I m) where type Compare (O n) (I m) = Join IsLesser (Compare n m)
-instance (Nat (I n), Nat (O m)) => CompareN (I n) (O m) where type Compare (I n) (O m) = Join IsGreater (Compare n m)
-instance (Nat (I n), Nat (I m)) => CompareN (I n) (I m) where type Compare (I n) (I m) = Compare n m
+type instance Compare Z Z = IsEqual
+type instance Compare (O n) Z = IsGreater
+type instance Compare (I n) Z = IsGreater
+type instance Compare Z (O n) = IsLesser
+type instance Compare Z (I n) = IsLesser
+
+type instance Compare (O n) (O m) = Compare n m
+type instance Compare (O n) (I m) = Join IsLesser (Compare n m)
+type instance Compare (I n) (O m) = Join IsGreater (Compare n m)
+type instance Compare (I n) (I m) = Compare n m
----------------------------------------------------------------
-- Positive and Non-zero numbers
Please sign in to comment.
Something went wrong with that request. Please try again.