summaryrefslogtreecommitdiff
path: root/src/Numeric/InfInt.hs
blob: 871e5eb3ac5cd0ade48a65d2726e010b7d9b47b7 (plain)
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
60
{-|
Module      : Numeric.InfInt
Copyright   : (c) UU, 2019
License     : MIT
Maintainer  : Tom Smeding
Stability   : experimental
Portability : POSIX, macOS, Windows
-}
module Numeric.InfInt where


-- | The integers with ±∞ added. This is not a full ring ('∞ + -∞' is
-- undefined, for instance), but it works well enough.
data InfInt = MInfinity | Finite Int | Infinity
  deriving (Show, Eq, Ord)

instance Num InfInt where
    Infinity  + MInfinity = undefined
    Infinity  + _         = Infinity
    MInfinity + Infinity  = undefined
    MInfinity + _         = MInfinity
    Finite n  + Finite m  = Finite (n + m)
    Finite _  + Infinity  = Infinity
    Finite _  + MInfinity = MInfinity

    Finite n  * Finite m  = Finite (n * m)
    Finite 0  * _         = undefined
    Finite n  * Infinity  = if n < 0 then MInfinity else Infinity
    Finite n  * MInfinity = if n < 0 then Infinity else MInfinity
    Infinity  * Finite m  = Finite m * Infinity
    Infinity  * Infinity  = Infinity
    Infinity  * MInfinity = MInfinity
    MInfinity * Finite m  = Finite m * MInfinity
    MInfinity * Infinity  = MInfinity
    MInfinity * MInfinity = Infinity

    abs (Finite n) = Finite (abs n)
    abs _          = Infinity

    signum (Finite n) = Finite (signum n)
    signum MInfinity  = (-1)
    signum Infinity   = 1

    fromInteger n = Finite (fromInteger n)

    negate (Finite n) = Finite (-n)
    negate Infinity = MInfinity
    negate MInfinity = Infinity


-- | If the number is finite, return the finite component.
toFinite :: InfInt -> Maybe Int
toFinite (Finite n) = Just n
toFinite Infinity   = Nothing
toFinite MInfinity  = Nothing

-- | @isFinite = isJust . toFinite@
isFinite :: InfInt -> Bool
isFinite (Finite _) = True
isFinite _          = False