Commit Graph

205 Commits

Author SHA1 Message Date
Louis Gesbert
8e91dcb281 Apply suggestions from code review
Thanks @vincent-botbol

Co-authored-by: vbot <vincent.botbol@inria.fr>
2024-08-28 18:12:33 +02:00
Louis Gesbert
9fa5f91e3a Python printer: add some parens to be safe
I can't find where the line cut triggering the error at
https://github.com/CatalaLang/catala/actions/runs/10304111306/job/28522272547?pr=666
came from:
```
/home/ocaml/french-law/_python_venv/lib/python3.12/site-packages/french_law/Aides_logement.py:27785:
error: invalid syntax  [syntax]
```

the file at this point contains:
```
    def traitement_aide_finale_montee_en_charge_saint_pierre_miquelon1(
        aide_finale4:Money):
→       traitement_aide_finale_montee_en_charge_saint_pierre_miquelon4 =
            handle_exceptions(
                [],
                []
            )
```

This workaround adds parens after `=`, which ensures the syntax will be correct.
2024-08-28 18:12:33 +02:00
Louis Gesbert
5d61963a93 Reformat 2024-08-28 18:12:32 +02:00
Louis Gesbert
e9abbf9bd8 Scalc change: switch only on variables
matches can bind, but switches cannot, so we can assume the switch argument
should always be bound to a name ; this allow the intermediate variable to be
better renamed.
2024-08-28 18:12:32 +02:00
Louis Gesbert
14a378a33d Translation to scalc: fix renaming in blocks
Statements are often flattened, in which case their idents need to be
conflict-free. We pass along the renaming context to handle this.
2024-08-28 18:12:32 +02:00
Louis Gesbert
081e07378a Renaming: move to its own module 2024-08-28 18:12:28 +02:00
Louis Gesbert
1b6da0b572 reformat (renaming in scalc) 2024-08-28 18:10:41 +02:00
Louis Gesbert
1230f787d6 Renaming: use in the scalc translation and in Python 2024-08-28 18:10:36 +02:00
Louis Gesbert
b9156bb60e Implement safe renaming of idents for backend printing
Previously we had some heuristics in the backends trying to achieve this with a
lot of holes ; this should be much more solid, relying on `Bindlib` to do the
correct renamings.

**Note1**: it's not plugged into the backends other than OCaml at the moment.

**Note2**: the related, obsolete heuristics haven't been cleaned out yet

**Note3**: we conservatively suppose a single namespace at the moment. This is
required for e.g. Python, but it forces vars named like struct fields to be
renamed, which is more verbose in e.g. OCaml. The renaming engine could be
improved to support different namespaces, with a way to select how to route the
different kinds of identifiers into them.

Similarly, customisation for what needs to be uppercase or lowercase is not
available yet.

**Note4**: besides excluding keywords, we should also be careful to exclude (or
namespace):
- the idents used in the runtime (e.g. `o_add_int_int`)
- the dynamically generated idents (e.g. `embed_*`)

**Note5**: module names themselves aren't handled yet. The reason is that they
must be discoverable by the user, and even need to match the filenames, etc. In
other words, imagine that `Mod` is a keyword in the target language. You can't
rename a module called `Mod` to `Mod1` without knowing the whole module context,
because that would destroy the mapping for a module already called `Mod1`.

A reliable solution would be to translate all module names to e.g.
`CatalaModule_*`, which we can assume will never conflict with any built-in, and
forbid idents starting with that prefix. We may also want to restrict their
names to ASCII ? Currently we use a projection, but what if I have two modules
called `Là` and `La` ?
2024-08-28 17:18:26 +02:00
Louis Gesbert
98a460e1b3 Remove the R backend
There are no users at the moment, so we won't be actively maintaining it.

If and when the need arises again, we can revert this commit and resurrect it.
2024-07-08 14:52:41 +02:00
Louis Gesbert
0b19130c1d Fix the Python printer 2024-07-08 14:52:41 +02:00
Louis Gesbert
7e97c19901 Cleanup leftover commented code bits 2024-07-08 10:12:42 +02:00
Louis Gesbert
a5278244ec Debugging monomorphisation 2024-07-04 15:08:13 +02:00
Louis Gesbert
03e74b505d Cleanup 2024-07-04 15:08:13 +02:00
Louis Gesbert
88f5e932c8 Remove RaiseEmpty and CatchEmpty from the AST 2024-07-04 15:08:13 +02:00
Louis Gesbert
293bcd3817 Replace HandleDefault* internal operators by HandleExceptions
HandleExceptions only takes an array of exceptions, and returns Some if only one
of them is Some, None if they are all None, or raises a conflict error
otherwise.

The compilation of default terms then wraps this in a match (for the result of
HandleExceptions), and an if-then-else (for the justification-consequence in the
None case).

This avoids the complexity of having to handle thunked functions as arguments.
2024-07-04 15:08:13 +02:00
Louis Gesbert
c3b978bef8 Remove the now unused HandleDefault operator
(we now only need HandleDefaultOpt)
2024-07-04 15:08:13 +02:00
Louis Gesbert
583e80993a Remove the with-exceptions backend
*Disclaimer*: This is intended for discussion

My impression is that the with-exceptions backend is to be superseded by the
without-exception backend, which is more general and more efficient. Therefore,
seeing the added complexity of maintaining the two in parallel, I see no good
reason to keep the with-exceptions version now that the equivalence of their
semantics have been proved.

It will also be nice to reduce divergences between the different backends ; and
this should make further simplifications possible (e.g. some thunkings may no
longer be needed)

Of course I am ready to hear arguments in favor of keeping it, be it in the mid-
or long-term.

This patch removes the `--avoid-exceptions` flag, making it the only option, and
the corresponding `with_exceptions` variant of the dcalc->lcalc translation. It
doesn't do further simplifications.
2024-07-04 15:08:13 +02:00
Louis Gesbert
c0ad0e8820 Closure conversion: recursive hoisting 2024-07-04 15:08:13 +02:00
Denis Merigoux
5da46ff7be
Remove extra space 2024-06-03 09:06:57 +02:00
Louis Gesbert
4436d50011 C backend: To/FromClosureEnv are no-ops
Let them pass through.
2024-05-31 16:23:26 +02:00
Louis Gesbert
4acf321309 C backend: make error raising more concise 2024-05-30 16:59:55 +02:00
Louis Gesbert
eea30381a2 C backend: use switches for matches 2024-05-30 16:19:55 +02:00
Louis Gesbert
f04e889173 Pass the "external module" info along passes 2024-05-28 11:43:50 +02:00
Louis Gesbert
403156b36e Computation and checking of module hashes
This includes a few separate changes:

- pass visibility information of declarations (depending on wether the
  declaration was in a ```catala-metadata block or not)

- add reasonable hash computation functions to discriminate the interfaces. In
  particular:
  * Uids have a `hash` function that depends on their string, but not on their
    actual uid (which is not stable between runs of the compiler) ; the existing
    `hash` function and its uses have been renamed to `id`.
  * The `Hash` module provides the tools to properly combine hashes, etc. While
    we rely on `Hashtbl.hash` for the atoms, we take care not to use it on any
    recursive structure (it relies on a bounded traversal).

- insert the hashes in the artefacts, and properly check and report those (for
  OCaml)

**Remains to do**:

- Record and check the hashes in the other backends

- Provide a way to get stable inline-test outputs in the presence of module
  hashes

- Provide a way to write external modules that don't break at every Catala
  update.
2024-05-28 11:43:50 +02:00
Louis Gesbert
cee8e57d02 More precise positions for operators throughout 2024-04-30 16:35:08 +02:00
Louis Gesbert
959bcb9ccd Remove obsolete "except" type from the interpreter 2024-04-29 13:42:40 +02:00
Louis Gesbert
9d07015864 Unify runtime error handling
- Clearly distinguish Exceptions from Errors. The only catchable exception
  available in our AST is `EmptyError`, so the corresponding nodes are made less
  generic, and a node `FatalError` is added

- Runtime errors are defined as a specific type in the OCaml runtime, with a
  carrier exception and printing functions. These are used throughout, and
  consistently by the interpreter. They always carry a position, that can be
  converted to be printed with the fancy compiler location printer, or in a
  simpler way from the backends.

- All operators that might be subject to an error take a position as argument,
  in order to print an informative message without relying on backtraces from
  the backend
2024-04-26 18:31:26 +02:00
Louis Gesbert
97d007f1e7 Rename EmptyError to Empty
It's not an error! It happens in the normal control flow :)

This is to distinguish from the other runtime exceptions which are actually
fatal errors.
2024-04-25 14:39:15 +02:00
Louis Gesbert
090faf8c9f Fix statement ordering in scalc 2024-04-23 16:01:23 +02:00
Louis Gesbert
20288bcb26 Protect the interpreter against exceptions from custom code 2024-04-15 14:13:33 +02:00
Louis Gesbert
98fc97a241 Rewriting message calls to use the new intf 2024-04-10 19:26:23 +02:00
Louis Gesbert
8ae9701a4f Fix python name clash printing again, for non-ascii 2024-04-05 15:25:46 +02:00
Louis Gesbert
f71db385d5 Python backend: workaround func/var name clash 2024-04-05 14:45:17 +02:00
Louis Gesbert
619cafebb8 Reformat 2024-03-20 14:41:06 +01:00
Louis Gesbert
4cec981f62 Move global options of Cli to their own module
This resolves a dependency cycle that would forbid `Cli` from using the modue
`File`, which was annoying.
2024-03-19 15:18:35 +01:00
Louis Gesbert
dc6bfae75c Fixes for linking modules in the backends (in particular, Python) 2024-03-08 17:36:00 +01:00
Denis Merigoux
c2c46c431e
Adress @altgr's suggestion 2024-03-04 13:30:25 +01:00
Denis Merigoux
ad5dd6f2f6 Fix R compilation 2024-03-03 18:36:50 +01:00
Louis Gesbert
ba9fc85b84 Make exception output consistent across the interpreters 2024-02-26 14:56:43 +01:00
Louis Gesbert
97ae62384e Add externals to scalc, working test with Python backend 2024-02-26 14:56:43 +01:00
Louis Gesbert
589833bca7 Make plugin load failures less verbose
In general you don't care unless you may have attempted to use one.
2024-02-26 14:56:43 +01:00
Louis Gesbert
e308ff8d02 Generalise the definition of lists of nested binders 2024-02-09 18:33:41 +01:00
Louis Gesbert
e8e112d7f7 Add tuples to the Python backend 2024-01-31 11:02:03 +01:00
Louis Gesbert
5d233f4532 Fix warning 2024-01-29 11:37:42 +01:00
Denis Merigoux
b48fb0e223
Array compilation to C is OK 2024-01-26 20:15:32 +01:00
Denis Merigoux
3e807ae071
Remove warnings 2024-01-26 17:53:49 +01:00
Denis Merigoux
a39b67bf12
Merge branch 'master' into c_backend 2024-01-26 17:43:07 +01:00
Louis Gesbert
bc90a7b890 Add (internally) a map2 operator 2024-01-26 11:22:12 +01:00
Denis Merigoux
8a139f6a3c
Work on arrays 2024-01-22 16:49:58 +01:00