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

Compiled js min/max value with bigint #7062

Open
remitbri opened this issue Sep 30, 2024 · 12 comments
Open

Compiled js min/max value with bigint #7062

remitbri opened this issue Sep 30, 2024 · 12 comments

Comments

@remitbri
Copy link

We can't use Math.min/max for bigint values.

If the rescript code is simple enough,

let a = 5n
let b = 7n
let c = max(a,b)

the compiled js is simple too:

var c = 5n > 7n ? 5n : 7n;

var a = 5n;

var b = 7n;

But if it's trickier,

let a = (x)=> BigInt.add(x, 5n)
let b = (x)=> BigInt.add(x, 7n)

let c =  max(a(4n), b(9n))

the compiled js is

import * as Caml from "./stdlib/caml.js";

function a(x) {
  return x + 5n;
}

function b(x) {
  return x + 7n;
}

var c = Caml.bigint_max(4n + 5n, 9n + 7n);

Problem:

"Property 'bigint_max' may not exist on type 'typeof import("project_path/node_modules/rescript/lib/es6/caml")'. Did you mean 'int_max'?

@zth
Copy link
Collaborator

zth commented Sep 30, 2024

Thanks for the report!

Who's best to look at this, @mununki perhaps?

@cometkim
Copy link
Member

cometkim commented Oct 4, 2024

We can't use Math.min/max for bigint values.

You can't use Math.min/max for "bigint" in JS, that operations are for "number" types

@cometkim
Copy link
Member

cometkim commented Oct 4, 2024

We could add min/max as compiler primitives (and handle it in #7057 too), but that would add some runtime codes anyway. To make it "simple enough"

@mununki
Copy link
Member

mununki commented Oct 4, 2024

I'm thinking that adding primitives to have the compiler throw errors when using max or min with bigint might be a good idea. What do you think?

@cometkim
Copy link
Member

cometkim commented Oct 4, 2024

Actually the compiler's %min / %max primitives already support bigints as well, and I don't think there's reason to drop it.

"Property 'bigint_max' may not exist on type 'typeof import("project_path/node_modules/rescript/lib/es6/caml")'. Did you mean 'int_max'?

What the @remitbri reported is more like not a runtime error, but a Gentype-related one.

@cometkim
Copy link
Member

cometkim commented Oct 4, 2024

Also note the bigint folding will be a bit improved in v12 by #7029

@mununki
Copy link
Member

mununki commented Oct 6, 2024

Actually the compiler's %min / %max primitives already support bigints as well, and I don't think there's reason to drop it.

As far as I know, JavaScript does not support Math.max and Math.min for BigInt. Does this mean you are suggesting adding runtime support for max and min in ReScript? How do you plan to implement the runtime?

@remitbri
Copy link
Author

remitbri commented Oct 6, 2024

When I was saying "We can't use Math.min/max for bigint values." in the issue description, it was about Javascript support, that's why I wasn't keen on having a compiler primitive. That being said, we could have a BigInt.min (and BigInt.max) that would just compile to a < b ? a : b

@cometkim
Copy link
Member

cometkim commented Oct 6, 2024

That's what exactly we have already. Just need to expose it to the API surface

@mununki
Copy link
Member

mununki commented Oct 7, 2024

I guess there is an issue with latest master branch:

let max0 = max(1n, 2n)
let min0 = min(1n, 2n)
let c0 = 1n > 2n

let a = 5n
let b = 7n
let c = max(a, b)

compiled to

let max0 = Primitive_bigint.compare(1n, 2n); // should be max?

let min0 = Primitive_bigint.compare(1n, 2n); // should be min?

let c0 = 1n > 2n;

let c = Primitive_bigint.compare(5n, 7n); // should be max?

let a = 5n;

let b = 7n;

@mununki
Copy link
Member

mununki commented Oct 7, 2024

Since the PR #7088, it is compiled to:

open! Js.BigInt

let e = x => x + 5n
let f = x => x + 7n

let g = max(e(4n), f(9n))
function e(x) {
  return x + 5n;
}

function f(x) {
  return x + 7n;
}

let g = Primitive_bigint.max(4n + 5n, 9n + 7n);

@mununki
Copy link
Member

mununki commented Oct 7, 2024

// Primitive_bigint.js
function max(x, y) {
  if (x > y) {
    return x;
  } else {
    return y;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants