1
1
module Data.Int.AtLeast
2
- ( IntAL -- Constructor not exported. Use fromInt or fromInt'
2
+ ( IntAL -- Constructor not exported. Use fromInt, fromInt' or clamp
3
3
, fromInt
4
4
, fromInt'
5
+ , clamp
5
6
, toInt
6
7
, toNumber
7
- , (%+%), plus
8
- , (%-%), minus
9
- , (%*%), times
10
- , (%/%), quotient
8
+ , extent
9
+ , (%+%)
10
+ , plus
11
+ , (%-%)
12
+ , minus
13
+ , (%*%)
14
+ , times
15
+ , (%/%)
16
+ , quotient
11
17
, modulo
12
18
, remainder
13
19
, divide
@@ -54,34 +60,47 @@ instance Reflectable min Int => Enum (IntAL min) where
54
60
if n > reflectType (Proxy :: _ min ) then Just (IntAL $ n - 1 ) else Nothing
55
61
56
62
instance Reflectable min Int => Show (IntAL min ) where
57
- show (IntAL i) = " IntAL " <> show (reflectType (Proxy :: _ min )) <> " " <> show i
63
+ show (IntAL i) = " IntAL " <> show (reflectType (Proxy :: _ min )) <> " " <>
64
+ show i
58
65
59
66
instance Reflectable min Int => Arbitrary (IntAL min ) where
60
67
arbitrary =
61
68
let
62
69
n = reflectType (Proxy :: _ min )
63
70
in
64
- IntAL <$> oneOf (NEA .cons'
65
- ( elements $ NEA .singleton n )
66
- [ chooseInt (n + 1 ) (n + 10 )
67
- , chooseInt (n + 11 ) (n + 100 )
68
- , chooseInt (n + 101 ) (n + 10000 )
69
- ])
71
+ IntAL <$> oneOf
72
+ ( NEA .cons'
73
+ (elements $ NEA .singleton n)
74
+ [ chooseInt (n + 1 ) (n + 10 )
75
+ , chooseInt (n + 11 ) (n + 100 )
76
+ , chooseInt (n + 101 ) (n + 10000 )
77
+ ]
78
+ )
70
79
71
80
-- | Convert an `Int` to an `IntAL min`, returning `Nothing` if the runtime
72
81
-- | value of the input is smaller than `min`
73
82
fromInt :: ∀ (min :: Int ). Reflectable min Int => Int -> Maybe (IntAL min )
74
- fromInt i = if i >= reflectType (Proxy :: _ min ) then Just (IntAL i) else Nothing
83
+ fromInt i =
84
+ if i >= reflectType (Proxy :: _ min ) then Just (IntAL i) else Nothing
75
85
76
86
-- | A partial function, converting an `Int` to an `IntAL min`. Crashes at
77
87
-- | runtime if the runtime value of the input is smaller than `min`
78
88
fromInt'
79
89
:: ∀ (min :: Int ). Reflectable min Int => Partial => Int -> IntAL min
80
90
fromInt' i =
81
- if i >= reflectType (Proxy :: _ min ) then IntAL i
82
- else
83
- crashWith $ " Cannot convert Int " <> show i <> " to IntAL " <>
84
- show (reflectType (Proxy :: _ min ))
91
+ let
92
+ minInt = reflectType (Proxy :: _ min )
93
+ in
94
+ if i >= minInt then IntAL i
95
+ else crashWith $
96
+ " Cannot convert Int " <> show i <> " to IntAL " <> show minInt
97
+
98
+ clamp :: ∀ (min :: Int ). Reflectable min Int => Int -> IntAL min
99
+ clamp i =
100
+ let
101
+ minInt = reflectType (Proxy :: _ min )
102
+ in
103
+ if i >= minInt then IntAL i else IntAL minInt
85
104
86
105
-- | Convert an `IntAL` to an `Int`
87
106
toInt :: ∀ (min :: Int ). IntAL min -> Int
@@ -91,6 +110,11 @@ toInt (IntAL i) = i
91
110
toNumber :: ∀ (min :: Int ). IntAL min -> Number
92
111
toNumber (IntAL i) = Int .toNumber i
93
112
113
+ -- | Measure the distance between the runtime value of an `IntAL min` and the
114
+ -- | type-level minimum value `min`, as an IntAL 0
115
+ extent :: ∀ (min :: Int ). Reflectable min Int => IntAL min -> IntAL 0
116
+ extent (IntAL i) = IntAL $ reflectType (Proxy :: _ min ) - i
117
+
94
118
-- | Add two IntAL values with possibly different type-level minimums
95
119
plus
96
120
:: ∀ (min_i :: Int ) (min_j :: Int ) (min_sum :: Int )
0 commit comments