-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNat1.hs
57 lines (41 loc) · 1.18 KB
/
Nat1.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
module Nat1 (Nat (..)) where
import GHC.Real (Ratio ((:%)))
data Nat = Z | S Nat deriving Show
instance Eq Nat where
Z == Z = True
S n1 == S n2 = n1 == n2
_ == _ = False
instance Ord Nat where
Z <= _ = True
S n1 <= S n2 = n1 <= n2
_ <= Z = False
instance Num Nat where
Z + n = n
S n1 + n2 = n1 + S n2
Z * _ = Z
S n1 * n2 = n2 + n1 * n2
abs n = n
signum Z = Z
signum _ = S Z
fromInteger 0 = Z
fromInteger n
| n > 0 = S $ fromInteger $ n - 1
| otherwise = error "fromInteger (negative integer) :: Nat"
n - Z = n
S n1 - S n2 = n1 - n2
Z - _ = error "Z - S n"
instance Real Nat where
toRational = (GHC.Real.:% 1) . toInteger
instance Enum Nat where
toEnum = fromInteger . toEnum
fromEnum = fromEnum . toInteger
instance Integral Nat where
toInteger Z = 0
toInteger (S n) = 1 + toInteger n
a `quotRem` b
| a >= b =
let (quot', rem') = (a - b) `quotRem` b
in (quot' + 1, rem')
| otherwise = (Z, a)
divMod = quotRem
-- ‘even’ and ‘gcd’ defined in ‘GHC.Real’ now work