Last active
December 25, 2015 07:39
-
-
Save mlen/6941306 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
import Natural | |
main = putStrLn . show $ x > 100 | |
where x :: Natural | |
x = 1 + x |
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
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