-
Notifications
You must be signed in to change notification settings - Fork 0
/
natural.hs
59 lines (42 loc) · 1.35 KB
/
natural.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Natural where
fstAdd :: Integral a => (a, t) -> a -> (a, t)
fstAdd (a, b) x = (a + x, b)
data Natural = Zero | Succ Natural
deriving (Eq, Ord)
instance Enum Natural where
pred Zero = undefined
pred (Succ x) = x
succ x = Succ x
toEnum 0 = Zero
toEnum x = Succ (toEnum (x - 1))
fromEnum Zero = 0
fromEnum (Succ x) = fromEnum x + 1
instance Num Natural where
(+) x Zero = x
(+) x (Succ y) = Succ (x + y)
(-) Zero (Succ x) = undefined
(-) x Zero = x
(-) (Succ x) (Succ y) = x - y
(*) x Zero = Zero
(*) x (Succ y) = x * y + x
abs x = x
signum Zero = Zero
signum (Succ x) = Succ Zero
fromInteger 0 = Zero
fromInteger x = Succ (fromInteger (x - 1))
instance Real Natural where
toRational Zero = 0
toRational (Succ x) = toRational x + 1
instance Integral Natural where
toInteger Zero = 0
toInteger (Succ x) = toInteger x + 1
quotRem x Zero = undefined
quotRem Zero x = (Zero, Zero)
quotRem x (Succ Zero) = (x, Zero)
quotRem (Succ Zero) x = (Zero, Succ Zero)
quotRem x y = if x >= y then fstAdd (quotRem (x - y) y) 1 else (Zero, x)
instance Show Natural where
--showsPrec p n = [(fromEnum i, s') | (i, s') <- showsPrec p s]
show x = show $ toInteger x
instance Read Natural where
readsPrec p s = [(fromInteger i, s') | (i, s') <- readsPrec p s]