Skip to content

Commit fdd2f1c

Browse files
authored
fix(stdlib)!: Provide correct types for BigInt operations (#1297)
1 parent abf9749 commit fdd2f1c

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

stdlib/bigint.gr

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export toNumber
5151
*/
5252
@unsafe
5353
export let incr = (num: BigInt) => {
54-
WasmI32.toGrain(BI.incr(WasmI32.fromGrain(num)))
54+
WasmI32.toGrain(BI.incr(WasmI32.fromGrain(num))): BigInt
5555
}
5656

5757
/**
@@ -64,7 +64,7 @@ export let incr = (num: BigInt) => {
6464
*/
6565
@unsafe
6666
export let decr = (num: BigInt) => {
67-
WasmI32.toGrain(BI.decr(WasmI32.fromGrain(num)))
67+
WasmI32.toGrain(BI.decr(WasmI32.fromGrain(num))): BigInt
6868
}
6969

7070
/**
@@ -104,7 +104,9 @@ export let abs = (num: BigInt) => {
104104
*/
105105
@unsafe
106106
export let add = (num1: BigInt, num2: BigInt) => {
107-
WasmI32.toGrain(BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)))
107+
WasmI32.toGrain(
108+
BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
109+
): BigInt
108110
}
109111

110112
/**
@@ -118,7 +120,9 @@ export let add = (num1: BigInt, num2: BigInt) => {
118120
*/
119121
@unsafe
120122
export let sub = (num1: BigInt, num2: BigInt) => {
121-
WasmI32.toGrain(BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)))
123+
WasmI32.toGrain(
124+
BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
125+
): BigInt
122126
}
123127

124128
/**
@@ -132,7 +136,9 @@ export let sub = (num1: BigInt, num2: BigInt) => {
132136
*/
133137
@unsafe
134138
export let mul = (num1: BigInt, num2: BigInt) => {
135-
WasmI32.toGrain(BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)))
139+
WasmI32.toGrain(
140+
BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
141+
): BigInt
136142
}
137143

138144
// For further reading on Truncated vs. Floored division: https://en.wikipedia.org/wiki/Modulo_operation
@@ -201,7 +207,9 @@ export let quotRem = (num1: BigInt, num2: BigInt) => {
201207
*/
202208
@unsafe
203209
export let gcd = (num1: BigInt, num2: BigInt) => {
204-
WasmI32.toGrain(BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)))
210+
WasmI32.toGrain(
211+
BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
212+
): BigInt
205213
}
206214

207215
/**
@@ -368,7 +376,7 @@ export let gte = (num1: BigInt, num2: BigInt) => {
368376
*/
369377
@unsafe
370378
export let lnot = (num: BigInt) => {
371-
WasmI32.toGrain(BI.bitwiseNot(WasmI32.fromGrain(num)))
379+
WasmI32.toGrain(BI.bitwiseNot(WasmI32.fromGrain(num))): BigInt
372380
}
373381

374382
/**
@@ -384,7 +392,7 @@ export let lnot = (num: BigInt) => {
384392
export let land = (num1: BigInt, num2: BigInt) => {
385393
WasmI32.toGrain(
386394
BI.bitwiseAnd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
387-
)
395+
): BigInt
388396
}
389397

390398
/**
@@ -400,7 +408,7 @@ export let land = (num1: BigInt, num2: BigInt) => {
400408
export let lor = (num1: BigInt, num2: BigInt) => {
401409
WasmI32.toGrain(
402410
BI.bitwiseOr(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
403-
)
411+
): BigInt
404412
}
405413

406414
/**
@@ -416,7 +424,7 @@ export let lor = (num1: BigInt, num2: BigInt) => {
416424
export let lxor = (num1: BigInt, num2: BigInt) => {
417425
WasmI32.toGrain(
418426
BI.bitwiseXor(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
419-
)
427+
): BigInt
420428
}
421429

422430
/**

stdlib/bigint.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ No other changes yet.
7979
</details>
8080

8181
```grain
82-
incr : BigInt -> a
82+
incr : BigInt -> BigInt
8383
```
8484

8585
Increments the value by one.
@@ -94,7 +94,7 @@ Returns:
9494

9595
|type|description|
9696
|----|-----------|
97-
|`a`|The incremented value|
97+
|`BigInt`|The incremented value|
9898

9999
### Bigint.**decr**
100100

@@ -104,7 +104,7 @@ No other changes yet.
104104
</details>
105105

106106
```grain
107-
decr : BigInt -> a
107+
decr : BigInt -> BigInt
108108
```
109109

110110
Decrements the value by one.
@@ -119,7 +119,7 @@ Returns:
119119

120120
|type|description|
121121
|----|-----------|
122-
|`a`|The decremented value|
122+
|`BigInt`|The decremented value|
123123

124124
### Bigint.**neg**
125125

@@ -179,7 +179,7 @@ No other changes yet.
179179
</details>
180180

181181
```grain
182-
add : (BigInt, BigInt) -> a
182+
add : (BigInt, BigInt) -> BigInt
183183
```
184184

185185
Computes the sum of its operands.
@@ -195,7 +195,7 @@ Returns:
195195

196196
|type|description|
197197
|----|-----------|
198-
|`a`|The sum of the two operands|
198+
|`BigInt`|The sum of the two operands|
199199

200200
### Bigint.**sub**
201201

@@ -205,7 +205,7 @@ No other changes yet.
205205
</details>
206206

207207
```grain
208-
sub : (BigInt, BigInt) -> a
208+
sub : (BigInt, BigInt) -> BigInt
209209
```
210210

211211
Computes the difference of its operands.
@@ -221,7 +221,7 @@ Returns:
221221

222222
|type|description|
223223
|----|-----------|
224-
|`a`|The difference of the two operands|
224+
|`BigInt`|The difference of the two operands|
225225

226226
### Bigint.**mul**
227227

@@ -231,7 +231,7 @@ No other changes yet.
231231
</details>
232232

233233
```grain
234-
mul : (BigInt, BigInt) -> a
234+
mul : (BigInt, BigInt) -> BigInt
235235
```
236236

237237
Computes the product of its operands.
@@ -247,7 +247,7 @@ Returns:
247247

248248
|type|description|
249249
|----|-----------|
250-
|`a`|The product of the two operands|
250+
|`BigInt`|The product of the two operands|
251251

252252
### Bigint.**div**
253253

@@ -337,7 +337,7 @@ No other changes yet.
337337
</details>
338338

339339
```grain
340-
gcd : (BigInt, BigInt) -> a
340+
gcd : (BigInt, BigInt) -> BigInt
341341
```
342342

343343
Computes the greatest common divisior of the two operands.
@@ -353,7 +353,7 @@ Returns:
353353

354354
|type|description|
355355
|----|-----------|
356-
|`a`|The greatest common divisor of its operands|
356+
|`BigInt`|The greatest common divisor of its operands|
357357

358358
## Bitwise operations
359359

@@ -608,7 +608,7 @@ No other changes yet.
608608
</details>
609609

610610
```grain
611-
lnot : BigInt -> a
611+
lnot : BigInt -> BigInt
612612
```
613613

614614
Computes the bitwise NOT of the given value.
@@ -623,7 +623,7 @@ Returns:
623623

624624
|type|description|
625625
|----|-----------|
626-
|`a`|Containing the inverted bits of the given value|
626+
|`BigInt`|Containing the inverted bits of the given value|
627627

628628
### Bigint.**land**
629629

@@ -633,7 +633,7 @@ No other changes yet.
633633
</details>
634634

635635
```grain
636-
land : (BigInt, BigInt) -> a
636+
land : (BigInt, BigInt) -> BigInt
637637
```
638638

639639
Computes the bitwise AND (`&`) on the given operands.
@@ -649,7 +649,7 @@ Returns:
649649

650650
|type|description|
651651
|----|-----------|
652-
|`a`|Containing a `1` in each bit position for which the corresponding bits of both operands are `1`|
652+
|`BigInt`|Containing a `1` in each bit position for which the corresponding bits of both operands are `1`|
653653

654654
### Bigint.**lor**
655655

@@ -659,7 +659,7 @@ No other changes yet.
659659
</details>
660660

661661
```grain
662-
lor : (BigInt, BigInt) -> a
662+
lor : (BigInt, BigInt) -> BigInt
663663
```
664664

665665
Computes the bitwise OR (`|`) on the given operands.
@@ -675,7 +675,7 @@ Returns:
675675

676676
|type|description|
677677
|----|-----------|
678-
|`a`|Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`|
678+
|`BigInt`|Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`|
679679

680680
### Bigint.**lxor**
681681

@@ -685,7 +685,7 @@ No other changes yet.
685685
</details>
686686

687687
```grain
688-
lxor : (BigInt, BigInt) -> a
688+
lxor : (BigInt, BigInt) -> BigInt
689689
```
690690

691691
Computes the bitwise XOR (`^`) on the given operands.
@@ -701,7 +701,7 @@ Returns:
701701

702702
|type|description|
703703
|----|-----------|
704-
|`a`|Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`|
704+
|`BigInt`|Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`|
705705

706706
### Bigint.**clz**
707707

0 commit comments

Comments
 (0)