From 5e6106d3b9e2e061c584c323fc1d60097e53bbb5 Mon Sep 17 00:00:00 2001 From: Oscar Spencer Date: Fri, 3 Jun 2022 16:59:33 -0500 Subject: [PATCH] fix(stdlib)!: Provide correct types for BigInt operations --- stdlib/bigint.gr | 28 ++++++++++++++++++---------- stdlib/bigint.md | 40 ++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/stdlib/bigint.gr b/stdlib/bigint.gr index da3fbe050..1a924d6e8 100644 --- a/stdlib/bigint.gr +++ b/stdlib/bigint.gr @@ -51,7 +51,7 @@ export toNumber */ @unsafe export let incr = (num: BigInt) => { - WasmI32.toGrain(BI.incr(WasmI32.fromGrain(num))) + WasmI32.toGrain(BI.incr(WasmI32.fromGrain(num))): BigInt } /** @@ -64,7 +64,7 @@ export let incr = (num: BigInt) => { */ @unsafe export let decr = (num: BigInt) => { - WasmI32.toGrain(BI.decr(WasmI32.fromGrain(num))) + WasmI32.toGrain(BI.decr(WasmI32.fromGrain(num))): BigInt } /** @@ -104,7 +104,9 @@ export let abs = (num: BigInt) => { */ @unsafe export let add = (num1: BigInt, num2: BigInt) => { - WasmI32.toGrain(BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))) + WasmI32.toGrain( + BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) + ): BigInt } /** @@ -118,7 +120,9 @@ export let add = (num1: BigInt, num2: BigInt) => { */ @unsafe export let sub = (num1: BigInt, num2: BigInt) => { - WasmI32.toGrain(BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))) + WasmI32.toGrain( + BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) + ): BigInt } /** @@ -132,7 +136,9 @@ export let sub = (num1: BigInt, num2: BigInt) => { */ @unsafe export let mul = (num1: BigInt, num2: BigInt) => { - WasmI32.toGrain(BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))) + WasmI32.toGrain( + BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) + ): BigInt } // 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) => { */ @unsafe export let gcd = (num1: BigInt, num2: BigInt) => { - WasmI32.toGrain(BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))) + WasmI32.toGrain( + BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) + ): BigInt } /** @@ -368,7 +376,7 @@ export let gte = (num1: BigInt, num2: BigInt) => { */ @unsafe export let lnot = (num: BigInt) => { - WasmI32.toGrain(BI.bitwiseNot(WasmI32.fromGrain(num))) + WasmI32.toGrain(BI.bitwiseNot(WasmI32.fromGrain(num))): BigInt } /** @@ -384,7 +392,7 @@ export let lnot = (num: BigInt) => { export let land = (num1: BigInt, num2: BigInt) => { WasmI32.toGrain( BI.bitwiseAnd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) - ) + ): BigInt } /** @@ -400,7 +408,7 @@ export let land = (num1: BigInt, num2: BigInt) => { export let lor = (num1: BigInt, num2: BigInt) => { WasmI32.toGrain( BI.bitwiseOr(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) - ) + ): BigInt } /** @@ -416,7 +424,7 @@ export let lor = (num1: BigInt, num2: BigInt) => { export let lxor = (num1: BigInt, num2: BigInt) => { WasmI32.toGrain( BI.bitwiseXor(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2)) - ) + ): BigInt } /** diff --git a/stdlib/bigint.md b/stdlib/bigint.md index fc36158cb..b13c58525 100644 --- a/stdlib/bigint.md +++ b/stdlib/bigint.md @@ -79,7 +79,7 @@ No other changes yet. ```grain -incr : BigInt -> a +incr : BigInt -> BigInt ``` Increments the value by one. @@ -94,7 +94,7 @@ Returns: |type|description| |----|-----------| -|`a`|The incremented value| +|`BigInt`|The incremented value| ### Bigint.**decr** @@ -104,7 +104,7 @@ No other changes yet. ```grain -decr : BigInt -> a +decr : BigInt -> BigInt ``` Decrements the value by one. @@ -119,7 +119,7 @@ Returns: |type|description| |----|-----------| -|`a`|The decremented value| +|`BigInt`|The decremented value| ### Bigint.**neg** @@ -179,7 +179,7 @@ No other changes yet. ```grain -add : (BigInt, BigInt) -> a +add : (BigInt, BigInt) -> BigInt ``` Computes the sum of its operands. @@ -195,7 +195,7 @@ Returns: |type|description| |----|-----------| -|`a`|The sum of the two operands| +|`BigInt`|The sum of the two operands| ### Bigint.**sub** @@ -205,7 +205,7 @@ No other changes yet. ```grain -sub : (BigInt, BigInt) -> a +sub : (BigInt, BigInt) -> BigInt ``` Computes the difference of its operands. @@ -221,7 +221,7 @@ Returns: |type|description| |----|-----------| -|`a`|The difference of the two operands| +|`BigInt`|The difference of the two operands| ### Bigint.**mul** @@ -231,7 +231,7 @@ No other changes yet. ```grain -mul : (BigInt, BigInt) -> a +mul : (BigInt, BigInt) -> BigInt ``` Computes the product of its operands. @@ -247,7 +247,7 @@ Returns: |type|description| |----|-----------| -|`a`|The product of the two operands| +|`BigInt`|The product of the two operands| ### Bigint.**div** @@ -337,7 +337,7 @@ No other changes yet. ```grain -gcd : (BigInt, BigInt) -> a +gcd : (BigInt, BigInt) -> BigInt ``` Computes the greatest common divisior of the two operands. @@ -353,7 +353,7 @@ Returns: |type|description| |----|-----------| -|`a`|The greatest common divisor of its operands| +|`BigInt`|The greatest common divisor of its operands| ## Bitwise operations @@ -608,7 +608,7 @@ No other changes yet. ```grain -lnot : BigInt -> a +lnot : BigInt -> BigInt ``` Computes the bitwise NOT of the given value. @@ -623,7 +623,7 @@ Returns: |type|description| |----|-----------| -|`a`|Containing the inverted bits of the given value| +|`BigInt`|Containing the inverted bits of the given value| ### Bigint.**land** @@ -633,7 +633,7 @@ No other changes yet. ```grain -land : (BigInt, BigInt) -> a +land : (BigInt, BigInt) -> BigInt ``` Computes the bitwise AND (`&`) on the given operands. @@ -649,7 +649,7 @@ Returns: |type|description| |----|-----------| -|`a`|Containing a `1` in each bit position for which the corresponding bits of both operands are `1`| +|`BigInt`|Containing a `1` in each bit position for which the corresponding bits of both operands are `1`| ### Bigint.**lor** @@ -659,7 +659,7 @@ No other changes yet. ```grain -lor : (BigInt, BigInt) -> a +lor : (BigInt, BigInt) -> BigInt ``` Computes the bitwise OR (`|`) on the given operands. @@ -675,7 +675,7 @@ Returns: |type|description| |----|-----------| -|`a`|Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`| +|`BigInt`|Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`| ### Bigint.**lxor** @@ -685,7 +685,7 @@ No other changes yet. ```grain -lxor : (BigInt, BigInt) -> a +lxor : (BigInt, BigInt) -> BigInt ``` Computes the bitwise XOR (`^`) on the given operands. @@ -701,7 +701,7 @@ Returns: |type|description| |----|-----------| -|`a`|Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`| +|`BigInt`|Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`| ### Bigint.**clz**