Commit Graph

239 Commits

Author SHA1 Message Date
Denis Merigoux
21d4360120
Add verification condition special case for scope context arguments 2023-01-20 15:55:28 -05:00
Denis Merigoux
8405d243be
Fix compiler and tests 2023-01-20 14:10:18 -05:00
Denis Merigoux
7cffc53169
Merge branch 'master' into afromher_334 2023-01-20 14:05:38 -05:00
Louis Gesbert
f236e2cfb2 Replace the type conversion and rounding operators with overloads
Ref. #366

Also updates `CONTRIBUTING.md`.

This was pretty straight-forward :)
2022-12-13 15:32:49 +01:00
Louis Gesbert
fea01cfe4c Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.

The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions

The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-12-13 11:55:24 +01:00
Denis Merigoux
619461dba8
Merge branch 'master' into fix_362 2022-12-07 15:32:08 +01:00
Denis Merigoux
e9fd40dddd
Hotfix for CI 2022-12-06 18:11:40 +01:00
Denis Merigoux
e448a1a1b4
Fix 362 (was harder than expected and unit tests helped catch subsequent encoding bugs!) 2022-12-02 16:42:29 +01:00
Louis Gesbert
3f2aa19e97 Add ambiguous StructAccess for desugared
to be resolved in scopelang
2022-11-28 16:38:09 +01:00
Louis Gesbert
af2f5dbe19 Tweak error message location printing 2022-11-28 16:38:09 +01:00
Louis Gesbert
660e5775de Rename utils to catala_utils 2022-11-28 16:38:09 +01:00
Louis Gesbert
b329afbbdb Rename all Map/Set calls accordingly
This is just a bunch of `sed` calls:
```shell
sed -i 's/ScopeSet/ScopeName.Set/g' compiler/**/*.ml*
sed -i 's/ScopeMap/ScopeName.Map/g' compiler/**/*.ml*
sed -i 's/StructMap/StructName.Map/g' compiler/**/*.ml*
sed -i 's/StructSet/StructName.Set/g' compiler/**/*.ml*
sed -i 's/EnumMap/EnumName.Map/g' compiler/**/*.ml*
sed -i 's/EnumSet/EnumName.Set/g' compiler/**/*.ml*
sed -i 's/StructFieldName/StructField/g' compiler/**/*.ml*
sed -i 's/StructFieldMap/StructField.Map/g' compiler/**/*.ml*
sed -i 's/StructFieldSet/StructField.Set/g' compiler/**/*.ml*
sed -i 's/EnumConstructorMap/EnumConstructor.Map/g' compiler/**/*.ml*
sed -i 's/EnumConstructorSet/EnumConstructor.Set/g' compiler/**/*.ml*
sed -i 's/RuleMap/RuleName.Map/g' compiler/**/*.ml*
sed -i 's/RuleSet/RuleName.Set/g' compiler/**/*.ml*
sed -i 's/LabelMap/LabelName.Map/g' compiler/**/*.ml*
sed -i 's/LabelSet/LabelName.Set/g' compiler/**/*.ml*
sed -i 's/ScopeVarMap/ScopeVar.Map/g' compiler/**/*.ml*
sed -i 's/ScopeVarSet/ScopeVar.Set/g' compiler/**/*.ml*
sed -i 's/SubScopeNameMap/SubScopeName.Map/g' compiler/**/*.ml*
sed -i 's/SubScopeNameSet/SubScopeName.Set/g' compiler/**/*.ml*
```

... and reformat
2022-11-28 16:38:09 +01:00
Louis Gesbert
4ae392c900 AST refactoring
Many changes got bundled in here and would be too tedious to separate.

Closes #330

See changes in `shared_ast/definitions.ml` to check the main point.

- the biggest change is a modification of the struct and enum types in
  expressions: they are now stored as `Map`s throughout passes, and no longer
  converted to indexed lists after scopelang. Their accessors are also changed,
  and tuples only exist in Lcalc (they're used for closure conversion).

  This implied adding some more information in the contexts, to keep the mapping
  between struct fields and scope output variables. It should also be much more
  robust (no longer relying on assumptions upon different orderings).

- another very pervasive change is more cosmetic: the rewrite of the main AST to
  use inline records, labelling individual subfields.

- moved the checks for correct definitions and accesses of structures from
  `Scope_to_dcalc` to `Typing`

- defining some new shallow iterators in module `Shared_ast.Expr`, and
  factorising a few same-pass rewriting functions accordingly (closure
  conversion, optimisations, etc.)

- some smaller style improvements (ensuring we use the proper compare/equal
  functions instead of `=` in a few `when` closes, for example)
2022-11-17 18:16:09 +01:00
Aymeric Fromherz
1d0871e65c format 2022-11-16 22:28:20 +01:00
Aymeric Fromherz
cfba9d456a Merge branch 'afromher_verif' into afromher_334 2022-11-16 22:16:11 +01:00
Aymeric Fromherz
16c9bae810 cleanup unused var/module errors 2022-11-16 22:13:14 +01:00
Aymeric Fromherz
fe9ef4f8cb Remove map of free_vars_typ from VC generation 2022-11-16 22:08:07 +01:00
Aymeric Fromherz
5a5003b22d Do not pass free_vars to make_context 2022-11-16 21:59:48 +01:00
Aymeric Fromherz
0ccf7da89a Remove ctx_var from Z3 backend context 2022-11-16 21:57:26 +01:00
Aymeric Fromherz
1343f9e1f6 Leverage typed information embedded in expressions to remove uses of the ctx_var map 2022-11-16 21:55:31 +01:00
Aymeric Fromherz
5c19bdc0db Store typ in z3_vars map 2022-11-16 21:52:14 +01:00
Aymeric Fromherz
43fa3ba550 Start removing ctx_var 2022-11-16 21:36:21 +01:00
Aymeric Fromherz
4af44fb519 Add debug info about encoded assertions 2022-11-08 22:25:07 +01:00
Aymeric Fromherz
c4756c485c WIP: Encoding assertions to Z3 2022-11-08 22:09:35 +01:00
Aymeric Fromherz
b443174033 Better handling of assertions in conditions.ml 2022-11-08 22:09:21 +01:00
Aymeric Fromherz
fee9533b87 Add local asserts to each generated vc 2022-11-08 21:51:56 +01:00
Aymeric Fromherz
3c478148e1 Basic infrastructure for collecting assertions 2022-11-08 20:55:40 +01:00
Aymeric Fromherz
5f36bee94f Extend verification_condition with field for assertions in scope 2022-11-08 20:48:43 +01:00
Louis Gesbert
e925ec1795 Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).

This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)

---

Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).

Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.

Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)

This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing

The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.

`Expr.Box.inj` is provided to swap back to a box, before binding for example.

Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-07 18:00:23 +02:00
Aymeric Fromherz
7f07274c1f Ignore unfound (i.e., internal) variables during Z3 model printing 2022-09-13 15:54:26 +02:00
Aymeric Fromherz
fee64d6f6f format 2022-09-13 10:37:30 +02:00
Aymeric Fromherz
b7854eb6f3 Z3Backend: Use type information inside AST node for match translation 2022-09-13 10:33:02 +02:00
Aymeric Fromherz
ea13981c5d Match annotation type in io.mli and in Z3 backend 2022-09-12 18:10:02 +02:00
Aymeric Fromherz
0592cdab2a Z3encoding: Use auxiliary variable for encoding type-generic EMatch 2022-09-12 17:52:55 +02:00
Denis Merigoux
d7e219b0dd
Correct test output for proof mode 2022-09-06 14:10:32 +02:00
Denis Merigoux
84b994b521
Bug caught with proof mode 2022-09-05 17:35:44 +02:00
Raphaël Monat
973c346328 Add support for is_leap_year in z3 backend and runtime 2022-09-05 09:28:58 +02:00
Raphaël Monat
f60cfcc435 Merge branch 'master' of github.com:CatalaLang/catala into dates_calc_lib 2022-08-30 15:14:51 +02:00
Louis Gesbert
e10771c187
Make all supertypes use ('a, 't) gexpr as parameter instead of naked_gexpr 2022-08-29 10:57:21 +02:00
Louis Gesbert
a9c8bab2b3
Same treatment for typ and marked_typ 2022-08-29 10:57:21 +02:00
Louis Gesbert
0a23dc526d
Rename marked_expr -> expr, expr -> naked_expr throughout
Since the marked kind is used throughout, this should be more clear
2022-08-29 10:57:21 +02:00
Louis Gesbert
54eee2edea Rationalise the tuple / enum types
This will allow to unify with types used earlier in the
pipeline (`Scopelang.Ast.typ`).

It seems cleaner! But some areas may warrant a later clean-up, in particular
handling of options and their types in the backends, or possible name conflicts
of structs/enums with built-in types when printing.
2022-08-23 15:48:06 +02:00
Louis Gesbert
4caf828e48 Additional cleanup/fixes on the compiler refactoring
following review ^^
2022-08-23 00:13:02 +02:00
Louis Gesbert
576e0fb3ff Factorise AST printers
Note that there were significant differences between the two printers (see the test diff!). Overall the `dcalc` one seemed newer so that's what I took, with only the required additions from `lcalc` (exceptions, raise and catch)
2022-08-22 19:28:27 +02:00
Louis Gesbert
ae2801be6d Move mode handling code from dcalc to shared_ast
Handling code should now be reasonably well sorted between `Shared_ast.{Var,Expr,Scope,Program}`

The function parameters (e.g. `make_let_in`) could be removed from the
scope handling functions since now the types are compatible, which
makes them much easier to read.
2022-08-22 19:28:27 +02:00
Louis Gesbert
d02c02e352 Fix some warnings 2022-08-22 19:28:27 +02:00
Louis Gesbert
4bb49c14f1 Simplify some type aliases 2022-08-22 19:28:27 +02:00
Louis Gesbert
06dbab74d2 reformat 2022-08-22 19:28:27 +02:00
Louis Gesbert
2b6ee8dd4b Leverage the shared AST: big cleanup (part I) 2022-08-22 19:28:21 +02:00
Louis Gesbert
988e5eff1c Split the shared AST into a separate lib 2022-08-22 19:16:28 +02:00
Denis Merigoux
4455b8fb0c
Stub before correct replacement 2022-08-19 15:21:38 +02:00
Denis Merigoux
e5aed3efcb
Merge branch 'master' into rmonat_dates_calc_lib 2022-08-19 14:59:46 +02:00
Louis Gesbert
0b0e774d1c More factorisation, in particular for variables 2022-08-12 17:18:06 +02:00
Raphaël Monat
3e71f25bfe Post merge fixes on using dates_calc 2022-08-06 17:49:06 +02:00
Raphaël Monat
ffd2e1dec3 Merge branch 'master' into dates_calc_lib 2022-08-06 17:26:55 +02:00
Denis Merigoux
fa55a83fb4
Merge branch 'master' into 290-jsoo-wrapper-plugin 2022-07-22 17:54:51 +02:00
Emile Rolley
0c180e12f9 refactor(runtimes): add runtime_ocaml and runtime_jsoo in the catala package 2022-07-22 16:52:56 +02:00
Denis Merigoux
dc00ce8957
Merge branch 'master' into aides_logement 2022-07-22 16:49:01 +02:00
Denis Merigoux
0304602433 Trying to fix Bindlib unboxing bug [skip ci] 2022-07-22 12:31:05 +02:00
Denis Merigoux
dcf80f4bec
Adding first_day_of_month and last_day_of_month 2022-07-21 14:14:04 +02:00
Denis Merigoux
58cb48fb28
Merge remote-tracking branch 'origin/master' into aides_logement 2022-07-20 11:20:00 +02:00
Denis Merigoux
793efb4682
In the process of switching some money types to decimal [skip ci] 2022-07-19 13:53:12 +02:00
Louis Gesbert
97120c4dc2 compiler/verification: force a typed AST as input 2022-07-12 15:57:50 +02:00
Louis Gesbert
7485c7f2ce Reformat 2022-07-11 17:42:34 +02:00
Louis Gesbert
18e86621d5 Port verification code to the AST changes 2022-07-11 17:42:34 +02:00
Louis Gesbert
67179a793c Add type annotations on all AST nodes (first pass) 2022-07-11 16:51:54 +02:00
Louis Gesbert
513647cd32 AST simplification: remove redundant annotations
The AST structures track annotations (e.g., at the moment, source code
position information) in a lot of places. This patch tidies up a bit and
removes some duplication, ensuring a single level of annotation wrapping
at each AST recursion level.

This will be important when adding type information in these
annotations, because there will be consitency constraints to be ensured
and duplication is a likely source of mistakes.
2022-07-11 16:51:54 +02:00
Louis Gesbert
af0ac95682 Propagate renaming of Pos.mark* into module Marked
this patch is just a bunch of `sed` commands

```shell
cd compiler
sed -i 's/Pos.marked/Marked.pos/g' *.ml* **/*.ml*
sed -i 's/Pos.unmark/Marked.unmark/g' *.ml* **/*.ml*
sed -i 's/Pos\.get_position/Marked.get_mark/g' *.ml* **/*.ml*
sed -i 's/Pos\.same_pos_as/Marked.same_mark_as/g' *.ml* **/*.ml*
sed -i 's/Pos\.map_under_mark/Marked.map_under_mark/g' *.ml* **/*.ml*
sed -i 's/Pos\.mark/Marked.mark/g' *.ml* **/*.ml*
sed -i 's/Pos\.compare_marked/Marked.compare/g' *.ml* **/*.ml*
```
2022-07-11 16:51:54 +02:00
Louis Gesbert
f17875f90e Formatting: some other personal preferences 2022-05-11 16:25:49 +02:00
Louis Gesbert
74c5629153 Formatting: reduce extra match-case indentation
2 is plenty enough, esp. for nested matches :)
2022-05-11 16:25:20 +02:00
Louis Gesbert
fa3693d813 Formatting: disable deprecated "smart" fun-decl option 2022-05-11 16:22:56 +02:00
Denis Merigoux
c7b84ca2d0
Merge pull request #244 from CatalaLang/c_backend
Bringing structure to the Lcalc AST
2022-05-05 14:22:26 +02:00
Denis Merigoux
78e8275d5e
Adds a decimal rounding operator 2022-04-29 21:18:15 +02:00
Denis Merigoux
e7440e043c
Remove duplicated function 2022-04-12 17:07:00 +02:00
Denis Merigoux
b24dbc8360
Remove logging calls for vc generation 2022-04-12 11:14:39 +02:00
Denis Merigoux
a43f89a0f4
Parametrize Dcalc.scope_expr 2022-04-12 10:53:07 +02:00
Denis Merigoux
3f49824150
Merge branch 'master' into c_backend 2022-04-04 18:02:33 +02:00
Denis Merigoux
31e8f37a43
Sort VCs by alphabetical order 2022-04-04 17:51:41 +02:00
Denis Merigoux
7ca5ef283a
Code builds but bugguy [skip ci] 2022-04-04 08:56:48 +02:00
Denis Merigoux
158d49fe86
Removed unnecessary extra runtime function 2022-03-28 18:59:53 +02:00
Aymeric Fromherz
2b0206a5a8 Restrict duration z3 encoding to days only 2022-03-28 18:47:13 +02:00
Aymeric Fromherz
2c247128d1 [Z3encoding] Add support for duration literals 2022-03-24 17:22:31 +01:00
Aymeric Fromherz
8d2348d1d9 [Z3 encoding] Add support for Duration type and operators 2022-03-24 17:15:22 +01:00
Denis Merigoux
6722cf9647
Fix bug 2022-03-17 17:52:26 +01:00
Denis Merigoux
a7bdc0a114
Add condition for focusing the proof mode on a single scope 2022-03-17 17:44:24 +01:00
Denis Merigoux
cdac6de9fe
Implement the round money builtin 2022-03-17 12:30:14 +01:00
Aymeric Fromherz
97c0211bdc [Z3encoding] Support for unit literal 2022-03-16 12:01:21 +01:00
Aymeric Fromherz
b00d270df7 [Z3backend] Add support for EInj nodes 2022-03-16 12:00:19 +01:00
Aymeric Fromherz
e3f3704be9 comment typo 2022-03-16 11:35:13 +01:00
Aymeric Fromherz
148afda523 Counterexamples generation for arrays 2022-03-16 11:28:03 +01:00
Aymeric Fromherz
fb6c18763f Fix encoding of hypotheses into Z3 2022-03-16 11:20:20 +01:00
Aymeric Fromherz
f6ad6bbd2f Encode that an array length is always positive 2022-03-15 18:52:02 +01:00
Aymeric Fromherz
fb924c50e5 Encode the length of arrays into Z3 2022-03-15 18:43:11 +01:00
Aymeric Fromherz
4b1ace2739 [Z3Backend] Support GetYear equality comparison 2022-03-15 18:09:56 +01:00
Aymeric Fromherz
d760d883a6 [Z3encoding] Print variable name when encoding is not supported 2022-03-15 18:09:33 +01:00
Louis Gesbert
e7e89873db Make Z3 an optional dependency
If Catala is compiled without Z3, trying to run it with the backend `Proof` will
yield:
```
[ERROR] This instance of Catala was compiled without Z3 support.
```
and return 124

Note that this doesn't change the `make depends`, opam file or CI to account for it,
it just enables it at the build-system level.

There are also no hooks at this moment to have Catala self-document the options
whith which it was compiled (e.g. in the `--help` screen). But that could be
added in a more general way later, it's probably not really needed yet.
2022-03-08 18:38:42 +01:00
Denis Merigoux
5bd66142a6
Big reformatting
ocamlformat 0.19.0 -> 0.20.1
100 -> 80 columns per line
Reestablished @emilerolley's smart fun break
2022-03-08 15:03:14 +01:00
Louis Gesbert
12ec65601d Use format strings directly in debug/error/log functions
This avoids many intermediate calls to e.g. `Format.asprintf`; should result in
some cases in "more correct" use of `Format`¹, avoid the computation of unused
debug strings, and make the code more readable.

¹ for `Format` to work as expected, all intermediate calls need to go through
it. Some cases of formatting to an intermediate string then printing through Format
again are still present, but this makes the situation better.
2022-03-08 13:04:27 +01:00
Aymeric Fromherz
a463ec7db2 Use Cli.max_prec_digits 2022-02-21 14:54:40 +01:00
Aymeric Fromherz
63704324c1 [Z3 Backend] Better error message when function has type TAny 2022-02-19 23:28:04 +01:00