Skip to content

Instantly share code, notes, and snippets.

@mlen
Last active December 25, 2015 07:39
Show Gist options
  • Save mlen/6941306 to your computer and use it in GitHub Desktop.
Save mlen/6941306 to your computer and use it in GitHub Desktop.
import Natural
main = putStrLn . show $ x > 100
where x :: Natural
x = 1 + x
module Natural where
import Data.Function (fix)
data Natural = Zero | Succ Natural
instance Show Natural where
show = show . natToInt
where natToInt Zero = 0
natToInt (Succ n) = 1 + natToInt n
instance Num Natural where
Zero + n = n
Succ n + m = Succ (n + m)
Zero * n = Zero
Succ n * m = n * m + m
signum Zero = Zero
signum n = Succ Zero
abs n = n
fromInteger 0 = Zero
fromInteger n | n > 0 = Succ . fromInteger $ n - 1
| otherwise = error "Naturals must be greater or equal than 0"
instance Eq Natural where
Zero == Zero = True
Succ n == Succ m = n == m
_ == _ = False
instance Ord Natural where
Zero `compare` Zero = EQ
Zero `compare` Succ _ = LT
Succ _ `compare` Zero = GT
Succ n `compare` Succ m = n `compare` m
infinity :: Natural
infinity = fix Succ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment