Peano series

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
module Data.Peano where

-- oh, yeah, I went there! ... I so love me my peano series!

data Peano = Z | S Peano
   deriving Eq

instance Show Peano where
   show p = "P" ++ show (toInt p)

instance Ord Peano where
   compare Z Z = EQ
   compare (S n) Z = GT
   compare Z (S n) = LT
   compare (S n1) (S n2) = compare n1 n2

fromInt :: Int -> Peano      -- we actually want Int, not Integer
fromInt n | n <= 0 = Z
          | otherwise = S $ fromInt $ pred n

toInt :: Peano -> Int
toInt Z = 0
toInt (S n) = succ $ toInt n

instance Num Peano where
   fromInteger = fromInt . fromInteger
   abs p = p
   signum Z = Z
   signum (S _) = S Z
   p + Z = p
   Z + p = p
   (S n) + (S m) = S (S (n + m))
   Z * _ = Z
   _ * Z = Z
   (S n) * (S m) = sum (replicate (toInt $ S n) (S m))
9:13: Warning: Use :
Found:
"P" ++ show (toInt p)
Why not:
'P' : show (toInt p)