Skip to content

Instantly share code, notes, and snippets.

@mietek
Created November 12, 2016 17:11
Show Gist options
  • Save mietek/f71cb0189c53ed012bd5b3ee16a1efa7 to your computer and use it in GitHub Desktop.
Save mietek/f71cb0189c53ed012bd5b3ee16a1efa7 to your computer and use it in GitHub Desktop.
{-# 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