1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-02 10:47:32 +03:00
Commit Graph

496 Commits

Author SHA1 Message Date
Łukasz Czajka
5f803ec6c2
Named arguments for record projections (#3173)
* Closes #3054
* Adds named argument signatures for generated record projections.
2024-11-15 18:25:49 +01:00
Łukasz Czajka
9f25ffde16
Detect termination for nested local definitions (#3169)
* Closes #3147 

When we call a function that is currently being defined (there may be
several such due to nested local definitions), we add a reflexive edge
in the call map instead of adding an edge from the most nested
definition. For example, for

```juvix
go {A B} (f : A -> B) : List A -> List B
  | nil := nil
  | (elem :: next) :=
    let var1 := f elem;
        var2 := go f next;
    in var1 :: var2;
```

we add an edge from `go` to the recursive call `go f next`, instead of
adding an edge from `var2` to `go f next` as before.

This makes the above type-check.

The following still doesn't type-check, because `next'` is not a
subpattern of the clause pattern of `go`. But this is a less pressing
problem.

```juvix
go {A B} (f : A -> B) : List A -> List B
  | nil := nil
  | (elem :: next) :=
    let var1 := f elem;
        var2 (next' : List A) : List B := go f next';
    in myCons var1 (var2 next);
```
2024-11-15 12:30:18 +01:00
Jan Mas Rovira
1d7bf1f25b
Fix compiler error on import cycles (#3171)
- Fixes #3161 

The strongly connected components given in [this
function](https://hackage.haskell.org/package/containers-0.7/docs/Data-Graph.html#v:stronglyConnComp)
are not guaranteed to give a cycle in the order they are given. I've
fixed that
2024-11-15 09:41:02 +01:00
Paul Cadman
49c14be71e
Fix nock compilation and builtin evaluation of sha256 stdilb (#3175)
This PR:

1. Fixes the compilation of the sha256 builtin anoma lib call

A sha256 hash is 32 bytes long, not 64 bytes. This number is used when
constructing the ByteArray representation (i.e `[length payload]` cell)
of the output of Anoma stdlib sha256 call. The Anoma stdlib sha256 call
just returns the atom payload.

2. Fixes the evaluation of the sha256 stdlib call

Previously we were converting the sha256 hash bytestring to Base16
format. This is convenient when displaying the ByteString hash. However
the Anoma nock interpreter outputs the raw bytes so we must change the
builtin evaluator to match this behaviour.

After these fixes we can re-enable the test084 anoma compilation test.
2024-11-14 18:54:39 +01:00
Paul Cadman
29041dcb32
Launch the Anoma node and client from the elixir REPL (#3172)
This PR changes how we launch the Anoma Client to avoid a bug with
linking cryptographic APIs.

libsodium cryptographic APIs like sign-detached cannot currently be
called from within the Anoma node or client binaries. Until this is
solved we must start both the node and client from the elixir REPL.
Previously we were starting the node using the REPL and the client using
the binary.

This commit changes the `start.exs` script we were using to start the
node to now start both a node and a client.

After this change we can enable Anoma compilation test `test077`.

The output of `juvix dev anoma node --anoma-dir ANOMA_DIR` is now:

```
Anoma node and client successfully started
Listening on port 51748
```
2024-11-14 09:10:00 +01:00
Jan Mas Rovira
200daad1dc
Fix bug in ComputeTypeInfo for let (#3158)
- Fixes #3030 

The `ComputeTypeInfo` transformation was incorrectly assuming that the
type of the body couldn't refer to the let item. When inferring the type
of a let, we now inline the let item(s) into the type of the body.

```
NLet Let {..} ->
   let bodyTy = Info.getNodeType _letBody
   in subst (_letItem ^. letItemValue) bodyTy
```
2024-11-12 16:44:25 +01:00
Jan Mas Rovira
ca56b6b0cd
Support traces in the anoma node (#3152)
This pr adds support for getting traces from the anoma node.
I've updated the test suite so that tests that were disabled because of
traces are now being run.
There are a few tests that require atention:
1. `test028`: Gives the wrong answer.
2. `test084`: Gives the wrong answer.
4. `test074`: Expected to fail because it uses scry.
5. `test086`: Expected to fail because Anoma representation of prngs is
different than the juvix representation.
2024-11-08 11:54:17 +00:00
Paul Cadman
6ed5e136b6
Add option to run Anoma tests in non-debug mode (#3144)
> ⚠️ Based on https://github.com/anoma/juvix/pull/3142

When using AnomaTestModeFull the compilation tests are run in debug
mode, with stdlib interception and run using the raw nock (without
stdlib interception).

---------

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-11-07 13:11:41 +00:00
Paul Cadman
2a463a00a2
Fix StdlibRandomNextBytes call (#3150)
The hoon code that generates the stdlib call is:


1a8b632463/src/Juvix/Compiler/Nockma/AnomaLib.hs (L101)

i.e the random number generator is the first argument and the width is
the second argument. We have the arguments transposed in the
corresponding Juvix builtin API so the call was failing.

This PR transposes the argumetns in the stdlib call so the builtin API
and hoon generated nock code are compatible.
2024-11-07 09:05:13 +01:00
Jan Mas Rovira
4cdcb2f747
Add anoma nockma tests (#3134)
* Fixes a bug in calling Anoma stdlib from Nock code
* Runs the anoma compilation test with the anoma node nockma evaluator.

I've classified the tests in 4 categories:
1. `Working`. The test works as expected.
2. `Trace`. We need more work on our end to get the traces from the
anoma node and check that they match the expected result.
3. `NodeError`. The anoma node returns `failed to prove the nock
program`.
4. `Wrong`. The anoma node returns some value that does not match the
expected value.

---------

Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
Co-authored-by: Łukasz Czajka <62751+lukaszcz@users.noreply.github.com>
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-11-05 13:28:28 +00:00
Łukasz Czajka
3030196fdd Non-recursive definitions (#3138)
* Closes #2968 
* Implements detection of function-like definitions, which either:
  - have some arguments on the left of `:`, or
  - have at least one clause.
* Only function-like definitions are recursive.
* Non-recursive definitions are not mutually recursive either, and can
be used only after their definition. This necessitates rearranging some
definitions in existing Juvix code.
* Changes the scoping of identifiers in record updates. Now field names
on the right side don't refer to the old values of the record fields but
to identifiers in scope defined outside the record update. To refer to
old values, one needs to explicitly use record projections, e.g.
```
r@Rec{x := Rec.x r}
```
2024-11-04 18:18:39 +01:00
Jan Mas Rovira
71161ffecd
Fix package-base interaction (#3139)
- Fixes #3009 
- Fixes #2877
- TODO think if this makes https://github.com/anoma/juvix/issues/2985
slightly easier to fix
2024-11-01 14:42:18 +00:00
Łukasz Czajka
95275ca5c1
Detect constant side conditions in matches (#3133)
* Closes #3007 
* Depends on #3101 
* Detects side conditions which are `true` (removes the condition) or
`false` (removes the branch).
2024-11-01 09:50:19 +00:00
Łukasz Czajka
68a79bc8a8
Detect redundant patterns (#3101)
* Closes #3008
* Implements the algorithm from [Luc Maranget, Warnings for Pattern
Matching](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/3165B75113781E2431E3856972940347/S0956796807006223a.pdf/warnings-for-pattern-matching.pdf)
to detect redundant patterns.
* Adds an option to the Core pretty printer to print match patterns in a
user-friendly format consistent with pattern syntax in Juvix frontend
language.
2024-10-30 11:38:22 +01:00
Paul Cadman
3b34f6e4ff
Support random API from the Anoma stdlib (#3129)
This PR adds frontend support for the Anoma Random API:

The frontend builtin APIs are:

```
builtin anoma-random-generator
axiom RandomGenerator : Type;

builtin anoma-random-generator-init
axiom randomGeneratorInit : Nat -> RandomGenerator;

builtin anoma-random-generator-split
axiom randomGeneratorSplit : RandomGenerator
  -> Pair RandomGenerator RandomGenerator;

builtin anoma-random-next-bytes
axiom randomNextBytes : Nat
  -> RandomGenerator
  -> Pair ByteArray RandomGenerator;
```

### Nockma Evaluator

The Nockma evaluator intercepts the corresponding Anoma random stdlib
calls using the
[System.Random](https://hackage.haskell.org/package/random-1.2.1.2/docs/System-Random.html)
API. The implementation uses the
[splitmix](https://hackage.haskell.org/package/splitmix-0.1.0.5/docs/System-Random-SplitMix.html)
generator directly because it has an API to destructure the generator
into a pair of integers. We can use this to serialise the generator.



* Closes https://github.com/anoma/juvix/issues/2902
2024-10-29 18:23:37 +00:00
Jan Mas Rovira
021f183d33
Run Nockma in an Anoma node (#3128)
# Changes
1. Adds a new command `juvix dev anoma node`. This command runs the
anoma node.
2. Adds a flag `--anoma-dir` to `juvix dev nockma run`. When given, it
must point to the anoma clone. Then, it will run the nockma code in the
anoma node and report the result (with no traces).

# Prerequisites
1. An anoma clone at some specific commit. 
   ```
   git clone git@github.com:anoma/anoma.git
   cd anoma
   git checkout 98e3660b91cd55f1d9424dcff9420425ae98f5f8
   
   # build anoma
   mix deps.get
   mix escript.install hex protobuf
   mix compile

   # build the client
   mix do --app anoma_client escript.build
   ```
2. The `mix` command (elixir).
3. The [`grpcurl`](https://github.com/fullstorydev/grpcurl) command. To
install a single binary in `~/.local/bin` you can run:
   ```
curl -sSL
"https://github.com/fullstorydev/grpcurl/releases/download/v1.9.1/grpcurl_1.9.1_linux_x86_64.tar.gz"
| tar -xz -C ~/.local/bin --no-wildcards grpcurl
   ```

# Testing
I've not included any test. It can be tested locally like this:
```
cd juvix/tests/Anoma/Compilation/positive
juvix compile anoma test001.juvix
echo 20 > args.debug.nockma
juvix dev nockma run --anoma-dir ~/projects/anoma test001.nockma --args args.debug.nockma
2024-10-29 17:32:59 +01:00
Paul Cadman
18cca89296
Add frontend support for Anoma Resource Machine builtins (#3113)
This PR adds frontend builtin support for the Anoma Resource machine
functions provided in
[resource-machine.hoon](4897751366/hoon/resource-machine.hoon),
*except* for the `prove-logic` function which still needs some
discussion.

Users must now mark the Anoma `Resource` type with
`builtin-anoma-resource` and the Anoma `Action` type with
`builtin-anoma-action`. This is required because the resource machine
functions use these types.

The compiler does not check that the constructors of `Resource` and
`Action` match the RM spec. I made this decision because the Anoma types
are sill in flux and it's easier to change if correctness is delegated
to the RM library for now. We can add the constructor checks when the
Anoma RM interface is stable.

The test file
[test085.juvix](47ba3e2746/tests/Anoma/Compilation/positive/test085.juvix)
demonstrates how each builtin should be used.

### Core Evaluator

The Core evaluator does not support these builtin functions in normal
mode. When used for normalisation (e.g when used in the constant folding
pass) the Core evaluator leaves the builtin functions unchanged.

### Nock Evaluator

The Nock evaluator does not intercept the Anoma lib functions that the
builtins correspond to in the Nock backend. It executes the underlying
Nock code instead. This means that several of the functions cannot be
tested because they're either too slow (e.g commitment) or do not have
an implementation in the Nock code (e.g addDelta).

* Closes: https://github.com/anoma/juvix/issues/3084
2024-10-22 13:10:08 +02:00
Paul Cadman
8fb5ae77ba
Rename Nockma stdlib to anomalib and add RM references (#3111)
Most changes in this PR relate to renaming of the Anoma Nock
StandardLibrary to AnomaLibrary. This is because the Anoma library now
consists of a standard library from
[anoma.hoon](a20b5e7838/hoon/anoma.hoon)
and the resource machine library
[resource-machine.hoon](a20b5e7838/hoon/anoma.hoon).

The Anoma RM functions and value references are also added. Their
integration into the compiler pipeline will happen in a separate PR.

The Anoma Library RM functions and stdlib functions are enumerated
separately. There is a separate type for Anoma Library values because
these are compiled differently than functions.

Part of:

* https://github.com/anoma/juvix/issues/3084
2024-10-21 13:28:37 +02:00
Łukasz Czajka
5d32e8f0b5
Fix closure representation in the Nock backend (#3105)
* Closes #3083 
* Closes #3042 

The representation of closures is changed to make it more efficient and
compatible with the Nock calling convention.
2024-10-18 18:49:34 +01:00
Paul Cadman
0a9ec8fb37
Add frontend support for Anoma stdlib sha256 (#3109)
This PR adds frontend support for Anoma stdlib sha256 function (aka
`shax` in Nock).

* Closes https://github.com/anoma/juvix/issues/2901

The new builtin can be declared as follows:

```
builtin anoma-sha256
axiom anomaSha256 : Nat -> ByteArray;
```

The intention is that it wraps a call to anomaEncode as follows:

```
sha256 {A} (a : A) : ByteArray := anomaSha256 (anomaEncode a);
```

### Fix for atom to ByteString

This PR also includes a commit
6205dc9ff9
to fix an issue with functions like `integerToByteArray` when called
with negative integers (the solution is to change the argument types to
Natural, as this is all we need for Anoma).
2024-10-17 19:11:26 +02:00
Łukasz Czajka
f1bb0e50d9
Remove VampIR compile command and tests (#3104)
* Closes #2841 
* Moves the `vampir` compilation target under `dev`.
* Removes VampIR tests that require the external `vamp-ir` executable.
2024-10-16 15:03:14 +02:00
Łukasz Czajka
c50ad06976
Compile-time configuration (#3102)
* Closes #3077 
* Closes #3100 
* Adds a compilation-time configuration script that creates a
`config/config.json` file which is then read by the
`Makefile`/`justfile` and embedded into the Juvix binary.
2024-10-16 11:47:23 +02:00
Paul Cadman
c499d0d7e1
Support Anoma stdlib curry function (#3097)
This PR adds support for the Anoma node stdlib function to the nockma
backend.

https://developers.urbit.org/reference/hoon/stdlib/2n#cury

This PR also changes the arguments placeholder value when compiling
functions and closures to make it a tuple of length equal to the
function/closure arity.

To use curry, the function argument's placeholder argument tuple must
have length equal to the function airty.

For example if we are compiling a function with arity 2, the compiled
nock function should have the form:

```
[compiled-code [0 0] env]
```
2024-10-11 16:53:06 +02:00
Łukasz Czajka
7760267bcd
Fix JuvixTree unification (#3087)
* Closes #3016 
* Fixes the `curryType` function
* Changes the behaviour of `unifyTypes` and `isSubtype` to always curry
first
2024-10-09 15:33:42 +02:00
Łukasz Czajka
a3bfaca7bb
Avoid duplication in Nockma code generation (#3070)
* Closes #3013 
* Removes all remaining problems with evaluation duplication that could
potentially lead to an exponential blow-up in the running time.
* Adds the capacity to generate saves of temporary values in the Nock
code generation backend.
* Removes the `TempHeight` transformation on JuvixTree. It is no longer
needed.
* Removes the `ComputeCaseANF` transformation on JuvixCore from the
Anoma pipeline. It is no longer necessary.
2024-10-08 10:05:20 +02:00
Łukasz Czajka
40b71b95de
Nockma backend: translate trace to %puts hints (#3053)
* Closes #3022 
* Requires https://github.com/anoma/anoma/pull/861
2024-10-07 14:01:01 +02:00
Jan Mas Rovira
358551995e
Fix named application bug (#3075)
- Fixes #3074

- Merge after #3076

---------

Co-authored-by: Łukasz Czajka <62751+lukaszcz@users.noreply.github.com>
2024-10-03 16:26:59 +01:00
Jan Mas Rovira
8c37d9b439
Merge typechecker negative tests (#3076) 2024-10-03 10:02:45 +02:00
Jan Mas Rovira
137b6d83eb
Fix termination crash due to empty permutation (#3081)
- Fixes #3064
2024-10-02 18:59:30 +02:00
Jan Mas Rovira
a1926547a2
Reimplement positivity checker (#3057)
- Fixes #3048
- Fixes #3058

Due to #3071 I had to change the order of two lines in
tests/Compilation/positive/test079.juvix.
2024-10-01 13:39:28 +02:00
Jan Mas Rovira
deca981fa3
Ignore files that start with a . (#3072)
- Closes #3068
2024-09-30 12:17:18 +02:00
Jan Mas Rovira
c09d10db02
Improve parsing error for missing @ in named application (#3012)
- Closes #2796 

Example:
```
module NamedApplicationMissingAt;

type T := t;

fun (a : T)
 : T := t;

main : T := fun {a := t};
```

The error displays as:

![image](https://github.com/user-attachments/assets/e36232cb-9ec3-462c-8ee4-8332924b4b07)
2024-09-20 18:00:38 +01:00
Łukasz Czajka
b609e1f6a5
Don't fold lets if the let-bound variable occurs under a lambda-abstraction (#3029)
* Closes #3002
2024-09-13 19:29:39 +02:00
Łukasz Czajka
56e2db7336
Fix JuvixTree parsing and pretty printing (#3024)
Recent changes to the compiler left JuvixTree parsing and pretty
printing not in sync with each other.
2024-09-12 14:37:51 +02:00
Łukasz Czajka
26ea94b977
The assert builtin (#3014)
* Requires #3015
2024-09-12 09:29:57 +02:00
Jan Mas Rovira
d8919087dd
Fix location of scoped modulePathName (#3011)
Closes #2737.

This issues caused the formatter to sometimes insert unwanted line
breaks.
2024-09-09 15:50:29 +02:00
Łukasz Czajka
ab2d31a313
Compilation of side conditions in pattern matches (#2984)
* Closes #2804 
* Requires #3003
* Front-end syntax for side conditions was implemented in #2852. This PR
implements compilation of side conditions.
* Adds side-conditions to `Match` nodes in Core. Updates Core parsing,
printing and the evaluator.
* Only side-conditions without an `else` branch are allowed in Core. If
there is an `else` branch, the side conditions are translated in
`fromInternal` into nested ifs. Because with `else` the conditions are
exhaustive, there are no implications for pattern exhaustiveness
checking.
* Adjusts the "wildcard row" case in the pattern matching compilation
algorithm to take into account the side conditions.
2024-09-09 12:25:15 +02:00
Jan Mas Rovira
4ae4e4e4d9
Fix a bug that prevented use of name signature defined after the point (#3001)
- Fixes #2999
2024-09-06 14:32:03 +02:00
Jan Mas Rovira
e45503a63e
Fix typechecking of default arguments in signatures with trait arguments (#2998)
- Fixes #2994
2024-09-05 19:43:04 +02:00
Łukasz Czajka
b9d864123a
Isabelle/HOL translation: comments (#2974)
* Closes #2962 
* Depends on #2963 
* In Isabelle/HOL comments cannot appear in internal syntax. All
comments inside a Juvix definition are moved outside: to before the
definition or before the earliest function clause.

---------

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-09-02 15:56:58 +02:00
Paul Cadman
87c5f0af44
Improve performance of anomaEncode / anomaDecode in the Core evaluator (#2975)
This PR:

* Adds a new implementation of {decode, encode}ByteString functions,
used by anomaEncode and anomaDecode in the Core evaluator
* Adds property tests for roundtripping and benchmarks for the new
functions.

The old implementation used
[bitvec](https://hackage.haskell.org/package/bitvec) to manipulate the
ByteString. This was far too slow. The new implementation uses bit
operations directly on the input integer and ByteArray.

It's now possible to run
[anoma-app-patterns:`Tests/Swap.juvix`](https://github.com/anoma/anoma-app-patterns/blob/feature/tests/Tests/Swap.juvix)
to completion.

For encoding, if the size of the output integer exceeds 64 bits (and
therefore a BigInt must be used) then the new implementation has
quadratic time complexity in the number of input bytes if an
implementation of `ByteString -> Integer` is used as follows:

```
byteStringToIntegerLE :: ByteString -> Integer
byteStringToIntegerLE = BS.foldr (\b acc -> acc `shiftL` 8 .|. fromIntegral b) 0
```

```
byteStringToInteger' :: ByteString -> Integer
byteStringToInteger' = BS.foldl' (\acc b -> acc `shiftL` 8 .|. fromIntegral b) 0

```

I think this is because `shiftL` is expensive for large Integers. To
mitigate this I'm splitting the input ByteString into 1024 byte chunks
and processing each separately. Using this we get 100x speed up at
~0.25Mb input over the non-chunked approach and linear time-complexity
thereafter.

## Benchmarks

The benchmarks for encoding and decoding 250000 bytes:

```
 ByteString Encoding to/from integer
      encode bytes to integer:   OK
        59.1 ms ± 5.3 ms
      decode bytes from integer: OK
        338  ms ±  16 ms
```

The previous implementation would never complete for this input.

Benchmarks for encoding and decoding 2 * 250000 bytes:

```
    ByteString Encoding to/from integer
      encode bytes to integer:   OK
        121  ms ± 8.3 ms
      decode bytes from integer: OK
        651  ms ±  27 ms
```

Benchmarks for encoding and decoding 4 * 250000 bytes:

```
    ByteString Encoding to/from integer
      encode bytes to integer:   OK
        249  ms ±  17 ms
      decode bytes from integer: OK
        1.317 s ±  16 ms
```

---------

Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
2024-08-30 18:20:18 +01:00
Łukasz Czajka
eb5b2e4595
Fix JuvixTree type unification (#2972)
* Closes #2954 
* The problem was that the type validation algorithm was too strict for
higher-order functions with a dynamic (unknown) target.
2024-08-27 10:31:14 +02:00
Paul Cadman
8d03ac2b6c
Add anoma-bytearray-{to, from}-anoma-contents builtins (#2960)
The `anoma-bytearray-{to, from}-anoma-contents` are intended to be used
to convert to/from atoms representing `ByteArrays`. These builtins are
required temporarily until Anoma Node makes ByteArray representation
uniform across all of its APIs.

We represent ByteArrays in nock as a cell:

```
[size contents]
```

Where `size` is the size of the ByteArray and `contents` is an Atom
representing the bytes in LSB ordering.

The `size` is required in general because the encoding of ByteArrays to
Atoms is ambiguous. For example the ByteArrays [0x01; 0x00] and [0x01]
are represented by `1`.

Some Anoma ByteArrays like keys and signatures are represented using on
the `contents` atom because the size is constant.

Users of Anoma APIs have to strip / add size information from ByteArrays
depending on where the data is used. The new builtins provide this
facility.

These builtins are temporary because it's been agreed with Anoma
engineering team to change the Anoma APIs to make the ByteArray
representation uniform, i.e always represent ByteArrays using `[size
content]`. When this is implemented in Anoma Node we can remove these
builtins.

```
builtin anoma-bytearray-to-anoma-contents
axiom toAnomaContents : ByteArray -> Nat;

builtin anoma-bytearray-from-anoma-contents
axiom fromAnomaContents :
  -- | The size of the ByteArray
  Nat
  -- | The contents of the ByteArray
  -> Nat
  -- | The resulting ByteArray
  -> ByteArray;
```
2024-08-19 11:19:26 +02:00
Jan Mas Rovira
b78279c3e0
Fix inference of let and letrec in core (#2953)
* Closes #2949

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-08-14 15:15:49 +01:00
Paul Cadman
d759d27da7
Use ByteArray for Anoma cryptographic builtins (#2947)
This PR adds support for ByteArray in the Anoma cryptographic functions.

```
builtin anoma-sign
axiom anomaSign : {A : Type} -> A -> ByteArray -> ByteArray;

builtin anoma-verify-with-message
axiom anomaVerifyWithMessage : {A : Type} -> ByteArray -> ByteArray -> Maybe A;

builtin anoma-sign-detached
axiom anomaSignDetached : {A : Type} -> A -> ByteArray -> ByteArray;

builtin anoma-verify-detached
axiom anomaVerifyDetached : {A : Type} -> ByteArray -> A -> ByteArray -> Bool;
```

The Anoma / Hoon Stdlib function `length` needs to be exposed as a
StdlibFunction because a ByteArray stores its length and the value
returned by `anomaSign` is not a fixed length.
2024-08-13 13:17:57 +01:00
Paul Cadman
ce5c2c5c55
Add builtin ByteArray type (#2933)
This PR adds support for a builtin `ByteArray` type and associated
functions for constructing a `ByteArray` from a list of bytes and a
function to query the size of the `ByteArray`. It is only available in
the Anoma backend.

In Core / Tree, ByteArray constant is stored using a Haskell ByteString.

In Anoma the ByteArray is stored as a cell where the head is the length
of the ByteArray and the tail is an integer is an integer formed by
concatenating the bytes in the array using little-endian byte ordering.

The Nock for constructing a `ByteArray` uses the `length`, `add`,
`folder` and `lsh` functions from the Anoma hoon stdlib. See the [code
comment](fa068a30e7/src/Juvix/Compiler/Nockma/StdlibFunction.hs (L37))
for more details.

Example:

```
module test082;

import Stdlib.Prelude open;
import Stdlib.Debug.Trace open;

builtin bytearray
axiom ByteArray : Type;

builtin bytearray-from-list-byte
axiom mkByteArray : List Byte -> ByteArray;

builtin bytearray-size
axiom size : ByteArray -> Nat;

bs0 : ByteArray := mkByteArray [];

bs1 : ByteArray := mkByteArray [0x0; 0x0; 0x0];

bs2 : ByteArray := mkByteArray [0x1; 0x0; 0x0; 0x0];

bs3 : ByteArray := mkByteArray [0x2; 0x1];

bs4 : ByteArray := mkByteArray [0x100];

main : ByteArray :=
  trace (size bs0)
   >-> trace bs0
   >-> trace (size bs1)
    >-> trace bs1
    >-> trace (size bs2)
    >-> trace bs2
    >-> trace (size bs3)
    >-> trace bs3
    >-> trace (size bs4)
    >-> bs4;
```

Output using `tests/Anoma/Compilation/positive/test082.juvix`

```
$ juvix compile anoma -g test082.juvix
$ juvix dev nockma run test082.pretty.nockma
0
[0 0]
3
[3 0]
4
[4 1]
2
[2 258]
1
[1 0]
```
2024-08-13 11:13:27 +01:00
Paul Cadman
e2fe830d28
Add support for unsigned 8-bit integer type Byte (#2918)
This PR adds `Byte` as a builtin with builtin functions for equality,
`byte-from-nat` and `byte-to-nat`. The standard library is updated to
include this definition with instances for `FromNatural`, `Show` and
`Eq` traits.

The `FromNatural` trait means that you can assign `Byte` values using
non-negative numeric literals.


You can use byte literals in jvc files by adding the u8 suffix to a
numeric value. For example, 1u8 represents a byte literal.

Arithmetic is not supported as the intention is for this type to be used
to construct ByteArrays of data where isn't not appropriate to modify
using arithmetic operations. We may add a separate `UInt8` type in the
future which supports arithmetic.

The Byte is supported in the native, rust and Anoma backend. Byte is not
supported in the Cairo backend because `byte-from-nat` cannot be
defined.

The primitive builtin ops for `Byte` are called `OpUInt8ToInt` and
`OpUInt8FromInt`, named because these ops work on integers and in future
we may reuse these for a separate unsigned 8-bit integer type that
supports arithmetic.

Part of:

* https://github.com/anoma/juvix/issues/2865
2024-08-02 07:43:24 +01:00
Jan Mas Rovira
3a9eb20f4a
Fix scanning of names with import prefix (#2929)
1. When the flatparse scanner fails and we fallback to megaparsec, a
warning is issued.
2. The flatparse scanner has been fixed so it is not confused when a
name starts with `import`.
2024-07-31 10:02:38 +02:00
Jan Mas Rovira
178bc5324f
Fix name signature bug and extend test for instance fields (#2928)
- Closes #2923 

This pr fixes a bug where all fields were assigned to be explicit
arguments in the NameSignature Builder. A single line change was enough
to fix it.
```diff
-           RecordStatementField RecordField {..} -> addSymbol @s Explicit Nothing _fieldName _fieldType
+           RecordStatementField RecordField {..} -> addSymbol @s (fromIsImplicitField _fieldIsImplicit) Nothing _fieldName _fieldType
```

I've also added a compilation test for instance fields.
2024-07-30 17:56:42 +02:00
Jan Mas Rovira
a5479d0718
Properly handle confluent imports (#2915)
- Fixes #2914
2024-07-23 19:56:30 +02:00