Created
November 12, 2016 17:11
-
-
Save mietek/f71cb0189c53ed012bd5b3ee16a1efa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE KindSignatures #-} | |
data Nat :: * where | |
Zero :: Nat | |
Suc :: Nat -> Nat | |
instance Eq Nat where | |
Zero == Zero = True | |
Suc n == Suc n' = n == n' | |
_ == _ = False | |
instance Ord Nat where | |
Zero <= _ = True | |
Suc n <= Suc n' = n <= n' | |
_ <= _ = False | |
instance Enum Nat where | |
fromEnum Zero = 0 | |
fromEnum (Suc n) = fromEnum n + 1 | |
toEnum n | n == 0 = Zero | |
| n > 0 = Suc (toEnum (n - 1)) | |
| otherwise = error ("Cannot cast " ++ show n ++ " to Nat") | |
instance Show Nat where | |
show = show . fromEnum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment