2021-01-28 02:28:28 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 18:59:49 +03:00
|
|
|
open Shared_ast
|
2021-01-28 02:28:28 +03:00
|
|
|
module D = Dcalc.Ast
|
|
|
|
module A = Ast
|
|
|
|
|
2023-12-18 17:23:08 +03:00
|
|
|
let rec translate_typ (tau : typ) : typ =
|
2024-02-05 15:52:11 +03:00
|
|
|
Mark.map
|
|
|
|
(function
|
2023-12-18 17:23:08 +03:00
|
|
|
| TDefault t -> Mark.remove (translate_typ t)
|
|
|
|
| TLit l -> TLit l
|
|
|
|
| TTuple ts -> TTuple (List.map translate_typ ts)
|
|
|
|
| TStruct s -> TStruct s
|
|
|
|
| TEnum en -> TEnum en
|
|
|
|
| TOption _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~internal:true
|
2023-12-18 17:23:08 +03:00
|
|
|
"The types option should not appear before the dcalc -> lcalc \
|
|
|
|
translation step."
|
|
|
|
| TClosureEnv ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~internal:true
|
2023-12-18 17:23:08 +03:00
|
|
|
"The types closure_env should not appear before the dcalc -> lcalc \
|
|
|
|
translation step."
|
|
|
|
| TAny -> TAny
|
|
|
|
| TArray ts -> TArray (translate_typ ts)
|
2024-02-05 15:52:11 +03:00
|
|
|
| TArrow (t1, t2) -> TArrow (List.map translate_typ t1, translate_typ t2))
|
|
|
|
tau
|
2023-01-23 14:19:36 +03:00
|
|
|
|
2024-02-05 15:52:11 +03:00
|
|
|
let translate_mark m = Expr.map_ty translate_typ m
|
2024-01-31 20:56:11 +03:00
|
|
|
|
2021-01-28 02:28:28 +03:00
|
|
|
let rec translate_default
|
2022-08-25 17:35:08 +03:00
|
|
|
(exceptions : 'm D.expr list)
|
|
|
|
(just : 'm D.expr)
|
|
|
|
(cons : 'm D.expr)
|
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-06 20:13:45 +03:00
|
|
|
(mark_default : 'm mark) : 'm A.expr boxed =
|
2022-10-04 15:21:04 +03:00
|
|
|
let pos = Expr.mark_pos mark_default in
|
2021-01-28 02:28:28 +03:00
|
|
|
let exceptions =
|
2024-01-31 20:56:11 +03:00
|
|
|
List.map (fun except -> Expr.thunk_term (translate_expr except)) exceptions
|
2021-01-28 02:28:28 +03:00
|
|
|
in
|
2024-04-30 17:35:08 +03:00
|
|
|
Expr.eappop
|
|
|
|
~op:(Op.HandleDefault, Expr.pos cons)
|
2024-01-31 20:56:11 +03:00
|
|
|
~tys:
|
|
|
|
[
|
|
|
|
TArray (TArrow ([TLit TUnit, pos], (TAny, pos)), pos), pos;
|
|
|
|
TArrow ([TLit TUnit, pos], (TLit TBool, pos)), pos;
|
|
|
|
TArrow ([TLit TUnit, pos], (TAny, pos)), pos;
|
|
|
|
]
|
|
|
|
~args:
|
|
|
|
[
|
|
|
|
Expr.earray exceptions
|
|
|
|
(Expr.map_ty
|
|
|
|
(fun ty -> TArray (TArrow ([TLit TUnit, pos], ty), pos), pos)
|
|
|
|
mark_default);
|
|
|
|
Expr.thunk_term (translate_expr just);
|
|
|
|
Expr.thunk_term (translate_expr cons);
|
|
|
|
]
|
|
|
|
mark_default
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2023-07-04 19:31:52 +03:00
|
|
|
and translate_expr (e : 'm D.expr) : 'm A.expr boxed =
|
2024-02-05 15:52:11 +03:00
|
|
|
match e with
|
2024-04-26 19:31:26 +03:00
|
|
|
| EEmpty, m -> Expr.eraiseempty (translate_mark m)
|
2024-02-05 15:52:11 +03:00
|
|
|
| EErrorOnEmpty arg, m ->
|
|
|
|
let m = translate_mark m in
|
2024-04-26 19:31:26 +03:00
|
|
|
Expr.ecatchempty (translate_expr arg) (Expr.efatalerror Runtime.NoValue m) m
|
2024-02-05 15:52:11 +03:00
|
|
|
| EDefault { excepts; just; cons }, m ->
|
|
|
|
translate_default excepts just cons (translate_mark m)
|
|
|
|
| EPureDefault e, _ -> translate_expr e
|
|
|
|
| EAppOp { op; args; tys }, m ->
|
2023-12-18 12:25:00 +03:00
|
|
|
Expr.eappop ~op:(Operator.translate op)
|
|
|
|
~args:(List.map translate_expr args)
|
2024-01-31 20:56:11 +03:00
|
|
|
~tys:(List.map translate_typ tys)
|
2024-02-05 15:52:11 +03:00
|
|
|
(translate_mark m)
|
|
|
|
| ( ( ELit _ | EArray _ | EVar _ | EAbs _ | EApp _ | EExternal _
|
|
|
|
| EIfThenElse _ | ETuple _ | ETupleAccess _ | EInj _ | EAssert _
|
2024-04-26 19:31:26 +03:00
|
|
|
| EFatalError _ | EStruct _ | EStructAccess _ | EMatch _ ),
|
2024-02-05 15:52:11 +03:00
|
|
|
_ ) as e ->
|
|
|
|
Expr.map ~f:translate_expr ~typ:translate_typ e
|
2023-04-05 11:23:21 +03:00
|
|
|
| _ -> .
|
2021-01-28 02:28:28 +03:00
|
|
|
|
2024-01-31 20:56:11 +03:00
|
|
|
let translate_program (prg : 'm D.program) : 'm A.program =
|
2024-02-09 18:48:02 +03:00
|
|
|
Program.map_exprs prg ~typ:translate_typ ~varf:Var.translate ~f:translate_expr
|