catala/compiler/surface
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
..
.ocamlformat-ignore Workaround some ocamlformat crashes 2022-05-11 16:21:07 +02:00
ast.ml Improvements to expiration checking 2022-09-07 17:14:22 +02:00
desugaring.ml Swap boxing and annotations in expressions 2022-10-07 18:00:23 +02:00
desugaring.mli Big reformatting 2022-03-08 15:03:14 +01:00
dune Merge branch 'master' into dates_calc_lib 2022-08-06 17:26:55 +02:00
fill_error_tokens.py Big renaming and dir reorg 2021-06-21 11:39:06 +02:00
fill_positions.ml Propagate renaming of Pos.mark* into module Marked 2022-07-11 16:51:54 +02:00
fill_positions.mli Big reformatting 2022-03-08 15:03:14 +01:00
lexer_common.ml Improvements to expiration checking 2022-09-07 17:14:22 +02:00
lexer_common.mli Fix doc generation 2022-04-14 11:18:26 +02:00
lexer_en.cppo.ml Add local 'let in' bindings 2022-08-09 12:17:31 +02:00
lexer_en.mli Big reformatting 2022-03-08 15:03:14 +01:00
lexer_fr.cppo.ml Add local 'let in' bindings 2022-08-09 12:17:31 +02:00
lexer_fr.mli Big reformatting 2022-03-08 15:03:14 +01:00
lexer_pl.cppo.ml Add local 'let in' bindings 2022-08-09 12:17:31 +02:00
lexer_pl.mli Big reformatting 2022-03-08 15:03:14 +01:00
lexer.cppo.ml Remove division durations 2022-08-19 15:21:29 +02:00
name_resolution.ml Implement typing of desugared/scopelang and lcalc terms 2022-10-04 14:50:37 +02:00
name_resolution.mli Implement typing of desugared/scopelang and lcalc terms 2022-10-04 14:50:37 +02:00
parse_utils.ml Big reformatting 2022-03-08 15:03:14 +01:00
parse_utils.mli Big reformatting 2022-03-08 15:03:14 +01:00
parser_driver.ml Propagate renaming of Pos.mark* into module Marked 2022-07-11 16:51:54 +02:00
parser_driver.mli Big reformatting 2022-03-08 15:03:14 +01:00
parser_errors.mli Big reformatting 2022-03-08 15:03:14 +01:00
parser.messages Fix parser message that didn't make sense to me 2022-10-04 14:50:37 +02:00
parser.mly Improvements to expiration checking 2022-09-07 17:14:22 +02:00
print.ml Big reformatting 2022-03-08 15:03:14 +01:00
print.mli Big reformatting 2022-03-08 15:03:14 +01:00
surface.mld Documentation pass 2022-01-19 10:54:16 +01:00
tokens.mly Improvements to expiration checking 2022-09-07 17:14:22 +02:00