ic-mops 0.8.3 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mops/base@0.7.4/LICENSE +208 -0
- package/.mops/base@0.7.4/README.md +64 -0
- package/.mops/base@0.7.4/mops.toml +5 -0
- package/.mops/base@0.7.4/src/Array.mo +686 -0
- package/.mops/base@0.7.4/src/AssocList.mo +203 -0
- package/.mops/base@0.7.4/src/Blob.mo +55 -0
- package/.mops/base@0.7.4/src/Bool.mo +44 -0
- package/.mops/base@0.7.4/src/Buffer.mo +1937 -0
- package/.mops/base@0.7.4/src/CertifiedData.mo +29 -0
- package/.mops/base@0.7.4/src/Char.mo +67 -0
- package/.mops/base@0.7.4/src/Debug.mo +15 -0
- package/.mops/base@0.7.4/src/Deque.mo +75 -0
- package/.mops/base@0.7.4/src/Error.mo +41 -0
- package/.mops/base@0.7.4/src/ExperimentalCycles.mo +51 -0
- package/.mops/base@0.7.4/src/ExperimentalInternetComputer.mo +36 -0
- package/.mops/base@0.7.4/src/ExperimentalStableMemory.mo +121 -0
- package/.mops/base@0.7.4/src/Float.mo +150 -0
- package/.mops/base@0.7.4/src/Func.mo +38 -0
- package/.mops/base@0.7.4/src/Hash.mo +83 -0
- package/.mops/base@0.7.4/src/HashMap.mo +229 -0
- package/.mops/base@0.7.4/src/Heap.mo +113 -0
- package/.mops/base@0.7.4/src/Int.mo +150 -0
- package/.mops/base@0.7.4/src/Int16.mo +159 -0
- package/.mops/base@0.7.4/src/Int32.mo +160 -0
- package/.mops/base@0.7.4/src/Int64.mo +161 -0
- package/.mops/base@0.7.4/src/Int8.mo +160 -0
- package/.mops/base@0.7.4/src/Iter.mo +220 -0
- package/.mops/base@0.7.4/src/IterType.mo +7 -0
- package/.mops/base@0.7.4/src/List.mo +433 -0
- package/.mops/base@0.7.4/src/Nat.mo +75 -0
- package/.mops/base@0.7.4/src/Nat16.mo +146 -0
- package/.mops/base@0.7.4/src/Nat32.mo +146 -0
- package/.mops/base@0.7.4/src/Nat64.mo +146 -0
- package/.mops/base@0.7.4/src/Nat8.mo +146 -0
- package/.mops/base@0.7.4/src/None.mo +19 -0
- package/.mops/base@0.7.4/src/Option.mo +160 -0
- package/.mops/base@0.7.4/src/Order.mo +46 -0
- package/.mops/base@0.7.4/src/Prelude.mo +33 -0
- package/.mops/base@0.7.4/src/Principal.mo +58 -0
- package/.mops/base@0.7.4/src/RBTree.mo +218 -0
- package/.mops/base@0.7.4/src/Random.mo +188 -0
- package/.mops/base@0.7.4/src/Result.mo +210 -0
- package/.mops/base@0.7.4/src/Stack.mo +40 -0
- package/.mops/base@0.7.4/src/Text.mo +615 -0
- package/.mops/base@0.7.4/src/Time.mo +37 -0
- package/.mops/base@0.7.4/src/Trie.mo +1200 -0
- package/.mops/base@0.7.4/src/TrieMap.mo +180 -0
- package/.mops/base@0.7.4/src/TrieSet.mo +97 -0
- package/.mops/base@0.8.3/LICENSE +208 -0
- package/.mops/base@0.8.3/README.md +64 -0
- package/.mops/base@0.8.3/mops.toml +6 -0
- package/.mops/base@0.8.3/src/Array.mo +717 -0
- package/.mops/base@0.8.3/src/AssocList.mo +404 -0
- package/.mops/base@0.8.3/src/Blob.mo +212 -0
- package/.mops/base@0.8.3/src/Bool.mo +44 -0
- package/.mops/base@0.8.3/src/Buffer.mo +2660 -0
- package/.mops/base@0.8.3/src/CertifiedData.mo +53 -0
- package/.mops/base@0.8.3/src/Char.mo +65 -0
- package/.mops/base@0.8.3/src/Debug.mo +56 -0
- package/.mops/base@0.8.3/src/Deque.mo +243 -0
- package/.mops/base@0.8.3/src/Error.mo +68 -0
- package/.mops/base@0.8.3/src/ExperimentalCycles.mo +151 -0
- package/.mops/base@0.8.3/src/ExperimentalInternetComputer.mo +60 -0
- package/.mops/base@0.8.3/src/ExperimentalStableMemory.mo +348 -0
- package/.mops/base@0.8.3/src/Float.mo +843 -0
- package/.mops/base@0.8.3/src/Func.mo +46 -0
- package/.mops/base@0.8.3/src/Hash.mo +82 -0
- package/.mops/base@0.8.3/src/HashMap.mo +457 -0
- package/.mops/base@0.8.3/src/Heap.mo +233 -0
- package/.mops/base@0.8.3/src/Int.mo +365 -0
- package/.mops/base@0.8.3/src/Int16.mo +521 -0
- package/.mops/base@0.8.3/src/Int32.mo +522 -0
- package/.mops/base@0.8.3/src/Int64.mo +522 -0
- package/.mops/base@0.8.3/src/Int8.mo +522 -0
- package/.mops/base@0.8.3/src/Iter.mo +227 -0
- package/.mops/base@0.8.3/src/IterType.mo +7 -0
- package/.mops/base@0.8.3/src/List.mo +930 -0
- package/.mops/base@0.8.3/src/Nat.mo +305 -0
- package/.mops/base@0.8.3/src/Nat16.mo +144 -0
- package/.mops/base@0.8.3/src/Nat32.mo +144 -0
- package/.mops/base@0.8.3/src/Nat64.mo +144 -0
- package/.mops/base@0.8.3/src/Nat8.mo +144 -0
- package/.mops/base@0.8.3/src/None.mo +19 -0
- package/.mops/base@0.8.3/src/Option.mo +154 -0
- package/.mops/base@0.8.3/src/Order.mo +46 -0
- package/.mops/base@0.8.3/src/Prelude.mo +33 -0
- package/.mops/base@0.8.3/src/Principal.mo +249 -0
- package/.mops/base@0.8.3/src/RBTree.mo +681 -0
- package/.mops/base@0.8.3/src/Random.mo +270 -0
- package/.mops/base@0.8.3/src/Result.mo +209 -0
- package/.mops/base@0.8.3/src/Stack.mo +93 -0
- package/.mops/base@0.8.3/src/Text.mo +761 -0
- package/.mops/base@0.8.3/src/Time.mo +36 -0
- package/.mops/base@0.8.3/src/Timer.mo +62 -0
- package/.mops/base@0.8.3/src/Trie.mo +1603 -0
- package/.mops/base@0.8.3/src/TrieMap.mo +392 -0
- package/.mops/base@0.8.3/src/TrieSet.mo +148 -0
- package/mops.js +1 -1
- package/network.txt +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/// Natural numbers with infinite precision.
|
|
2
|
+
///
|
|
3
|
+
/// Most operations on natural numbers (e.g. addition) are available as built-in operators (e.g. `1 + 1`).
|
|
4
|
+
/// This module provides equivalent functions and `Text` conversion.
|
|
5
|
+
///
|
|
6
|
+
/// Import from the base library to use this module.
|
|
7
|
+
/// ```motoko name=import
|
|
8
|
+
/// import Nat "mo:base/Nat";
|
|
9
|
+
/// ```
|
|
10
|
+
|
|
11
|
+
import Int "Int";
|
|
12
|
+
import Order "Order";
|
|
13
|
+
import Prim "mo:⛔";
|
|
14
|
+
import Char "Char";
|
|
15
|
+
|
|
16
|
+
module {
|
|
17
|
+
|
|
18
|
+
/// Infinite precision natural numbers.
|
|
19
|
+
public type Nat = Prim.Types.Nat;
|
|
20
|
+
|
|
21
|
+
/// Converts a natural number to its textual representation. Textual
|
|
22
|
+
/// representation _do not_ contain underscores to represent commas.
|
|
23
|
+
///
|
|
24
|
+
/// Example:
|
|
25
|
+
/// ```motoko include=import
|
|
26
|
+
/// Nat.toText 1234 // => "1234"
|
|
27
|
+
/// ```
|
|
28
|
+
public func toText(n : Nat) : Text = Int.toText n;
|
|
29
|
+
|
|
30
|
+
/// Creates a natural number from its textual representation. Returns `null`
|
|
31
|
+
/// if the input is not a valid natural number.
|
|
32
|
+
///
|
|
33
|
+
/// Note: The textual representation _must not_ contain underscores.
|
|
34
|
+
///
|
|
35
|
+
/// Example:
|
|
36
|
+
/// ```motoko include=import
|
|
37
|
+
/// Nat.fromText "1234" // => ?1234
|
|
38
|
+
/// ```
|
|
39
|
+
public func fromText(text : Text) : ?Nat {
|
|
40
|
+
if (text == "") {
|
|
41
|
+
return null
|
|
42
|
+
};
|
|
43
|
+
var n = 0;
|
|
44
|
+
for (c in text.chars()) {
|
|
45
|
+
if (Char.isDigit(c)) {
|
|
46
|
+
let charAsNat = Prim.nat32ToNat(Prim.charToNat32(c) -% Prim.charToNat32('0'));
|
|
47
|
+
n := n * 10 + charAsNat
|
|
48
|
+
} else {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
?n
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/// Returns the minimum of `x` and `y`.
|
|
56
|
+
///
|
|
57
|
+
/// Example:
|
|
58
|
+
/// ```motoko include=import
|
|
59
|
+
/// Nat.min(1, 2) // => 1
|
|
60
|
+
/// ```
|
|
61
|
+
public func min(x : Nat, y : Nat) : Nat {
|
|
62
|
+
if (x < y) { x } else { y }
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/// Returns the maximum of `x` and `y`.
|
|
66
|
+
///
|
|
67
|
+
/// Example:
|
|
68
|
+
/// ```motoko include=import
|
|
69
|
+
/// Nat.max(1, 2) // => 2
|
|
70
|
+
/// ```
|
|
71
|
+
public func max(x : Nat, y : Nat) : Nat {
|
|
72
|
+
if (x < y) { y } else { x }
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/// Equality function for Nat types.
|
|
76
|
+
/// This is equivalent to `x == y`.
|
|
77
|
+
///
|
|
78
|
+
/// Example:
|
|
79
|
+
/// ```motoko include=import
|
|
80
|
+
/// ignore Nat.equal(1, 1); // => true
|
|
81
|
+
/// 1 == 1 // => true
|
|
82
|
+
/// ```
|
|
83
|
+
///
|
|
84
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
85
|
+
/// to the existing `==` operator) is so that you can use it as a function
|
|
86
|
+
/// value to pass to a higher order function. It is not possible to use `==`
|
|
87
|
+
/// as a function value at the moment.
|
|
88
|
+
///
|
|
89
|
+
/// Example:
|
|
90
|
+
/// ```motoko include=import
|
|
91
|
+
/// import Buffer "mo:base/Buffer";
|
|
92
|
+
///
|
|
93
|
+
/// let buffer1 = Buffer.Buffer<Nat>(3);
|
|
94
|
+
/// let buffer2 = Buffer.Buffer<Nat>(3);
|
|
95
|
+
/// Buffer.equal(buffer1, buffer2, Nat.equal) // => true
|
|
96
|
+
/// ```
|
|
97
|
+
public func equal(x : Nat, y : Nat) : Bool { x == y };
|
|
98
|
+
|
|
99
|
+
/// Inequality function for Nat types.
|
|
100
|
+
/// This is equivalent to `x != y`.
|
|
101
|
+
///
|
|
102
|
+
/// Example:
|
|
103
|
+
/// ```motoko include=import
|
|
104
|
+
/// ignore Nat.notEqual(1, 2); // => true
|
|
105
|
+
/// 1 != 2 // => true
|
|
106
|
+
/// ```
|
|
107
|
+
///
|
|
108
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
109
|
+
/// to the existing `!=` operator) is so that you can use it as a function
|
|
110
|
+
/// value to pass to a higher order function. It is not possible to use `!=`
|
|
111
|
+
/// as a function value at the moment.
|
|
112
|
+
public func notEqual(x : Nat, y : Nat) : Bool { x != y };
|
|
113
|
+
|
|
114
|
+
/// "Less than" function for Nat types.
|
|
115
|
+
/// This is equivalent to `x < y`.
|
|
116
|
+
///
|
|
117
|
+
/// Example:
|
|
118
|
+
/// ```motoko include=import
|
|
119
|
+
/// ignore Nat.less(1, 2); // => true
|
|
120
|
+
/// 1 < 2 // => true
|
|
121
|
+
/// ```
|
|
122
|
+
///
|
|
123
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
124
|
+
/// to the existing `<` operator) is so that you can use it as a function
|
|
125
|
+
/// value to pass to a higher order function. It is not possible to use `<`
|
|
126
|
+
/// as a function value at the moment.
|
|
127
|
+
public func less(x : Nat, y : Nat) : Bool { x < y };
|
|
128
|
+
|
|
129
|
+
/// "Less than or equal" function for Nat types.
|
|
130
|
+
/// This is equivalent to `x <= y`.
|
|
131
|
+
///
|
|
132
|
+
/// Example:
|
|
133
|
+
/// ```motoko include=import
|
|
134
|
+
/// ignore Nat.lessOrEqual(1, 2); // => true
|
|
135
|
+
/// 1 <= 2 // => true
|
|
136
|
+
/// ```
|
|
137
|
+
///
|
|
138
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
139
|
+
/// to the existing `<=` operator) is so that you can use it as a function
|
|
140
|
+
/// value to pass to a higher order function. It is not possible to use `<=`
|
|
141
|
+
/// as a function value at the moment.
|
|
142
|
+
public func lessOrEqual(x : Nat, y : Nat) : Bool { x <= y };
|
|
143
|
+
|
|
144
|
+
/// "Greater than" function for Nat types.
|
|
145
|
+
/// This is equivalent to `x > y`.
|
|
146
|
+
///
|
|
147
|
+
/// Example:
|
|
148
|
+
/// ```motoko include=import
|
|
149
|
+
/// ignore Nat.greater(2, 1); // => true
|
|
150
|
+
/// 2 > 1 // => true
|
|
151
|
+
/// ```
|
|
152
|
+
///
|
|
153
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
154
|
+
/// to the existing `>` operator) is so that you can use it as a function
|
|
155
|
+
/// value to pass to a higher order function. It is not possible to use `>`
|
|
156
|
+
/// as a function value at the moment.
|
|
157
|
+
public func greater(x : Nat, y : Nat) : Bool { x > y };
|
|
158
|
+
|
|
159
|
+
/// "Greater than or equal" function for Nat types.
|
|
160
|
+
/// This is equivalent to `x >= y`.
|
|
161
|
+
///
|
|
162
|
+
/// Example:
|
|
163
|
+
/// ```motoko include=import
|
|
164
|
+
/// ignore Nat.greaterOrEqual(2, 1); // => true
|
|
165
|
+
/// 2 >= 1 // => true
|
|
166
|
+
/// ```
|
|
167
|
+
///
|
|
168
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
169
|
+
/// to the existing `>=` operator) is so that you can use it as a function
|
|
170
|
+
/// value to pass to a higher order function. It is not possible to use `>=`
|
|
171
|
+
/// as a function value at the moment.
|
|
172
|
+
public func greaterOrEqual(x : Nat, y : Nat) : Bool { x >= y };
|
|
173
|
+
|
|
174
|
+
/// General purpose comparison function for `Nat`. Returns the `Order` (
|
|
175
|
+
/// either `#less`, `#equal`, or `#greater`) of comparing `x` with `y`.
|
|
176
|
+
///
|
|
177
|
+
/// Example:
|
|
178
|
+
/// ```motoko include=import
|
|
179
|
+
/// Nat.compare(2, 3) // => #less
|
|
180
|
+
/// ```
|
|
181
|
+
///
|
|
182
|
+
/// This function can be used as value for a high order function, such as a sort function.
|
|
183
|
+
///
|
|
184
|
+
/// Example:
|
|
185
|
+
/// ```motoko include=import
|
|
186
|
+
/// import Array "mo:base/Array";
|
|
187
|
+
/// Array.sort([2, 3, 1], Nat.compare) // => [1, 2, 3]
|
|
188
|
+
/// ```
|
|
189
|
+
public func compare(x : Nat, y : Nat) : { #less; #equal; #greater } {
|
|
190
|
+
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/// Returns the sum of `x` and `y`, `x + y`. This operator will never overflow
|
|
194
|
+
/// because `Nat` is infinite precision.
|
|
195
|
+
///
|
|
196
|
+
/// Example:
|
|
197
|
+
/// ```motoko include=import
|
|
198
|
+
/// ignore Nat.add(1, 2); // => 3
|
|
199
|
+
/// 1 + 2 // => 3
|
|
200
|
+
/// ```
|
|
201
|
+
///
|
|
202
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
203
|
+
/// to the existing `+` operator) is so that you can use it as a function
|
|
204
|
+
/// value to pass to a higher order function. It is not possible to use `+`
|
|
205
|
+
/// as a function value at the moment.
|
|
206
|
+
///
|
|
207
|
+
/// Example:
|
|
208
|
+
/// ```motoko include=import
|
|
209
|
+
/// import Array "mo:base/Array";
|
|
210
|
+
/// Array.foldLeft([2, 3, 1], 0, Nat.add) // => 6
|
|
211
|
+
/// ```
|
|
212
|
+
public func add(x : Nat, y : Nat) : Nat { x + y };
|
|
213
|
+
|
|
214
|
+
/// Returns the difference of `x` and `y`, `x - y`.
|
|
215
|
+
/// Traps on underflow below `0`.
|
|
216
|
+
///
|
|
217
|
+
/// Example:
|
|
218
|
+
/// ```motoko include=import
|
|
219
|
+
/// ignore Nat.sub(2, 1); // => 1
|
|
220
|
+
/// // Add a type annotation to avoid a warning about the subtraction
|
|
221
|
+
/// 2 - 1 : Nat // => 1
|
|
222
|
+
/// ```
|
|
223
|
+
///
|
|
224
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
225
|
+
/// to the existing `-` operator) is so that you can use it as a function
|
|
226
|
+
/// value to pass to a higher order function. It is not possible to use `-`
|
|
227
|
+
/// as a function value at the moment.
|
|
228
|
+
///
|
|
229
|
+
/// Example:
|
|
230
|
+
/// ```motoko include=import
|
|
231
|
+
/// import Array "mo:base/Array";
|
|
232
|
+
/// Array.foldLeft([2, 3, 1], 10, Nat.sub) // => 4
|
|
233
|
+
/// ```
|
|
234
|
+
public func sub(x : Nat, y : Nat) : Nat { x - y };
|
|
235
|
+
|
|
236
|
+
/// Returns the product of `x` and `y`, `x * y`. This operator will never
|
|
237
|
+
/// overflow because `Nat` is infinite precision.
|
|
238
|
+
///
|
|
239
|
+
/// Example:
|
|
240
|
+
/// ```motoko include=import
|
|
241
|
+
/// ignore Nat.mul(2, 3); // => 6
|
|
242
|
+
/// 2 * 3 // => 6
|
|
243
|
+
/// ```
|
|
244
|
+
///
|
|
245
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
246
|
+
/// to the existing `*` operator) is so that you can use it as a function
|
|
247
|
+
/// value to pass to a higher order function. It is not possible to use `*`
|
|
248
|
+
/// as a function value at the moment.
|
|
249
|
+
///
|
|
250
|
+
/// Example:
|
|
251
|
+
/// ```motoko include=import
|
|
252
|
+
/// import Array "mo:base/Array";
|
|
253
|
+
/// Array.foldLeft([2, 3, 1], 1, Nat.mul) // => 6
|
|
254
|
+
/// ```
|
|
255
|
+
public func mul(x : Nat, y : Nat) : Nat { x * y };
|
|
256
|
+
|
|
257
|
+
/// Returns the unsigned integer division of `x` by `y`, `x / y`.
|
|
258
|
+
/// Traps when `y` is zero.
|
|
259
|
+
///
|
|
260
|
+
/// The quotient is rounded down, which is equivalent to truncating the
|
|
261
|
+
/// decimal places of the quotient.
|
|
262
|
+
///
|
|
263
|
+
/// Example:
|
|
264
|
+
/// ```motoko include=import
|
|
265
|
+
/// ignore Nat.div(6, 2); // => 3
|
|
266
|
+
/// 6 / 2 // => 3
|
|
267
|
+
/// ```
|
|
268
|
+
///
|
|
269
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
270
|
+
/// to the existing `/` operator) is so that you can use it as a function
|
|
271
|
+
/// value to pass to a higher order function. It is not possible to use `/`
|
|
272
|
+
/// as a function value at the moment.
|
|
273
|
+
public func div(x : Nat, y : Nat) : Nat { x / y };
|
|
274
|
+
|
|
275
|
+
/// Returns the remainder of unsigned integer division of `x` by `y`, `x % y`.
|
|
276
|
+
/// Traps when `y` is zero.
|
|
277
|
+
///
|
|
278
|
+
/// Example:
|
|
279
|
+
/// ```motoko include=import
|
|
280
|
+
/// ignore Nat.rem(6, 4); // => 2
|
|
281
|
+
/// 6 % 4 // => 2
|
|
282
|
+
/// ```
|
|
283
|
+
///
|
|
284
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
285
|
+
/// to the existing `%` operator) is so that you can use it as a function
|
|
286
|
+
/// value to pass to a higher order function. It is not possible to use `%`
|
|
287
|
+
/// as a function value at the moment.
|
|
288
|
+
public func rem(x : Nat, y : Nat) : Nat { x % y };
|
|
289
|
+
|
|
290
|
+
/// Returns `x` to the power of `y`, `x ** y`. Traps when `y > 2^32`. This operator
|
|
291
|
+
/// will never overflow because `Nat` is infinite precision.
|
|
292
|
+
///
|
|
293
|
+
/// Example:
|
|
294
|
+
/// ```motoko include=import
|
|
295
|
+
/// ignore Nat.pow(2, 3); // => 8
|
|
296
|
+
/// 2 ** 3 // => 8
|
|
297
|
+
/// ```
|
|
298
|
+
///
|
|
299
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
300
|
+
/// to the existing `**` operator) is so that you can use it as a function
|
|
301
|
+
/// value to pass to a higher order function. It is not possible to use `**`
|
|
302
|
+
/// as a function value at the moment.
|
|
303
|
+
public func pow(x : Nat, y : Nat) : Nat { x ** y };
|
|
304
|
+
|
|
305
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/// 16-bit unsigned integers with checked arithmetic
|
|
2
|
+
///
|
|
3
|
+
/// Most operations are available as built-in operators (e.g. `1 + 1`).
|
|
4
|
+
import Nat "Nat";
|
|
5
|
+
import Prim "mo:⛔";
|
|
6
|
+
|
|
7
|
+
module {
|
|
8
|
+
|
|
9
|
+
/// 16-bit natural numbers.
|
|
10
|
+
public type Nat16 = Prim.Types.Nat16;
|
|
11
|
+
|
|
12
|
+
/// Conversion.
|
|
13
|
+
public let toNat : Nat16 -> Nat = Prim.nat16ToNat;
|
|
14
|
+
|
|
15
|
+
/// Conversion. Traps on overflow/underflow.
|
|
16
|
+
public let fromNat : Nat -> Nat16 = Prim.natToNat16;
|
|
17
|
+
|
|
18
|
+
/// Conversion. Wraps on overflow/underflow.
|
|
19
|
+
public let fromIntWrap : Int -> Nat16 = Prim.intToNat16Wrap;
|
|
20
|
+
|
|
21
|
+
/// Returns the Text representation of `x`.
|
|
22
|
+
public func toText(x : Nat16) : Text {
|
|
23
|
+
Nat.toText(toNat(x))
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/// Returns the minimum of `x` and `y`.
|
|
27
|
+
public func min(x : Nat16, y : Nat16) : Nat16 {
|
|
28
|
+
if (x < y) { x } else { y }
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/// Returns the maximum of `x` and `y`.
|
|
32
|
+
public func max(x : Nat16, y : Nat16) : Nat16 {
|
|
33
|
+
if (x < y) { y } else { x }
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/// Returns `x == y`.
|
|
37
|
+
public func equal(x : Nat16, y : Nat16) : Bool { x == y };
|
|
38
|
+
|
|
39
|
+
/// Returns `x != y`.
|
|
40
|
+
public func notEqual(x : Nat16, y : Nat16) : Bool { x != y };
|
|
41
|
+
|
|
42
|
+
/// Returns `x < y`.
|
|
43
|
+
public func less(x : Nat16, y : Nat16) : Bool { x < y };
|
|
44
|
+
|
|
45
|
+
/// Returns `x <= y`.
|
|
46
|
+
public func lessOrEqual(x : Nat16, y : Nat16) : Bool { x <= y };
|
|
47
|
+
|
|
48
|
+
/// Returns `x > y`.
|
|
49
|
+
public func greater(x : Nat16, y : Nat16) : Bool { x > y };
|
|
50
|
+
|
|
51
|
+
/// Returns `x >= y`.
|
|
52
|
+
public func greaterOrEqual(x : Nat16, y : Nat16) : Bool { x >= y };
|
|
53
|
+
|
|
54
|
+
/// Returns the order of `x` and `y`.
|
|
55
|
+
public func compare(x : Nat16, y : Nat16) : { #less; #equal; #greater } {
|
|
56
|
+
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/// Returns the sum of `x` and `y`, `x + y`. Traps on overflow.
|
|
60
|
+
public func add(x : Nat16, y : Nat16) : Nat16 { x + y };
|
|
61
|
+
|
|
62
|
+
/// Returns the difference of `x` and `y`, `x - y`. Traps on underflow.
|
|
63
|
+
public func sub(x : Nat16, y : Nat16) : Nat16 { x - y };
|
|
64
|
+
|
|
65
|
+
/// Returns the product of `x` and `y`, `x * y`. Traps on overflow.
|
|
66
|
+
public func mul(x : Nat16, y : Nat16) : Nat16 { x * y };
|
|
67
|
+
|
|
68
|
+
/// Returns the division of `x by y`, `x / y`.
|
|
69
|
+
/// Traps when `y` is zero.
|
|
70
|
+
public func div(x : Nat16, y : Nat16) : Nat16 { x / y };
|
|
71
|
+
|
|
72
|
+
/// Returns the remainder of `x` divided by `y`, `x % y`.
|
|
73
|
+
/// Traps when `y` is zero.
|
|
74
|
+
public func rem(x : Nat16, y : Nat16) : Nat16 { x % y };
|
|
75
|
+
|
|
76
|
+
/// Returns `x` to the power of `y`, `x ** y`. Traps on overflow.
|
|
77
|
+
public func pow(x : Nat16, y : Nat16) : Nat16 { x ** y };
|
|
78
|
+
|
|
79
|
+
/// Returns the bitwise negation of `x`, `^x`.
|
|
80
|
+
public func bitnot(x : Nat16) : Nat16 { ^x };
|
|
81
|
+
|
|
82
|
+
/// Returns the bitwise and of `x` and `y`, `x & y`.
|
|
83
|
+
public func bitand(x : Nat16, y : Nat16) : Nat16 { x & y };
|
|
84
|
+
|
|
85
|
+
/// Returns the bitwise or of `x` and `y`, `x \| y`.
|
|
86
|
+
public func bitor(x : Nat16, y : Nat16) : Nat16 { x | y };
|
|
87
|
+
|
|
88
|
+
/// Returns the bitwise exclusive or of `x` and `y`, `x ^ y`.
|
|
89
|
+
public func bitxor(x : Nat16, y : Nat16) : Nat16 { x ^ y };
|
|
90
|
+
|
|
91
|
+
/// Returns the bitwise shift left of `x` by `y`, `x << y`.
|
|
92
|
+
public func bitshiftLeft(x : Nat16, y : Nat16) : Nat16 { x << y };
|
|
93
|
+
|
|
94
|
+
/// Returns the bitwise shift right of `x` by `y`, `x >> y`.
|
|
95
|
+
public func bitshiftRight(x : Nat16, y : Nat16) : Nat16 { x >> y };
|
|
96
|
+
|
|
97
|
+
/// Returns the bitwise rotate left of `x` by `y`, `x <<> y`.
|
|
98
|
+
public func bitrotLeft(x : Nat16, y : Nat16) : Nat16 { x <<> y };
|
|
99
|
+
|
|
100
|
+
/// Returns the bitwise rotate right of `x` by `y`, `x <>> y`.
|
|
101
|
+
public func bitrotRight(x : Nat16, y : Nat16) : Nat16 { x <>> y };
|
|
102
|
+
|
|
103
|
+
/// Returns the value of bit `p mod 16` in `x`, `(x & 2^(p mod 16)) == 2^(p mod 16)`.
|
|
104
|
+
public func bittest(x : Nat16, p : Nat) : Bool {
|
|
105
|
+
Prim.btstNat16(x, Prim.natToNat16(p))
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/// Returns the value of setting bit `p mod 16` in `x` to `1`.
|
|
109
|
+
public func bitset(x : Nat16, p : Nat) : Nat16 {
|
|
110
|
+
x | (1 << Prim.natToNat16(p))
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/// Returns the value of clearing bit `p mod 16` in `x` to `0`.
|
|
114
|
+
public func bitclear(x : Nat16, p : Nat) : Nat16 {
|
|
115
|
+
x & ^(1 << Prim.natToNat16(p))
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/// Returns the value of flipping bit `p mod 16` in `x`.
|
|
119
|
+
public func bitflip(x : Nat16, p : Nat) : Nat16 {
|
|
120
|
+
x ^ (1 << Prim.natToNat16(p))
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/// Returns the count of non-zero bits in `x`.
|
|
124
|
+
public let bitcountNonZero : (x : Nat16) -> Nat16 = Prim.popcntNat16;
|
|
125
|
+
|
|
126
|
+
/// Returns the count of leading zero bits in `x`.
|
|
127
|
+
public let bitcountLeadingZero : (x : Nat16) -> Nat16 = Prim.clzNat16;
|
|
128
|
+
|
|
129
|
+
/// Returns the count of trailing zero bits in `x`.
|
|
130
|
+
public let bitcountTrailingZero : (x : Nat16) -> Nat16 = Prim.ctzNat16;
|
|
131
|
+
|
|
132
|
+
/// Returns the sum of `x` and `y`, `x +% y`. Wraps on overflow.
|
|
133
|
+
public func addWrap(x : Nat16, y : Nat16) : Nat16 { x +% y };
|
|
134
|
+
|
|
135
|
+
/// Returns the difference of `x` and `y`, `x -% y`. Wraps on underflow.
|
|
136
|
+
public func subWrap(x : Nat16, y : Nat16) : Nat16 { x -% y };
|
|
137
|
+
|
|
138
|
+
/// Returns the product of `x` and `y`, `x *% y`. Wraps on overflow.
|
|
139
|
+
public func mulWrap(x : Nat16, y : Nat16) : Nat16 { x *% y };
|
|
140
|
+
|
|
141
|
+
/// Returns `x` to the power of `y`, `x **% y`. Wraps on overflow.
|
|
142
|
+
public func powWrap(x : Nat16, y : Nat16) : Nat16 { x **% y };
|
|
143
|
+
|
|
144
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/// 32-bit unsigned integers with checked arithmetic
|
|
2
|
+
///
|
|
3
|
+
/// Most operations are available as built-in operators (e.g. `1 + 1`).
|
|
4
|
+
import Nat "Nat";
|
|
5
|
+
import Prim "mo:⛔";
|
|
6
|
+
|
|
7
|
+
module {
|
|
8
|
+
|
|
9
|
+
/// 32-bit natural numbers.
|
|
10
|
+
public type Nat32 = Prim.Types.Nat32;
|
|
11
|
+
|
|
12
|
+
/// Conversion.
|
|
13
|
+
public let toNat : Nat32 -> Nat = Prim.nat32ToNat;
|
|
14
|
+
|
|
15
|
+
/// Conversion. Traps on overflow/underflow.
|
|
16
|
+
public let fromNat : Nat -> Nat32 = Prim.natToNat32;
|
|
17
|
+
|
|
18
|
+
/// Conversion. Wraps on overflow/underflow.
|
|
19
|
+
public let fromIntWrap : Int -> Nat32 = Prim.intToNat32Wrap;
|
|
20
|
+
|
|
21
|
+
/// Returns the Text representation of `x`.
|
|
22
|
+
public func toText(x : Nat32) : Text {
|
|
23
|
+
Nat.toText(toNat(x))
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/// Returns the minimum of `x` and `y`.
|
|
27
|
+
public func min(x : Nat32, y : Nat32) : Nat32 {
|
|
28
|
+
if (x < y) { x } else { y }
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/// Returns the maximum of `x` and `y`.
|
|
32
|
+
public func max(x : Nat32, y : Nat32) : Nat32 {
|
|
33
|
+
if (x < y) { y } else { x }
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/// Returns `x == y`.
|
|
37
|
+
public func equal(x : Nat32, y : Nat32) : Bool { x == y };
|
|
38
|
+
|
|
39
|
+
/// Returns `x != y`.
|
|
40
|
+
public func notEqual(x : Nat32, y : Nat32) : Bool { x != y };
|
|
41
|
+
|
|
42
|
+
/// Returns `x < y`.
|
|
43
|
+
public func less(x : Nat32, y : Nat32) : Bool { x < y };
|
|
44
|
+
|
|
45
|
+
/// Returns `x <= y`.
|
|
46
|
+
public func lessOrEqual(x : Nat32, y : Nat32) : Bool { x <= y };
|
|
47
|
+
|
|
48
|
+
/// Returns `x > y`.
|
|
49
|
+
public func greater(x : Nat32, y : Nat32) : Bool { x > y };
|
|
50
|
+
|
|
51
|
+
/// Returns `x >= y`.
|
|
52
|
+
public func greaterOrEqual(x : Nat32, y : Nat32) : Bool { x >= y };
|
|
53
|
+
|
|
54
|
+
/// Returns the order of `x` and `y`.
|
|
55
|
+
public func compare(x : Nat32, y : Nat32) : { #less; #equal; #greater } {
|
|
56
|
+
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/// Returns the sum of `x` and `y`, `x + y`. Traps on overflow.
|
|
60
|
+
public func add(x : Nat32, y : Nat32) : Nat32 { x + y };
|
|
61
|
+
|
|
62
|
+
/// Returns the difference of `x` and `y`, `x - y`. Traps on underflow.
|
|
63
|
+
public func sub(x : Nat32, y : Nat32) : Nat32 { x - y };
|
|
64
|
+
|
|
65
|
+
/// Returns the product of `x` and `y`, `x * y`. Traps on overflow.
|
|
66
|
+
public func mul(x : Nat32, y : Nat32) : Nat32 { x * y };
|
|
67
|
+
|
|
68
|
+
/// Returns the division of `x by y`, `x / y`.
|
|
69
|
+
/// Traps when `y` is zero.
|
|
70
|
+
public func div(x : Nat32, y : Nat32) : Nat32 { x / y };
|
|
71
|
+
|
|
72
|
+
/// Returns the remainder of `x` divided by `y`, `x % y`.
|
|
73
|
+
/// Traps when `y` is zero.
|
|
74
|
+
public func rem(x : Nat32, y : Nat32) : Nat32 { x % y };
|
|
75
|
+
|
|
76
|
+
/// Returns `x` to the power of `y`, `x ** y`. Traps on overflow.
|
|
77
|
+
public func pow(x : Nat32, y : Nat32) : Nat32 { x ** y };
|
|
78
|
+
|
|
79
|
+
/// Returns the bitwise negation of `x`, `^x`.
|
|
80
|
+
public func bitnot(x : Nat32) : Nat32 { ^x };
|
|
81
|
+
|
|
82
|
+
/// Returns the bitwise and of `x` and `y`, `x & y`.
|
|
83
|
+
public func bitand(x : Nat32, y : Nat32) : Nat32 { x & y };
|
|
84
|
+
|
|
85
|
+
/// Returns the bitwise or of `x` and `y`, `x \| y`.
|
|
86
|
+
public func bitor(x : Nat32, y : Nat32) : Nat32 { x | y };
|
|
87
|
+
|
|
88
|
+
/// Returns the bitwise exclusive or of `x` and `y`, `x ^ y`.
|
|
89
|
+
public func bitxor(x : Nat32, y : Nat32) : Nat32 { x ^ y };
|
|
90
|
+
|
|
91
|
+
/// Returns the bitwise shift left of `x` by `y`, `x << y`.
|
|
92
|
+
public func bitshiftLeft(x : Nat32, y : Nat32) : Nat32 { x << y };
|
|
93
|
+
|
|
94
|
+
/// Returns the bitwise shift right of `x` by `y`, `x >> y`.
|
|
95
|
+
public func bitshiftRight(x : Nat32, y : Nat32) : Nat32 { x >> y };
|
|
96
|
+
|
|
97
|
+
/// Returns the bitwise rotate left of `x` by `y`, `x <<> y`.
|
|
98
|
+
public func bitrotLeft(x : Nat32, y : Nat32) : Nat32 { x <<> y };
|
|
99
|
+
|
|
100
|
+
/// Returns the bitwise rotate right of `x` by `y`, `x <>> y`.
|
|
101
|
+
public func bitrotRight(x : Nat32, y : Nat32) : Nat32 { x <>> y };
|
|
102
|
+
|
|
103
|
+
/// Returns the value of bit `p mod 32` in `x`, `(x & 2^(p mod 32)) == 2^(p mod 32)`.
|
|
104
|
+
public func bittest(x : Nat32, p : Nat) : Bool {
|
|
105
|
+
Prim.btstNat32(x, Prim.natToNat32(p))
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/// Returns the value of setting bit `p mod 32` in `x` to `1`.
|
|
109
|
+
public func bitset(x : Nat32, p : Nat) : Nat32 {
|
|
110
|
+
x | (1 << Prim.natToNat32(p))
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/// Returns the value of clearing bit `p mod 32` in `x` to `0`.
|
|
114
|
+
public func bitclear(x : Nat32, p : Nat) : Nat32 {
|
|
115
|
+
x & ^(1 << Prim.natToNat32(p))
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/// Returns the value of flipping bit `p mod 32` in `x`.
|
|
119
|
+
public func bitflip(x : Nat32, p : Nat) : Nat32 {
|
|
120
|
+
x ^ (1 << Prim.natToNat32(p))
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/// Returns the count of non-zero bits in `x`.
|
|
124
|
+
public let bitcountNonZero : (x : Nat32) -> Nat32 = Prim.popcntNat32;
|
|
125
|
+
|
|
126
|
+
/// Returns the count of leading zero bits in `x`.
|
|
127
|
+
public let bitcountLeadingZero : (x : Nat32) -> Nat32 = Prim.clzNat32;
|
|
128
|
+
|
|
129
|
+
/// Returns the count of trailing zero bits in `x`.
|
|
130
|
+
public let bitcountTrailingZero : (x : Nat32) -> Nat32 = Prim.ctzNat32;
|
|
131
|
+
|
|
132
|
+
/// Returns the sum of `x` and `y`, `x +% y`. Wraps on overflow.
|
|
133
|
+
public func addWrap(x : Nat32, y : Nat32) : Nat32 { x +% y };
|
|
134
|
+
|
|
135
|
+
/// Returns the difference of `x` and `y`, `x -% y`. Wraps on underflow.
|
|
136
|
+
public func subWrap(x : Nat32, y : Nat32) : Nat32 { x -% y };
|
|
137
|
+
|
|
138
|
+
/// Returns the product of `x` and `y`, `x *% y`. Wraps on overflow.
|
|
139
|
+
public func mulWrap(x : Nat32, y : Nat32) : Nat32 { x *% y };
|
|
140
|
+
|
|
141
|
+
/// Returns `x` to the power of `y`, `x **% y`. Wraps on overflow.
|
|
142
|
+
public func powWrap(x : Nat32, y : Nat32) : Nat32 { x **% y };
|
|
143
|
+
|
|
144
|
+
}
|