Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stdlib)!: Provide correct types for BigInt operations #1297

Merged
merged 1 commit into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions stdlib/bigint.gr
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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
}

/**
Expand Down
40 changes: 20 additions & 20 deletions stdlib/bigint.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ No other changes yet.
</details>

```grain
incr : BigInt -> a
incr : BigInt -> BigInt
```

Increments the value by one.
Expand All @@ -94,7 +94,7 @@ Returns:

|type|description|
|----|-----------|
|`a`|The incremented value|
|`BigInt`|The incremented value|

### Bigint.**decr**

Expand All @@ -104,7 +104,7 @@ No other changes yet.
</details>

```grain
decr : BigInt -> a
decr : BigInt -> BigInt
```

Decrements the value by one.
Expand All @@ -119,7 +119,7 @@ Returns:

|type|description|
|----|-----------|
|`a`|The decremented value|
|`BigInt`|The decremented value|

### Bigint.**neg**

Expand Down Expand Up @@ -179,7 +179,7 @@ No other changes yet.
</details>

```grain
add : (BigInt, BigInt) -> a
add : (BigInt, BigInt) -> BigInt
```

Computes the sum of its operands.
Expand All @@ -195,7 +195,7 @@ Returns:

|type|description|
|----|-----------|
|`a`|The sum of the two operands|
|`BigInt`|The sum of the two operands|

### Bigint.**sub**

Expand All @@ -205,7 +205,7 @@ No other changes yet.
</details>

```grain
sub : (BigInt, BigInt) -> a
sub : (BigInt, BigInt) -> BigInt
```

Computes the difference of its operands.
Expand All @@ -221,7 +221,7 @@ Returns:

|type|description|
|----|-----------|
|`a`|The difference of the two operands|
|`BigInt`|The difference of the two operands|

### Bigint.**mul**

Expand All @@ -231,7 +231,7 @@ No other changes yet.
</details>

```grain
mul : (BigInt, BigInt) -> a
mul : (BigInt, BigInt) -> BigInt
```

Computes the product of its operands.
Expand All @@ -247,7 +247,7 @@ Returns:

|type|description|
|----|-----------|
|`a`|The product of the two operands|
|`BigInt`|The product of the two operands|

### Bigint.**div**

Expand Down Expand Up @@ -337,7 +337,7 @@ No other changes yet.
</details>

```grain
gcd : (BigInt, BigInt) -> a
gcd : (BigInt, BigInt) -> BigInt
```

Computes the greatest common divisior of the two operands.
Expand All @@ -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

Expand Down Expand Up @@ -608,7 +608,7 @@ No other changes yet.
</details>

```grain
lnot : BigInt -> a
lnot : BigInt -> BigInt
```

Computes the bitwise NOT of the given value.
Expand All @@ -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**

Expand All @@ -633,7 +633,7 @@ No other changes yet.
</details>

```grain
land : (BigInt, BigInt) -> a
land : (BigInt, BigInt) -> BigInt
```

Computes the bitwise AND (`&`) on the given operands.
Expand All @@ -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**

Expand All @@ -659,7 +659,7 @@ No other changes yet.
</details>

```grain
lor : (BigInt, BigInt) -> a
lor : (BigInt, BigInt) -> BigInt
```

Computes the bitwise OR (`|`) on the given operands.
Expand All @@ -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**

Expand All @@ -685,7 +685,7 @@ No other changes yet.
</details>

```grain
lxor : (BigInt, BigInt) -> a
lxor : (BigInt, BigInt) -> BigInt
```

Computes the bitwise XOR (`^`) on the given operands.
Expand All @@ -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**

Expand Down