File: Subtractive.hs

package info (click to toggle)
haskell-basement 0.0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: haskell: 11,336; ansic: 63; makefile: 5
file content (181 lines) | stat: -rw-r--r-- 5,534 bytes parent folder | download | duplicates (4)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{-# LANGUAGE CPP, UndecidableInstances #-}
module Basement.Numerical.Subtractive
    ( Subtractive(..)
    ) where

import           Basement.Compat.Base
import           Basement.Compat.C.Types
import           Basement.Compat.Natural
import           Basement.IntegralConv
import           Basement.Bounded
import           Basement.Nat
import           Basement.Types.Word128 (Word128)
import           Basement.Types.Word256 (Word256)
import qualified Basement.Types.Word128 as Word128
import qualified Basement.Types.Word256 as Word256
import qualified Prelude

-- | Represent class of things that can be subtracted.
--
--
-- Note that the result is not necessary of the same type
-- as the operand depending on the actual type.
--
-- For example:
--
-- > (-) :: Int -> Int -> Int
-- > (-) :: DateTime -> DateTime -> Seconds
-- > (-) :: Ptr a -> Ptr a -> PtrDiff
-- > (-) :: Natural -> Natural -> Maybe Natural
class Subtractive a where
    type Difference a
    (-) :: a -> a -> Difference a

infixl 6 -

instance Subtractive Integer where
    type Difference Integer = Integer
    (-) = (Prelude.-)
instance Subtractive Int where
    type Difference Int = Int
    (-) = (Prelude.-)
instance Subtractive Int8 where
    type Difference Int8 = Int8
    (-) = (Prelude.-)
instance Subtractive Int16 where
    type Difference Int16 = Int16
    (-) = (Prelude.-)
instance Subtractive Int32 where
    type Difference Int32 = Int32
    (-) = (Prelude.-)
instance Subtractive Int64 where
    type Difference Int64 = Int64
    (-) = (Prelude.-)
instance Subtractive Natural where
    type Difference Natural = Maybe Natural
    (-) a b
        | b > a     = Nothing
        | otherwise = Just (a Prelude.- b)
instance Subtractive Word where
    type Difference Word = Word
    (-) = (Prelude.-)
instance Subtractive Word8 where
    type Difference Word8 = Word8
    (-) = (Prelude.-)
instance Subtractive Word16 where
    type Difference Word16 = Word16
    (-) = (Prelude.-)
instance Subtractive Word32 where
    type Difference Word32 = Word32
    (-) = (Prelude.-)
instance Subtractive Word64 where
    type Difference Word64 = Word64
    (-) = (Prelude.-)
instance Subtractive Word128 where
    type Difference Word128 = Word128
    (-) = (Word128.-)
instance Subtractive Word256 where
    type Difference Word256 = Word256
    (-) = (Word256.-)

instance Subtractive Prelude.Float where
    type Difference Prelude.Float = Prelude.Float
    (-) = (Prelude.-)
instance Subtractive Prelude.Double where
    type Difference Prelude.Double = Prelude.Double
    (-) = (Prelude.-)

instance Subtractive Prelude.Char where
    type Difference Prelude.Char = Prelude.Int
    (-) a b = (Prelude.-) (charToInt a) (charToInt b)
instance (KnownNat n, NatWithinBound Word64 n) => Subtractive (Zn64 n) where
    type Difference (Zn64 n) = Zn64 n
    (-) a b = (Prelude.-) a b
instance KnownNat n => Subtractive (Zn n) where
    type Difference (Zn n) = Zn n
    (-) a b = (Prelude.-) a b

instance Subtractive CChar where
    type Difference CChar = CChar
    (-) = (Prelude.-)
instance Subtractive CSChar where
    type Difference CSChar = CSChar
    (-) = (Prelude.-)
instance Subtractive CUChar where
    type Difference CUChar = CUChar
    (-) = (Prelude.-)
instance Subtractive CShort where
    type Difference CShort = CShort
    (-) = (Prelude.-)
instance Subtractive CUShort where
    type Difference CUShort = CUShort
    (-) = (Prelude.-)
instance Subtractive CInt where
    type Difference CInt = CInt
    (-) = (Prelude.-)
instance Subtractive CUInt where
    type Difference CUInt = CUInt
    (-) = (Prelude.-)
instance Subtractive CLong where
    type Difference CLong = CLong
    (-) = (Prelude.-)
instance Subtractive CULong where
    type Difference CULong = CULong
    (-) = (Prelude.-)
instance Subtractive CPtrdiff where
    type Difference CPtrdiff = CPtrdiff
    (-) = (Prelude.-)
instance Subtractive CSize where
    type Difference CSize = CSize
    (-) = (Prelude.-)
instance Subtractive CWchar where
    type Difference CWchar = CWchar
    (-) = (Prelude.-)
instance Subtractive CSigAtomic where
    type Difference CSigAtomic = CSigAtomic
    (-) = (Prelude.-)
instance Subtractive CLLong where
    type Difference CLLong = CLLong
    (-) = (Prelude.-)
instance Subtractive CULLong where
    type Difference CULLong = CULLong
    (-) = (Prelude.-)
#if MIN_VERSION_base(4,10,0)
instance Subtractive CBool where
    type Difference CBool = CBool
    (-) = (Prelude.-)
#endif
instance Subtractive CIntPtr where
    type Difference CIntPtr = CIntPtr
    (-) = (Prelude.-)
instance Subtractive CUIntPtr where
    type Difference CUIntPtr = CUIntPtr
    (-) = (Prelude.-)
instance Subtractive CIntMax where
    type Difference CIntMax = CIntMax
    (-) = (Prelude.-)
instance Subtractive CUIntMax where
    type Difference CUIntMax = CUIntMax
    (-) = (Prelude.-)
instance Subtractive CClock where
    type Difference CClock = CClock
    (-) = (Prelude.-)
instance Subtractive CTime where
    type Difference CTime = CTime
    (-) = (Prelude.-)
instance Subtractive CUSeconds where
    type Difference CUSeconds = CUSeconds
    (-) = (Prelude.-)
instance Subtractive CSUSeconds where
    type Difference CSUSeconds = CSUSeconds
    (-) = (Prelude.-)
instance Subtractive COff where
    type Difference COff = COff
    (-) = (Prelude.-)

instance Subtractive CFloat where
    type Difference CFloat = CFloat
    (-) = (Prelude.-)
instance Subtractive CDouble where
    type Difference CDouble = CDouble
    (-) = (Prelude.-)