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

Direct translation of JuvixCore to VampIR #2034

Closed
lukaszcz opened this issue Apr 26, 2023 · 2 comments · Fixed by #2086
Closed

Direct translation of JuvixCore to VampIR #2034

lukaszcz opened this issue Apr 26, 2023 · 2 comments · Fixed by #2086
Assignees
Labels
enhancement New feature or request pipeline:vampir
Milestone

Comments

@lukaszcz
Copy link
Collaborator

lukaszcz commented Apr 26, 2023

Implement a direct translation of JuvixCore to VampIR.

It can be assumed that we're translating a single main function of type Int -> ... -> Int -> Int which has the form \x1 ... \xn -> b where the body b can contain only:

  • the variables x1,...,xn,
  • integer constants,
  • fully applied arithmetic operations and comparisons (OpInt* and OpEq from BuiltinOp in Core),
  • if-then-else, i.e., case expressions on booleans,
  • boolean constructors (with tags TagTrue and TagFalse),
  • fully applied failure nodes (OpFail),
  • let bindings of type Int at the top only (these should be translated to VampIR local definitions; see also Let hoisting #2033).

In particular, b doesn't contain lambdas, identifiers or inductive types other than booleans.

Ultimately, we should support arbitrary finite first-order data types in the type of the main function, with some fixed encoding to tuples of integers/field elements. It is easy to support Bool in addition to Int.

A quick and generally incorrect way of translating the failure nodes is to replace them with 0. The resulting VampIR programs will compile because Int is the only type. They will work correctly as long as the recursion limit is not reached. Perhaps this should be done at first. A proper way is to encode them with pairs (see B.7 in the Juvix-to-VampIR pipeline report).

Implementing booleans in VampIR is explained in the VampIR book (section Boolean logic). If-then-else with both branches integers can be implemented as:

def if b x y = b * x + (1 - b) * y;

Equality checking can be implemented as:

def isZero x = {
  def xi = fresh (1 | x);
  x * (1 - xi * x) = 0;
  1 - xi * x
};
def equal x y = isZero (x - y);

I'm not sure how we should implement less-than, but we can ignore it for now.

References

@lukaszcz lukaszcz added enhancement New feature or request pipeline:vampir labels Apr 26, 2023
@lukaszcz lukaszcz added this to the 0.4 - Prague milestone Apr 26, 2023
@mariari
Copy link
Member

mariari commented Apr 26, 2023

I'm not sure how we should implement less-than, but we can ignore it for now.

you can do it by doing bitwise decomposition of numbers. It's a pain, but you'll need to fix the number of bits.

https://github.com/anoma/VampIR-Book/blob/main/src/section_4_3.md

is an example, We do 32 bits in GEB

@lukaszcz
Copy link
Collaborator Author

Thanks! For natural numbers I guess just checking a \ b = 0 would be better. Ultimately, I think the field should be exposed directly as a separate type Field with its own operations, and integers should be a different type Int (or Int32) with less-than comparisons based on bitwise decomposition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pipeline:vampir
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants