1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-01 00:04:58 +03:00
Commit Graph

322 Commits

Author SHA1 Message Date
Jonathan Cubides
e2f2d0a2f4
Fix Makefile target bugs for formatting and type Checking Juvix files (#2057)
This PR resolves a few bugs in the Makefile targets for formatting and
type checking Juvix files, which were preventing the capture of type
checking errors for our examples and bad formatting for all the Juvix
files in the repository. With this PR, our code should now be clean, and
we can expect every file to be properly formatted and type checked.

Changes made:

- [x] Updated `make format-juvix-files`
- [x] Updated `make check-format-juvix-files`
- [x] Formatted all Juvix files
- [x] Comment a fragment in `examples/milestone/Bank/Bank.juvix`

In the future, we will drastically simplify the Makefile once we improve
the `format` and the `type check` command for example posted here:

- #2066 
- #2087 

Related:

- #2063 
- #2040 (due to some typechecking errors we're not capturing before)
- #2105
- https://github.com/anoma/juvix/issues/2059
2023-05-19 17:33:56 +02:00
Łukasz Czajka
fe78ff451c
Direct translation from normalized JuvixCore to VampIR (#2086)
* Closes #2034.
* Adds the `vampir` target to the `compile` command.
* Adds two tests which are not yet enabled because `vamp-ir` is not
available in the CI (these and more tests will be enabled in #2103).
2023-05-19 14:43:45 +02:00
Jan Mas Rovira
9b1011b8ad
Add syntax for Judoc blocks (#2102)
- Closes #2050 

This pr adds the possibility to give judoc documentation in blocks
delimited by `{--` and `--}`.
- Inside these blocks, normal comments are disabled.
- It is allowed to have multiple blocks associated with the same
identifier, e.g.
```
{-- an axiom --}
{-- of type ;Type; --}
axiom a : Type;
```
- Nested blocks are *not* allowed.
- Blocks can be empty: `{-- --}`.
- The formatter respects line breaks inside blocks.
- The formatter normalizes whitespace at both ends of the block to a
single whitespace.
2023-05-19 14:05:32 +02:00
Jan Mas Rovira
d9d72f98eb
Simplify formatting of lambdas with a single clause (#2105)
After this pr, lambdas with a single clause will be printed in one line,
without a preceding `|`. I.e. `\ {x := x}`.
2023-05-19 13:24:02 +02:00
Paul Cadman
ad3607f104
Fix extra whitespace in text rendering of indented empty lines (#2101)
The prettyprinter library takes care avoid adding whitespace to empty
lines when it is rendering indented text.

See:

7e32c010ec/prettyprinter/src/Prettyprinter/Internal.hs (L1999)

However it only does this for unannotated text.

In our code we were stripping annotations from renderings within
`toTextStream` but we must remove the annotations before calling
`layoutPretty` to get the proper handling of whitespace with
indentations. That's what this PR does.
2023-05-19 11:41:28 +02:00
Jan Mas Rovira
a164083b70
Modify open import syntax (#2098) 2023-05-17 11:08:48 +02:00
Jan Mas Rovira
d135f74cf8
Add dangling judoc error (#2099) 2023-05-16 15:39:43 +02:00
Jan Mas Rovira
3ed30dd210
Implement core transformation let-hoisting (#2076)
- Closes #2033 
- Based on #2032
2023-05-16 13:42:44 +02:00
Łukasz Czajka
336a934d18
Normalization by Evaluation (#2038)
* Closes #2032.
* Adds the `juvix dev core normalize` command.
* Adds the `:n` command in JuvixCore REPL.
* Adds the `--normalize` flag to `juvix dev core read` and `juvix dev
core from-concrete`.
* Adds `pipeline-normalize` which denotes pipeline steps necessary
before normalization.
* Adds normalization tests in `tests/VampIR/positive/Core`.
2023-05-15 18:01:40 +02:00
Łukasz Czajka
8aa54ecc28
Inlining (#2036)
* Closes #1989
* Adds optimization phases to the pipline (specified by
`opt-phase-eval`, `opt-phase-exec` and `opt-phase-geb` transformations).
* Adds the `-O` option to the `compile` command to specify the
optimization level.
* Functions can be declared for inlining with the `inline` pragma:
   ```
   {-# inline: true #-}
   const : {A B : Type} -> A -> B -> A;
   const x _ := x;
   ```
By default, the function is inlined only if it's fully applied. One can
specify that a function (partially) applied to at least `n` explicit
arguments should be inlined.
   ```
   {-# inline: 2 #-}
   compose : {A B C : Type} -> (B -> C) -> (A -> B) -> A -> C;
   compose f g x := f (g x);
   ```
Then `compose f g` will be inlined, even though it's not fully applied.
But `compose f` won't be inlined.
* Non-recursive fully applied functions are automatically inlined if the
height of the body term does not exceed the inlining depth limit, which
can be specified with the `--inline` option to the `compile` command.
* The pragma `inline: false` disables automatic inlining on a
per-function basis.
2023-05-15 17:27:05 +02:00
Jan Mas Rovira
3cbf6f5cb6
Fix ordering of statements in Abstract -> Internal (#2040)
- Closes #2039 
- Closes #2055 
- Depends on #2053 

Changes in this pr:
- Local modules are removed (flattened) in the translation abstract ->
internal.
- In the translation abstract -> internal we group definitions in
mutually recursive blocks. These blocks can contain function definitions
and type definitions. Previously we only handled functions.
- The translation of Internal has been enhanced to handle these mutually
recursive blocks.
- Some improvements the pretty printer for internal (e.g. we now print
builtin tags properly).
- A "hack" that puts the builtin bool definition at the beginning of a
module if present. This was the easiest way to handle the implicit
dependency of the builtin stringToNat with bool in the internal-to-core
translation.
- A moderately sized test defining a simple lambda calculus involving
and an evaluator for it. This example showcases mutually recursive types
in juvix.

---------

Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-05-15 13:02:09 +02:00
Paul Cadman
8eb4c64f5d
Improve error message when input path doesn't exist (#2092)
Previously if you call Juvix on a file that doesn't exist you get the
error:

```
$ juvix compile /i/don't/exist.juvix
juvix: /i/dont: changeWorkingDirectory: does not exist (No such file or directory)
```

After this change you will see:

```
$ juvix compile /i/don't/exist.juvix
The input path "/i/dont/exist.juvix" does not exist
```
2023-05-15 11:03:09 +02:00
Jan Mas Rovira
2b0668b55a
Allow nested block comments (#2075)
- Closes #2070
2023-05-15 09:57:23 +02:00
Łukasz Czajka
0462d623f2
Fix reachability analysis with imports in unreachable nested modules (#2062)
* Closes #2005

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2023-05-12 09:53:30 +01:00
Paul Cadman
11ebc4acde
Fix format on projects that contain subprojects (#2078)
This PR fixes the behaviour of the `format` command when run on a
project that contains subprojects. Files in subprojects must not be
processed by the formatter.

The format issue was caused by a bug in the `walkDirRel` function that
is used to traverse a file system tree:


9a64184253/src/Juvix/Data/Effect/Files.hs (L36)

In this function, the passed handler can return a function that
determines if a candidate subdirectory should be traversed. The first
argument of this function indicates if the candidate subdirectory
contains a juvix.yaml file. In the formatter and the path resolver we
use this argument to exclude such subdirectories from the traversal.

Previously the first argument was calculated from the files in the
current directory instead of the candidate subdirectory - which was the
source of the bug.

The callers of walkDirRel are also fixed to match the updated behaviour.

* Closes https://github.com/anoma/juvix/issues/2077
2023-05-10 18:34:53 +01:00
Jan Mas Rovira
9a64184253
Arity check types and functions (->) (#2049)
This pr fixes the following:

This example causes the compiler to crash with "implicitness mismatch".
```
f : id Bool -> Bool;
f _ := false;
```
The reason is that `id` expects an implicit argument but finds `Bool`,
which is explicit. The arity checker was not inserting any hole because
it was ignoring the whole type. Moreover the aritychecker was never
checking `->` types as we never expected to
have to insert holes in `->` types (since the only fragment of defined
functions that we accept in types are those which do not have implicit
arguments).

We now properly arity check all types and process the function type `->`
correctly.
2023-05-10 13:07:03 +01:00
Jan Mas Rovira
b0c566f52d
Partial incremental highlighting (#2053) 2023-05-10 13:09:48 +02:00
Veronika Romashkina
4e1fee1c03
Fix: topCommandInputFile for Format command (#2063)
This change, instead of changing the `FormatOptions` data type to have
some `AppPath *`, it just adds the special case on how to handle this
specific command to figure out its input directory.

- Fixes #2058
2023-05-10 08:52:23 +02:00
Jonathan Cubides
8f850c2e5b
Fix a bug in the positivity checker (#1943) (#2054)
# Description

This PR fixes #1943. The primary issue stemmed from an incorrect
insertion in the set designated for storing negative type parameters.
Additionally, there was a call site intended to use the variable `typ`,
but I mistakenly used `ty` (which was for something else). To prevent
such silly typos better to adopt more meaningful names.
2023-05-08 20:13:26 +02:00
Jonathan Cubides
c2d8fa44bc Bump version to v0.3.3 🎉 2023-05-08 12:52:11 +02:00
Paul Cadman
6894300e5a
Support module imports in Juvix REPL (#2029)
This PR adds support for importing modules from within a Juvix project
in the Juvix REPL.

The imported module is checked (parsed, arity-checked, type-checked etc)
as normal and added to the REPL session scope. Any errors during the
checking phase is reported to the user.

### Notes:

* You must load a file before using `import`. This is because the REPL
needs to know which Juvix project is active.
* You may only import modules from within the same Juvix project. 

### Examples

After launching `juvix repl`:

#### `open import`

```
Stdlib.Prelude> open import Stdlib.Data.Int.Ord
Stdlib.Prelude> 1 == 1
true
```

#### `import as`

```
Stdlib.Prelude> import Stdlib.Data.Int.Ord as Int
Stdlib.Prelude> 1 Int.== 1
true
```

#### `import`then `open`

```
Stdlib.Prelude> import Stdlib.Data.Int.Ord as Int
Stdlib.Prelude> open Int
Stdlib.Prelude> 1 == 1
true
```

#### Line-terminating semicolons are ignored:

```
Stdlib.Prelude> import Stdlib.Data.Int.Ord as Int;;;;;
Stdlib.Prelude> 1 Int.== 1
true
```

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

---------

Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-05-08 12:23:15 +02:00
Paul Cadman
70f27fcede
Update to latest stdlib (#2048)
This PR updates the juvix-stdlib submodule. In particular the update
contains the new traits implemented in
https://github.com/anoma/juvix-stdlib/pull/54.

---------

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2023-05-04 17:49:10 +02:00
Paul Cadman
cdc2d5fd2e
Fix HasExpressions ConstructorApp (#2044)
Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2023-05-03 11:24:39 +02:00
Veronika Romashkina
935f6fbfb9
Fix fromSource behaviour when both stdin and filename (#2043)
# Description

While working on highlight command, I noticed that after `format`
command changes the behaviour of the `highlight` command when both
`--stdin` option specified and FILENAME is provided is changed.
Instead on returning the result for the `stdin` input, it started to
return empty result.

This PR fixes that behaviour, it will now act exactly as before
introducing the changes that came with the `format` command fixes.

## Type of change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)

# Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works:
  - [x] smoke test
2023-05-02 11:09:56 +02:00
Veronika Romashkina
8ab4ccd73b
Make format command's filepath optional (#2028)
# Description

No the filepath in the `juvix forma` command is n=made optional.
However, in that case, the `--stdin` command is required.

### Implementation details

~For now, as a quick solution, I have introduce the "fake" path that is
used for `fomat` command with stdin option.~
I needed to do a couple of big changes:
* `format` command FILE is now optional, howvere, I check that in case
of `Nothing` `--stdin` option should be present, otherwise it will fail
 * `entryPointModulePaths` is now `[]` instead of `NonEmpty`
* `ScopeEff` now has `ScopeStdin` constructor as well, which would take
the input from stdin instead of having path passed around
* `RunPipelineNoFileEither` is added to the `App` with the bunch of
`*Stdin` functions that doesn't require filepath argument to be passed

Fixes #2008

## Type of change

- [x] New feature (non-breaking change which adds functionality)

# Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works:
  - [x] smoke tests

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
Co-authored-by: janmasrovira <janmasrovira@gmail.com>
2023-04-27 17:33:08 +02:00
janmasrovira
2017bc0504
Add bank example (#2037)
This PR adds the Juvix program for the `Bank` example provided in the
[Leo
workshop
repository](https://github.com/AleoHQ/workshop/blob/master/basic_bank/src/main.leo).
2023-04-27 10:56:31 +02:00
Łukasz Czajka
c31e373c88
Add: pragma support (#1997)
* Closes #1965 
* Implements the `unroll` pragma to control the unrolling depth on a
per-function basis.
* Implements parsing of the `inline` pragma.

---------

Co-authored-by: janmasrovira <janmasrovira@gmail.com>
2023-04-26 15:26:13 +02:00
janmasrovira
528eaa72d0
Substitute calls after lambda lifting (#2031)
- Closes #2006

During lambda lifting, we now substitute the calls to the lifted
functions before recursively applying lambda lifting. This will slightly
increase the amount of captured variables. However, I think this is the
only way since we need all identifiers to have a type when recursing.
2023-04-26 12:56:44 +02:00
janmasrovira
9ab91f3890
Improve formatting of comments (#2022)
- Closes #2016 
This pr is a work in progress towards respecting empty lines with
respect to comments.

While the algorithm is supposed to work in general, I've focused on
testing it on comments between statements.

See
13442eee34/tests/positive/Format.juvix
to see some examples
2023-04-24 16:46:05 +02:00
Paul Cadman
f61e612c54
Fix dependencies suggestion in missing module error (#2024)
If an import statement to a missing module occurs when parsing in a
project with no dependencies the error message has the following form:

```
The module Foo does not exist.
It should be in /Users/paul/heliax/juvix-2023/tests/negative/NoDependencies/Foo.juvix
or in one of the dependencies:
```

This PR changes this error message to the `or in one of the
dependencies:` line is omitted from the error message when there are no
dependencies in the project.

This commit also adds a negative parse error test for missing module.
2023-04-22 18:15:38 +01:00
Paul Cadman
d59fca6786
Add juvix clean to remove project build artifact directory (#2018)
This PR adds the `juvix clean` command to the CLI that removes the Juvix
project build directory.

It respects the `--internal-build-dir` global option:

```
$ juvix compile Foo.juvix --internal-build-dir /tmp/build
$ juvix clean --internal-build-dir /tmp/build
```

In addition this PR fixes the `juvix format` program brief description
string. This was too long for the `juvix --help` display. The longer
description is now only displayed when `juvix format --help` is run.

* Closes https://github.com/anoma/juvix/issues/2017
2023-04-21 14:21:31 +02:00
Veronika Romashkina
89d74228d1
Add format stdin smoke tests (#2019)
* Exploration of #2008 

As I were trying to fix the behaviour of `format` command with the
`--stdin` global option, I noticed that actually it is already behaves
the same was as `dev scope` command (which was the desire at least at
this point).

So, no actual behaviour of the command is channged, only a few tests
added to showcase some edge cases of the command with the `--stdin`
option together.

Here are the highlights:

* The filename is **not** optional for both `dev scope` and `format`
even if with `stdin` the content of the file comes from the input.
* `format` command expects the stdin input to be a proper module
* the name of the stdin module should be aligned with the provided
filename even though its sources are not used

I think that some of the above behaviours we can consider to change. Let
me know what do you think about any of that.

Otherwise, the `dev scope` command can be replaced with `format`.
2023-04-21 13:35:21 +02:00
Paul Cadman
c4d519ae09
Support positive arity typealias in arity checker (#2021)
Currently the arity checker assumes that applications within a type
signature have arity unit. This is not the case where a type alias
function is used within a type signature that returns a positive arity
function type.

For example:

```
type T :=
  | t : T;

funAlias : Type -> Type;
funAlias a := a -> a;

f : funAlias T;
f t := t;
```

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

Co-authored-by: Co-authored-by: janmasrovira <janmasrovira@gmail.com>
2023-04-20 11:07:37 +01:00
janmasrovira
3d012cc8fb
Support more paths (#2000)
- Closes #1993 

This pr makes it possible to use `~`, `..` and environment variables in
the `juvix.yaml` and all flags / input of the cli.

In the CLI, the shell will be responsible for replacing environment
variables with their value, so the usual syntax can be used. For the
`dependencies` field, I have implemented a parser that has some
restrictions:
1. Environment variables are given with the makefile-like syntax
`$(VAR)`
2. The three characters `$` `(` `)` are reserved for the environment
variables syntax.
    They cannot be part of the path.
3. `~` is reserved for `$(HOME)`. I.e. the prepath `~~` will expand to
`$HOME$HOME`.
4. Nested environment variables are not allowed.

Thanks @paulcadman for the feedback. I think we are ready to merge this
nightmarish pr 👻

---------

Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-04-19 15:56:48 +01:00
Paul Cadman
c55f23f704
format long lambda clause body on a new line (#2015)
This PR modifies the pretty printer for lambda clauses so that the body
will start on a new line if it is too long to fit on a single line. This
is exactly how we handle function clause bodies.

* Closes https://github.com/anoma/juvix/issues/2014
2023-04-18 18:04:46 +01:00
Jonathan Cubides
a9277945e4 Bump version to 0.3.2 🎉 2023-04-18 14:56:48 +02:00
Paul Cadman
2c98319cd1
Preserve escaped " in String literals (#2011)
* Closes https://github.com/anoma/juvix/issues/2010

Co-authored-by: janmasrovira <janmasrovira@gmail.com>
2023-04-18 13:17:33 +01:00
Łukasz Czajka
46360a753c
Add: the 'seq' builtin (>>>) (#1982)
* Closes #1913 
* Closes #1976
2023-04-14 19:24:58 +02:00
janmasrovira
15b29bca69
Qualified imports (#2004) 2023-04-14 17:37:23 +02:00
Paul Cadman
e1a23c677c
Write compile output file to invoke dir by default (#1999)
This PR fixes the issue below and also adds smoke tests for the target
option of the compile command.

* Closes https://github.com/anoma/juvix/issues/1998
2023-04-13 19:57:49 +02:00
Jonathan Cubides
0fcf22f693
Fix: add supported targets as option for compile commands (#1983)
- Closes #1882
2023-04-13 14:16:07 +02:00
janmasrovira
5de0026d83
Add juvix global project under xdg directory and other improvements (#1963)
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-04-13 11:27:39 +02:00
Paul Cadman
ea09ec3068
Add builtin integer type to the surface language (#1948)
This PR adds a builtin integer type to the surface language that is
compiled to the backend integer type.

## Inductive definition

The `Int` type is defined in the standard library as:

```
builtin int
type Int :=
  | --- ofNat n represents the integer n
    ofNat : Nat -> Int
  | --- negSuc n represents the integer -(n + 1)
    negSuc : Nat -> Int;
```

## New builtin functions defined in the standard library

```
intToString : Int -> String;
+ : Int -> Int -> Int;
neg : Int -> Int;
* : Int -> Int -> Int;
- : Int -> Int -> Int;
div : Int -> Int -> Int;
mod : Int -> Int -> Int;

== : Int -> Int -> Bool;
<= : Int -> Int -> Bool;
< : Int -> Int -> Bool;
```

Additional builtins required in the definition of the other builtins:

```
negNat : Nat -> Int;
intSubNat : Nat -> Nat -> Int;
nonNeg : Int -> Bool;
```

## REPL types of literals

In the REPL, non-negative integer literals have the inferred type `Nat`,
negative integer literals have the inferred type `Int`.

```
Stdlib.Prelude> :t 1
Nat
Stdlib.Prelude> :t -1
Int
:t let x : Int := 1 in x
Int
```

## The standard library Prelude

The definitions of `*`, `+`, `div` and `mod` are not exported from the
standard library prelude as these would conflict with the definitions
from `Stdlib.Data.Nat`.

Stdlib.Prelude
```
open import Stdlib.Data.Int hiding {+;*;div;mod} public;
```

* Closes https://github.com/anoma/juvix/issues/1679
* Closes https://github.com/anoma/juvix/issues/1984

---------

Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
2023-04-13 08:16:49 +01:00
Łukasz Czajka
34b0969141
Pretty print JuvixCore values consistently with Juvix syntax (#1988)
This PR implements pretty printing of evaluation results consistently
with Juvix syntax. The printed values do not necessarily originate
directly from the source code. All functions/lambdas are printed as
`<fun>`.

The same mechanism is used to implement pretty printing of unmatched
pattern examples.

Juvix REPL now uses the new value printing mechanism to display
evaluation results. Typing `nil` in the REPL will now just display
`nil`. The command `juvix dev repl` still prints raw JuvixCore terms.

* Closes #1957 
* Closes #1985
2023-04-12 12:52:40 +02:00
Jonathan Cubides
4d0267ebb9
Fix: format juvix files in test/positive (#1978)
This PR fixes a formatting issue that drops blank lines between axiom
declarations.

It goes after:

- #1980
- Closes https://github.com/anoma/juvix/issues/1986
2023-04-12 10:07:01 +02:00
Jonathan Cubides
7431d808a5
Fix: pprint positive kw for data types (#1980)
As the title says.

- I found this bug while formatting the examples found in the tests
folder.

- In addition to printing the missing positive' keyword for data types,
the code also prints certain keyword annotations onto separate lines,
the ones that act as attributes to their term. While this is a matter of
personal preference, I find that it makes it easier to comment and
uncomment individual annotations.
2023-04-05 16:40:46 +02:00
Łukasz Czajka
15fe8bf01b
Add FoldTypeSynonyms Transformation to Geb Pipeline and more Geb Tests (#1956)
* Adds tests for recursive lets
* Adds more tests for pattern matching
* Adds the `FoldTypeSynonyms` transformation to the Geb pipeline, which
fixes a bug with type synonyms in Core-to-Geb
2023-04-05 10:16:04 +02:00
Łukasz Czajka
dbd24f2f93
Check for the executable (WASM/native) pipeline prerequisites (#1970)
* Closes #1959
2023-04-04 11:58:36 +02:00
Łukasz Czajka
63a2c5144d
Print quoted strings in the runtime (#1969)
* Closes #1968
2023-04-04 10:29:02 +01:00
Paul Cadman
9e1e72d6b6
repl: Run disambiguateNames on result node (#1961)
There were two bugs here:

1. The _replNoDisambiguate option was set to True for `juvix repl`.
2. The disambiguateNames pass was being applied after the result node
had already been fetched from the infotable.

* Closes https://github.com/anoma/juvix/issues/1958
2023-04-03 10:58:08 +02:00