-
Notifications
You must be signed in to change notification settings - Fork 23
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
More programming content for TS/JS #72
Conversation
…ore-programming-content
What goes wrong? I can probably suggest a solution.
That's because it exists on the global namespace, not on a specific context, which is where you're trying to invoke it. That is, you'll need to pull it off of the result of the let wrapped = `
(function (Z3, setParam) { // note additional param
'use strict';
let module = {};
${result.result}
return module.exports;
})
`;
return await (0, eval)(wrapped)(Z3.Context('main'), Z3.setParam); // note additional argument and then invoke
You can add more type imports to the scope used for type checking by adding them to this line. TypeScript is pretty good about type inference, though, so you should only rarely need to explicitly refer to a type (other than in function parameters, which it doesn't infer); you still get the benefits of type checking. Though you might want to explicitly refer to types just for documentation purposes. Feel free to ping me about issues like this; I'm not necessarily going to be able to help but I'm happy to take a look. |
await solver.check(); // sat | ||
``` | ||
|
||
TODO: seems that we can only get the final output, not intermediate ones |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log doesn't seem to work
// AstVector | ||
const solver = new Z3.Solver(); | ||
|
||
// @ts-ignore: skipping typechecking for the following line for now, is there a better solution? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cast?
Thanks for joining the conversation.
The issue here is that type annotations are enforced unless you do
The build would fail at |
website/src/eval-z3/eval-z3.ts
Outdated
import type { init as initT, Model, Solver } from 'z3-solver'; | ||
declare let init: typeof initT; | ||
declare let Z3: Awaited<ReturnType<typeof init>>; | ||
declare let { Context, setParam } = Z3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bakkot I'm not sure I'm on the right track, so definitely would appreciate your help here. The example failed to be compiled is
// non-linear arithmetic
Z3.setParam('pp.decimal', true);
Z3.setParam('pp.decimal_precision', 20);
const x = Z3.Real.const('x');
const y = Z3.Real.const('y');
const z = Z3.Real.const('z');
const solver = new Z3.Solver();
solver.add(x.mul(x).add(y.mul(y)).eq(1)); // x^2 + y^2 == 1
solver.add(x.mul(x).mul(x).add(z.mul(z).mul(z)).lt('1/2')); // x^3 + z^3 < 1/2
await solver.check(); // sat
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, adding declare let { Context, setParam } = Z3
should make the typechecker happy, and then including the change I suggested above should make the actual runtime behavior work. (Except it will be called as setParam
, not Z3.setParam
.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, that's not legal syntax. Needs to be what it currently is, just with setParam
also: declare let { Context, setParam }: Awaited<ReturnType<typeof init>>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I messed up with the types in this commit. After further understanding the high-level API the issue is now fixed. The imports should be
declare let { Context, setParam }: Awaited<ReturnType<typeof init>>;
declare let Z3: ReturnType<typeof Context<'main'>>;
simply add setParam
to the destructure of init()
Also, if you want to make the // @ts-ignore
import type * as Z3 from 'z3-solver'; to the imports. (The You'll need to also increment the index (currently 4) in this line so the rewriting still works. |
I'll let @NikolajBjorner decide which style of type names is desired in the examples. |
All problems in the extra examples fixed, ready to merge. |
🎉 This PR is included in version 1.0.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
🎉 This PR is included in version 1.0.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
I expect the build to fail for some examples because the way I constructed them does not work with Kevin's solution for TS inputs. Main problems:
solve()
andprove()
in the unit test without bypassing type checking the function parameterssetParam
doesn't quite workZ3
namespace.@NikolajBjorner I should leave it to your judgement in terms of how to utilize/modify these examples so that everything works, or modify the
eval-z3.js
code as needed.