1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-28 01:52:57 +03:00
juvix/test/Anoma/Compilation.hs

9 lines
233 B
Haskell
Raw Normal View History

Support compilation to Anoma compatible functions (#2652) This PR adds a `anoma` target to the `juvix compile`. This target compiles a Juvix `main` function to a Nockma/Anoma "function". Unlike the native, wasm, and nockma targets the main function may have any type signature. ## Anoma calling convention [Anoma calls functions](https://github.com/anoma/anoma/blob/6a4e15fe9c356225034f38445ce0eace2b43ab5e/lib/anoma/resource.ex#L122) by evaluating the formula `[call L replace [RL args] @ S]` against a subject equal to the function. Here `args` is a Nockma term that evaluates to a tuple of arguments that should be passed to the function. The anoma target compiles the `main` function to Nockma in the same way as the nockma target. The main function is then [wrapped](https://github.com/anoma/juvix/blob/9a658465aebe687565ec4394f965983bb66295a1/src/Juvix/Compiler/Nockma/Translation/FromTree.hs#L627) to make it compatible with the Anoma calling convention. ## Testing The anoma calling convention is [unit tested](https://github.com/anoma/juvix/blob/9a658465aebe687565ec4394f965983bb66295a1/test/Nockma/Eval/Positive.hs#L117) and [smoke tested](https://github.com/anoma/juvix/blob/9a658465aebe687565ec4394f965983bb66295a1/tests/smoke/Commands/compile.smoke.yaml#L159). This PR also adds versions of the end-to-end compilation tests. Most tests are included, tests for builtin IO operations and string builtins are omitted. Other tests that use Strings have been adapted to use other types that are compatible with this backend. ## Nockma REPL To facilitate testing the Nockma REPL can now load a nockma file as an initial subject. --------- Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
2024-02-23 15:54:22 +03:00
module Anoma.Compilation where
Support compilation of Anoma transactions in nockma backend (#2693) When we first implemented the Nockma backend we wrongly assumed that the only entry point for Juvix compiled Nockma modules would be the main function. Using this assumption we could add a setup step in the main function that put the Anoma stdlib and compiled functions from the Juvix module in a static place in the Nockma subject. References to the Anoma stdlib and functions in the module could then be resolved statically. However, one of the use cases for Juvix -> Nockma compilation is for Anoma to run logic functions that are fields of a transaction. So the user writes a Juvix program with main function that returns a transaction. The result of the main function is passed to Anoma. When Anoma calls the logic function on a field of the transaction, the setup part of the main function is not run so the subject is not in the required state. In fact, the logic function is not even callable by Anoma because non-main functions in the Juvix module use a calling convention that assumes the subject has a particular shape. This PR solves the problem by making all functions in the Juvix module use the Anoma calling convention. We make all compiled closures (including, for example, the logic functions stored on resources in a transaction) self contained, i.e they contain the functions library and anoma standard library. Modules that contain many closures produce large nockma output files which slows down the evaluator. This will need to be fixed in the future either with Nockma compression ([jam serialization](https://developers.urbit.org/reference/hoon/stdlib/2p)) or otherwise. But it does not block the compilation and execution of Anoma transactions. Other fixes / additions: * Extra tracing. You can now annotate output cells with a tag that will be displayed in the output * Unittests for listToTuple, appendRights helper functions * Fixes for the nockma parser when parsing 'pretty nockma', specifically stdlib calls, tags and functions_library atom. * Adds `juvix dev nock run` command that can run a program output with the `anoma` target. * Remove the `nockma` target. As described above we always use the Anoma calling convention so there's no need for a separate target for the 'juvix calling convention' * Adds a `--profile` flag to `juvix dev nock run` which outputs a count of Nockma ops used in the evaluation * In tests we no longer serialise the compiled program to force full evaluation of the compiled code. We added a negative test to check that strings are not allowed in Nockma/Anoma programs, it is output in a file `OUTPUT.profile` and has the following form: ``` quote : 15077 apply : 0 isCell : 0 suc : 0 = : 4517 if : 5086 seq : 5086 push : 0 call : 4896 replace : 1 hint : 8 scry : 0 trace : 0 ``` --------- Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-03-23 01:03:38 +03:00
import Anoma.Compilation.Negative qualified as N
Support compilation to Anoma compatible functions (#2652) This PR adds a `anoma` target to the `juvix compile`. This target compiles a Juvix `main` function to a Nockma/Anoma "function". Unlike the native, wasm, and nockma targets the main function may have any type signature. ## Anoma calling convention [Anoma calls functions](https://github.com/anoma/anoma/blob/6a4e15fe9c356225034f38445ce0eace2b43ab5e/lib/anoma/resource.ex#L122) by evaluating the formula `[call L replace [RL args] @ S]` against a subject equal to the function. Here `args` is a Nockma term that evaluates to a tuple of arguments that should be passed to the function. The anoma target compiles the `main` function to Nockma in the same way as the nockma target. The main function is then [wrapped](https://github.com/anoma/juvix/blob/9a658465aebe687565ec4394f965983bb66295a1/src/Juvix/Compiler/Nockma/Translation/FromTree.hs#L627) to make it compatible with the Anoma calling convention. ## Testing The anoma calling convention is [unit tested](https://github.com/anoma/juvix/blob/9a658465aebe687565ec4394f965983bb66295a1/test/Nockma/Eval/Positive.hs#L117) and [smoke tested](https://github.com/anoma/juvix/blob/9a658465aebe687565ec4394f965983bb66295a1/tests/smoke/Commands/compile.smoke.yaml#L159). This PR also adds versions of the end-to-end compilation tests. Most tests are included, tests for builtin IO operations and string builtins are omitted. Other tests that use Strings have been adapted to use other types that are compatible with this backend. ## Nockma REPL To facilitate testing the Nockma REPL can now load a nockma file as an initial subject. --------- Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
2024-02-23 15:54:22 +03:00
import Anoma.Compilation.Positive qualified as P
import Base
allTests :: TestTree
Support compilation of Anoma transactions in nockma backend (#2693) When we first implemented the Nockma backend we wrongly assumed that the only entry point for Juvix compiled Nockma modules would be the main function. Using this assumption we could add a setup step in the main function that put the Anoma stdlib and compiled functions from the Juvix module in a static place in the Nockma subject. References to the Anoma stdlib and functions in the module could then be resolved statically. However, one of the use cases for Juvix -> Nockma compilation is for Anoma to run logic functions that are fields of a transaction. So the user writes a Juvix program with main function that returns a transaction. The result of the main function is passed to Anoma. When Anoma calls the logic function on a field of the transaction, the setup part of the main function is not run so the subject is not in the required state. In fact, the logic function is not even callable by Anoma because non-main functions in the Juvix module use a calling convention that assumes the subject has a particular shape. This PR solves the problem by making all functions in the Juvix module use the Anoma calling convention. We make all compiled closures (including, for example, the logic functions stored on resources in a transaction) self contained, i.e they contain the functions library and anoma standard library. Modules that contain many closures produce large nockma output files which slows down the evaluator. This will need to be fixed in the future either with Nockma compression ([jam serialization](https://developers.urbit.org/reference/hoon/stdlib/2p)) or otherwise. But it does not block the compilation and execution of Anoma transactions. Other fixes / additions: * Extra tracing. You can now annotate output cells with a tag that will be displayed in the output * Unittests for listToTuple, appendRights helper functions * Fixes for the nockma parser when parsing 'pretty nockma', specifically stdlib calls, tags and functions_library atom. * Adds `juvix dev nock run` command that can run a program output with the `anoma` target. * Remove the `nockma` target. As described above we always use the Anoma calling convention so there's no need for a separate target for the 'juvix calling convention' * Adds a `--profile` flag to `juvix dev nock run` which outputs a count of Nockma ops used in the evaluation * In tests we no longer serialise the compiled program to force full evaluation of the compiled code. We added a negative test to check that strings are not allowed in Nockma/Anoma programs, it is output in a file `OUTPUT.profile` and has the following form: ``` quote : 15077 apply : 0 isCell : 0 suc : 0 = : 4517 if : 5086 seq : 5086 push : 0 call : 4896 replace : 1 hint : 8 scry : 0 trace : 0 ``` --------- Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-03-23 01:03:38 +03:00
allTests = testGroup "Compilation to Anoma" [P.allTests, N.allTests]