1
1
mirror of https://github.com/anoma/juvix.git synced 2024-11-30 14:13:27 +03:00
Commit Graph

659 Commits

Author SHA1 Message Date
Jan Mas Rovira
d38c1cac36 add positive test 2024-11-21 19:22:43 +01:00
Jan Mas Rovira
2e14594c6b negative test 2024-11-20 18:34:04 +01:00
Łukasz Czajka
2e38aa609f
Unify type signature declaration syntax (#3178)
* Closes #2990 
* Allows type signatures for constructors, record fields and axioms to
have the same form as in function definitions, e.g.,
```
field {A} {{M A}} (arg1 arg2 : A) : List A
```
* For constructors, this introduces an ambiguity between record and GADT
syntax with names. For example,
```
ctr {
  A : Type
}
```
can be confused by the parser with initial part of GADT type signature:
```
ctr {A : Type} : A -> C A
```
For now, this is resolved by preferring the record syntax. Hence, it's
currently not possible to use type signatures with implicit arguments
for constructors. Ultimately, the `@` in record syntax should be made
mandatory, which would resolve the ambiguity:
```
ctr@{
  A : Type
}
```

---------

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-11-20 15:57:15 +00:00
Paul Cadman
865842046d
Support running nockma code with a running Anoma client (#3180)
This PR:

1. Adds a new interpretation for the Anoma effect, which makes gRPC
calls to an existing Anoma client instead of spawning a new one.
2. Adds a new `nockma run` mode, `with-client`, which can be used to run
an Anoma program against a running Anoma client, using its URL and gRPC
port.
3. separates the `nockma run` command into subcommands.

CLI docs:

## `nockma run`

```
Usage: juvix dev nockma run COMMAND

  Subcommands used to run an Anoma program. Use with artefacts obtained from
  compilation with the anoma target

Available options:
  -h,--help                Show this help text

Available commands:
  builtin-evaluator        Run with the builtin Nockma evaluator
  ephemeral-client         Run with an ephemeral Anoma client
  with-client              Run with a running Anoma client
```

### `with-client`

```
Usage: juvix dev nockma run with-client
         NOCKMA_FILE [--args ARGS_FILE] (-p|--grpc-port PORT) [--url URL]

  Run with a running Anoma client

Available options:
  NOCKMA_FILE              Path to a .nockma file
  --args ARGS_FILE         Path to file containing args. The args file should
                           contain a list (i.e. to pass 2 and [1 4] as args, the
                           contents should be [2 [1 4] 0]).
  -p,--grpc-port PORT      The GRPC port of a running Anoma client
  --url URL                The URL of a running Anoma client. default: localhost
  -h,--help                Show this help text
```

### `ephemeral-client`

```
Usage: juvix dev nockma run ephemeral-client
         NOCKMA_FILE [--args ARGS_FILE] --anoma-dir ANOMA_DIR

  Run with an ephemeral Anoma client

Available options:
  NOCKMA_FILE              Path to a .nockma file
  --args ARGS_FILE         Path to file containing args. The args file should
                           contain a list (i.e. to pass 2 and [1 4] as args, the
                           contents should be [2 [1 4] 0]).
  --anoma-dir ANOMA_DIR    Path to anoma repository
  -h,--help                Show this help text
```

### `builtin-evaluator`

```
Usage: juvix dev nockma run builtin-evaluator
         NOCKMA_FILE [--args ARGS_FILE] [--profile]

  Run with the builtin Nockma evaluator

Available options:
  NOCKMA_FILE              Path to a .nockma file
  --args ARGS_FILE         Path to file containing args. The args file should
                           contain a list (i.e. to pass 2 and [1 4] as args, the
                           contents should be [2 [1 4] 0]).
  --profile                Report evaluator profiling statistics
  -h,--help                Show this help text
```
2024-11-20 08:53:21 +01:00
Ł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
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
Paul Cadman
bebe3d121d
Release 0.6.8 (#3165)
The purpose of this release is to add support for the `--vscode` flag,
required by an update to the juvix vscode extension.

* https://github.com/anoma/juvix/pull/3162
* https://github.com/anoma/vscode-juvix/pull/153

This PR updates:

- [x] Package version
- [x] Smoke test

The CHANGELOG generator we use:
https://github.com/github-changelog-generator/github-changelog-generator
has stopped working with errors like the following:

```
Warning: PR 3148 merge commit was not found in the release branch or tagged git history and no rebased SHA comment was found
```

So we'll defer populating the CHANGELOG summary until after the release.
2024-11-11 17:08:17 +01:00
Łukasz Czajka
76af2124b2
Don't write log messages to stdout (#3159)
The "Cloning (...)" message was written to stdout which messed up
markdown generation.
2024-11-11 14:31:37 +00:00
Paul Cadman
0714391900
Release 0.6.7 (#3153)
This PR updates:

- [x] Package version
- [x] Smoke test

The CHANGELOG generator we use:
https://github.com/github-changelog-generator/github-changelog-generator
has stopped working with errors like the following:

```
Warning: PR 3148 merge commit was not found in the release branch or tagged git history and no rebased SHA comment was found
```

So we'll defer populating the CHANGELOG summary until after the release
2024-11-07 09:50:18 +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
Ł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
Łukasz Czajka
c143259aee
Allow trailing semicolons everywhere (#3123)
* Closes #3039
* Closes #3043
* Closes #2970
* Closes #3089
* Parser allows trailing semicolons for any kind of semicolon-separated
items:
  - let-block statements,
  - module statements,
  - record declaration statements,
  - record update fields,
  - record pattern fields,
  - named application arguments,
  - list literal items,
  - list pattern items,
  - open statement using/hiding items,
  - `syntax iterator` declaration parameters,
  - `syntax fixity` declaration parameters.
* Formatter prints trailing semicolons if the items are displayed on
separate lines, removes them if on a single line.
* The formatting of multiline lists is changed to make it consistent
with other semicolon-separated blocks:
```
[
  1;
  2;
  3;
]
```
instead of
```
[ 1
; 2
; 3
]
```
2024-10-29 18:25:06 +01: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
Jan Mas Rovira
47696514dc
Set line width to 80 (#3124) 2024-10-25 15:11:19 +02:00
Łukasz Czajka
3cf79faafb
Formatter: add braces when the iterator body is not enclosed (#3122)
* Closes #3091
* Formatter adds braces when the body is not enclosed in braces or
parentheses. Braces-enclosed body is always printed as a block on a new
line:
```
for (acc := 0) (x in lst) {
  x + acc
}
```
* If the body is enclosed in ordinary parentheses, then they are
preserved and the iterator is printed on a single line, if possible:
```
for (acc := 0) (x in lst) (x + acc)
```
This is sometimes useful when you want iterator application as an
argument to something.
2024-10-25 11:42:01 +02:00
Łukasz Czajka
8f180ccfda
Improve Set and Map modules in the standard library (#3120)
* Updates the standard library to
https://github.com/anoma/juvix-stdlib/pull/130
* Also changes `null` to `isEmpty`, which required updating some tests

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-10-24 12:29:33 +01:00
Jan Mas Rovira
e951df077d
Don't put a space after the lambda keyword (#3121)
- Closes #3095
2024-10-23 16:02:56 +02:00
Paul Cadman
ae89c4d480
Serialize Nockma output using nock jam (#3066)
The Anoma API accepts jammed nock terms as input. The benefit to this is
that jammed terms are greatly compressed compared to the original term.

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

Remaining tasks:

- [x] Deserialize input nockma file in `juvix dev nockma {run, eval}`
- [x] Support debug input nockma file in `juvix dev nockma {run, eval,
repl}` i.e there should be a way to pass the `*.debug.nockma` (output of
`juvix compile anoma --debug`) file to `juvix dev nockma {run, eval,
repl}`
- [x] Add proper JuvixErrors for deserialisation failures

---------

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-10-23 09:02:32 +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
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
feb422d445
Allow @ in constructor declarations (#3099)
* Closes #3041 
* The old syntax without `@` is still accepted, but the formatter
changes it to the new syntax
2024-10-15 19:15:37 +02:00
Łukasz Czajka
83539148cb
Update standard library coding style according to the guidelines (#3092)
* Closes #3079
* Closes #3086
* Depends on #3088 
* Updates the coding style guidelines (CODING.md) to reflect issues not
foreseen originally
* Changes the unicode arrow printed in the REPL to `->`. This is to make
the output consistent with how function types are written in the
standard library.

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-10-14 15:56:54 +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
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
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
Paul Cadman
6e58aad8e7
Include the juvix version in the build directory path (#3069)
For example, by default the build directory would now be:

```
.juvix-build/0.6.6
```

It is necessary to separate the build files by compiler version because
the structure of jvo files may be incompatible between compiler
releases.

* Closes https://github.com/anoma/juvix/issues/3019
2024-09-30 08:57:47 +01: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
Jan Mas Rovira
9d9591617d
Remove old named application syntax (#3026)
- Closes #2948
2024-09-12 19:27:29 +02:00
Łukasz Czajka
26ea94b977
The assert builtin (#3014)
* Requires #3015
2024-09-12 09:29:57 +02:00
Jan Mas Rovira
8e204634b8
Fix the location in the parser for .juvix.md (#3020)
This pr makes it possible to properly hihglight .juvix.md files
2024-09-11 14:07:16 +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
Paul Cadman
e4559bbc87
Release 0.6.6 (#2993)
This PR updates:

- [x] Package version
- [x] Smoke test
- [x] Changelog
2024-09-03 18:10:01 +01: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
3d21ab4325
Monad and Applicative traits in juvix stdlib (#2979)
This PR updates the stdlib submodule to juvix-stdlib main branch.

It contains Monad and Applicative traits.
2024-08-30 15:07:29 +02:00
Łukasz Czajka
a4f3704f4e
Isabelle/HOL translation: records and named patterns (#2963)
* Closes #2894 
* Closes #2895
* The translation of pattern matching on records is a bit tricky because
one cannot pattern match on records in Isabelle, except in top patterns
of function clauses. We thus need to translate into nested pattern
matching and record projections. Named patterns can be translated with a
similar technique and are also handled in this PR.

Checklist
---------
- [x] record creation
- [x] record projections
- [x] record update
- [x] top-level record patterns
- [x] nested record patterns
- [x] named patterns
- [x] remove redundant pattern matching clauses
- [x] remove redundant single-branch pattern matches
2024-08-29 16:15:58 +02: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
Łukasz Czajka
9c980d152a
Translate Judoc comments to Isabelle/HOL (#2958)
* Closes #2891
2024-08-23 20:43:57 +02:00
Jan Mas Rovira
eb0922a244
Add do notation (#2937)
- Closes #2355
- Depends on #2943 

Example:
```
minusOne : Nat -> Maybe Nat
  | zero := nothing
  | (suc n) := just n;


minusThree (n : Nat) : Maybe Nat :=
  do {
    x1 <- minusOne n;
    x2 <- minusOne x1;
    let
      x2' : Nat := x2;
    in
    x3 <- minusOne x2';
    pure x3;
  };
```
2024-08-21 12:01:44 +02:00