2020-11-23 18:12:45 +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
|
2020-11-23 18:12:45 +03:00
|
|
|
|
2022-01-31 20:09:14 +03:00
|
|
|
type scope_var_ctx = {
|
2022-08-17 18:14:29 +03:00
|
|
|
scope_var_name : ScopeVar.t;
|
2022-08-25 18:29:00 +03:00
|
|
|
scope_var_typ : naked_typ;
|
2022-11-07 15:50:28 +03:00
|
|
|
scope_var_io : Desugared.Ast.io;
|
2022-01-31 20:09:14 +03:00
|
|
|
}
|
|
|
|
|
2022-12-02 18:42:29 +03:00
|
|
|
type scope_input_var_ctx = {
|
2022-12-07 17:32:08 +03:00
|
|
|
scope_input_name : StructField.t;
|
2023-05-26 17:54:52 +03:00
|
|
|
scope_input_io : Runtime.io_input Mark.pos;
|
2022-12-02 18:42:29 +03:00
|
|
|
scope_input_typ : naked_typ;
|
2023-10-12 15:41:57 +03:00
|
|
|
scope_input_thunked : bool;
|
|
|
|
(* For reentrant variables: if true, the type t of the field has been
|
|
|
|
changed to (unit -> t). Otherwise, the type was already a function and
|
|
|
|
wasn't changed so no additional wrapping will be needed *)
|
2022-12-02 18:42:29 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
type 'm scope_ref =
|
|
|
|
| Local_scope_ref of 'm Ast.expr Var.t
|
2023-08-30 18:49:29 +03:00
|
|
|
| External_scope_ref of ScopeName.t Mark.pos
|
2023-08-10 17:52:39 +03:00
|
|
|
|
2022-09-30 17:52:35 +03:00
|
|
|
type 'm scope_sig_ctx = {
|
2022-01-31 20:09:14 +03:00
|
|
|
scope_sig_local_vars : scope_var_ctx list; (** List of scope variables *)
|
2023-08-10 17:52:39 +03:00
|
|
|
scope_sig_scope_ref : 'm scope_ref;
|
|
|
|
(** Var or external representing the scope *)
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_sig_input_struct : StructName.t; (** Scope input *)
|
|
|
|
scope_sig_output_struct : StructName.t; (** Scope output *)
|
2022-12-07 17:32:08 +03:00
|
|
|
scope_sig_in_fields : scope_input_var_ctx ScopeVar.Map.t;
|
2022-11-03 17:18:51 +03:00
|
|
|
(** Mapping between the input scope variables and the input struct fields. *)
|
2022-01-31 20:09:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
type 'm scope_sigs_ctx = {
|
|
|
|
scope_sigs : 'm scope_sig_ctx ScopeName.Map.t;
|
|
|
|
scope_sigs_modules : 'm scope_sigs_ctx ModuleName.Map.t;
|
|
|
|
}
|
2020-11-26 19:06:32 +03:00
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
type 'm ctx = {
|
2023-08-10 17:52:39 +03:00
|
|
|
decl_ctx : decl_ctx;
|
2023-01-23 14:19:36 +03:00
|
|
|
scope_name : ScopeName.t option;
|
2022-09-30 17:52:35 +03:00
|
|
|
scopes_parameters : 'm scope_sigs_ctx;
|
2023-02-13 17:00:23 +03:00
|
|
|
toplevel_vars : ('m Ast.expr Var.t * naked_typ) TopdefName.Map.t;
|
2022-11-21 12:12:45 +03:00
|
|
|
scope_vars :
|
|
|
|
('m Ast.expr Var.t * naked_typ * Desugared.Ast.io) ScopeVar.Map.t;
|
2022-02-07 12:30:36 +03:00
|
|
|
subscope_vars :
|
2022-11-21 12:12:45 +03:00
|
|
|
('m Ast.expr Var.t * naked_typ * Desugared.Ast.io) ScopeVar.Map.t
|
|
|
|
SubScopeName.Map.t;
|
2023-01-20 20:18:53 +03:00
|
|
|
date_rounding : date_rounding;
|
2020-11-23 20:51:06 +03:00
|
|
|
}
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let mark_tany m pos = Expr.with_ty m (Mark.add pos TAny) ~pos
|
2022-09-30 19:30:06 +03:00
|
|
|
|
|
|
|
(* Expression argument is used as a type witness, its type and positions aren't
|
|
|
|
used *)
|
2023-05-17 17:15:00 +03:00
|
|
|
let pos_mark_mk (type a m) (e : (a, m) gexpr) :
|
2023-05-17 16:44:57 +03:00
|
|
|
(Pos.t -> m mark) * ((_, Pos.t) Mark.ed -> m mark) =
|
2022-09-30 19:30:06 +03:00
|
|
|
let pos_mark pos =
|
2023-05-17 16:44:57 +03:00
|
|
|
Expr.map_mark (fun _ -> pos) (fun _ -> TAny, pos) (Mark.get e)
|
2022-09-30 19:30:06 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
let pos_mark_as e = pos_mark (Mark.get e) in
|
2022-09-30 19:30:06 +03:00
|
|
|
pos_mark, pos_mark_as
|
2022-05-31 19:38:14 +03:00
|
|
|
|
2023-08-30 18:49:29 +03:00
|
|
|
let module_scope_sig scope_sig_ctx scope =
|
|
|
|
let ssctx =
|
|
|
|
List.fold_left
|
|
|
|
(fun ssctx m -> ModuleName.Map.find m ssctx.scope_sigs_modules)
|
|
|
|
scope_sig_ctx (ScopeName.path scope)
|
|
|
|
in
|
|
|
|
ScopeName.Map.find scope ssctx.scope_sigs
|
2023-08-10 17:52:39 +03:00
|
|
|
|
2022-12-02 18:42:29 +03:00
|
|
|
let merge_defaults
|
|
|
|
~(is_func : bool)
|
2023-05-17 17:15:00 +03:00
|
|
|
(caller : (dcalc, 'm) boxed_gexpr)
|
|
|
|
(callee : (dcalc, 'm) boxed_gexpr) : (dcalc, 'm) boxed_gexpr =
|
2022-12-02 18:42:29 +03:00
|
|
|
(* the merging of the two defaults, from the reentrant caller and the callee,
|
|
|
|
is straightfoward in the general case and a little subtler when the
|
|
|
|
variable being defined is a function. *)
|
|
|
|
if is_func then
|
2023-05-17 16:44:57 +03:00
|
|
|
let m_callee = Mark.get callee in
|
2022-12-07 19:44:14 +03:00
|
|
|
let unboxed_callee = Expr.unbox callee in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove unboxed_callee with
|
2022-12-07 19:44:14 +03:00
|
|
|
| EAbs { binder; tys } ->
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
2023-05-17 16:44:57 +03:00
|
|
|
let m_body = Mark.get body in
|
2022-12-07 19:44:14 +03:00
|
|
|
let caller =
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = Mark.get caller in
|
2022-12-07 19:44:14 +03:00
|
|
|
let pos = Expr.mark_pos m in
|
|
|
|
Expr.make_app caller
|
|
|
|
(List.map2
|
2023-05-17 17:15:00 +03:00
|
|
|
(fun (var : (dcalc, 'm) naked_gexpr Bindlib.var) ty ->
|
2022-12-07 19:44:14 +03:00
|
|
|
Expr.evar var
|
|
|
|
(* we have to correctly propagate types when doing this
|
|
|
|
rewriting *)
|
|
|
|
(Expr.with_ty m_body ~pos:(Expr.mark_pos m_body) ty))
|
|
|
|
(Array.to_list vars) tys)
|
|
|
|
pos
|
|
|
|
in
|
|
|
|
let ltrue =
|
|
|
|
Expr.elit (LBool true)
|
|
|
|
(Expr.with_ty m_callee
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.add (Expr.mark_pos m_callee) (TLit TBool)))
|
2022-12-07 19:44:14 +03:00
|
|
|
in
|
2023-11-08 20:25:50 +03:00
|
|
|
|
|
|
|
let cons = Expr.make_puredefault (Expr.rebox body) in
|
|
|
|
let d =
|
|
|
|
Expr.edefault ~excepts:[caller] ~just:ltrue ~cons (Mark.get cons)
|
|
|
|
in
|
2023-11-07 20:25:57 +03:00
|
|
|
Expr.make_abs vars (Expr.make_erroronempty d) tys (Expr.mark_pos m_callee)
|
2022-12-07 19:44:14 +03:00
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen because there should always be a lambda at the
|
|
|
|
beginning of a default with a function type *)
|
2022-12-02 18:42:29 +03:00
|
|
|
else
|
|
|
|
let caller =
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = Mark.get caller in
|
2022-12-02 18:42:29 +03:00
|
|
|
let pos = Expr.mark_pos m in
|
|
|
|
Expr.make_app caller
|
2023-05-17 16:44:57 +03:00
|
|
|
[Expr.elit LUnit (Expr.with_ty m (Mark.add pos (TLit TUnit)))]
|
2022-12-02 18:42:29 +03:00
|
|
|
pos
|
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
|
|
|
in
|
2022-12-02 18:42:29 +03:00
|
|
|
let body =
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = Mark.get callee in
|
2022-12-02 18:42:29 +03:00
|
|
|
let ltrue =
|
|
|
|
Expr.elit (LBool true)
|
2023-05-17 16:44:57 +03:00
|
|
|
(Expr.with_ty m (Mark.add (Expr.mark_pos m) (TLit TBool)))
|
2022-12-02 18:42:29 +03:00
|
|
|
in
|
2023-11-08 20:25:50 +03:00
|
|
|
let cons = Expr.make_puredefault callee in
|
|
|
|
Expr.make_erroronempty
|
|
|
|
(Expr.edefault ~excepts:[caller] ~just:ltrue ~cons (Mark.get cons))
|
2022-12-02 18:42:29 +03:00
|
|
|
in
|
|
|
|
body
|
2020-11-23 18:12:45 +03:00
|
|
|
|
2021-01-21 23:33:04 +03:00
|
|
|
let tag_with_log_entry
|
2022-11-07 15:50:28 +03:00
|
|
|
(e : 'm Ast.expr boxed)
|
2023-04-03 11:56:13 +03:00
|
|
|
(l : log_entry)
|
|
|
|
(markings : Uid.MarkedString.info list) : 'm Ast.expr boxed =
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = mark_tany (Mark.get e) (Expr.pos e) in
|
2023-04-03 11:56:13 +03:00
|
|
|
|
2023-06-28 16:57:52 +03:00
|
|
|
if Cli.globals.trace then
|
2023-04-03 11:56:13 +03:00
|
|
|
Expr.eapp (Expr.eop (Log (l, markings)) [TAny, Expr.pos e] m) [e] m
|
|
|
|
else e
|
2021-01-21 23:33:04 +03:00
|
|
|
|
2022-05-25 15:54:59 +03:00
|
|
|
(* In a list of exceptions, it is normally an error if more than a single one
|
|
|
|
apply at the same time. This relaxes this constraint slightly, allowing a
|
|
|
|
conflict if all the triggered conflicting exception yield syntactically equal
|
|
|
|
results (and as long as none of these exceptions have exceptions themselves)
|
|
|
|
|
|
|
|
NOTE: the choice of the exception that will be triggered and show in the
|
|
|
|
trace is arbitrary (but deterministic). *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let collapse_similar_outcomes (type m) (excepts : m Scopelang.Ast.expr list) :
|
|
|
|
m Scopelang.Ast.expr list =
|
2022-09-23 18:43:48 +03:00
|
|
|
let module ExprMap = Map.Make (struct
|
2022-11-07 15:50:28 +03:00
|
|
|
type t = m Scopelang.Ast.expr
|
2022-09-26 17:05:57 +03:00
|
|
|
|
|
|
|
let compare = Expr.compare
|
2023-07-12 12:48:46 +03:00
|
|
|
let format = Expr.format
|
2022-09-26 17:05:57 +03:00
|
|
|
end) in
|
2022-05-25 15:54:59 +03:00
|
|
|
let cons_map =
|
|
|
|
List.fold_left
|
|
|
|
(fun map -> function
|
2022-11-17 19:13:35 +03:00
|
|
|
| (EDefault { excepts = []; cons; _ }, _) as e ->
|
2022-09-23 18:43:48 +03:00
|
|
|
ExprMap.update cons
|
2022-05-25 15:54:59 +03:00
|
|
|
(fun prev -> Some (e :: Option.value ~default:[] prev))
|
|
|
|
map
|
|
|
|
| _ -> map)
|
2022-09-23 18:43:48 +03:00
|
|
|
ExprMap.empty excepts
|
2022-05-25 15:54:59 +03:00
|
|
|
in
|
|
|
|
let _, excepts =
|
|
|
|
List.fold_right
|
|
|
|
(fun e (cons_map, excepts) ->
|
|
|
|
match e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EDefault { excepts = []; cons; _ }, _ ->
|
2022-05-25 15:54:59 +03:00
|
|
|
let collapsed_exc =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc -> function
|
2022-11-17 19:13:35 +03:00
|
|
|
| EDefault { excepts = []; just; cons }, pos ->
|
|
|
|
[EDefault { excepts = acc; just; cons }, pos]
|
2022-05-25 15:54:59 +03:00
|
|
|
| _ -> assert false)
|
|
|
|
[]
|
2022-09-23 18:43:48 +03:00
|
|
|
(ExprMap.find cons cons_map)
|
2022-05-25 15:54:59 +03:00
|
|
|
in
|
2022-09-23 18:43:48 +03:00
|
|
|
ExprMap.add cons [] cons_map, collapsed_exc @ excepts
|
2022-05-25 15:54:59 +03:00
|
|
|
| e -> cons_map, e :: excepts)
|
|
|
|
excepts (cons_map, [])
|
|
|
|
in
|
|
|
|
excepts
|
|
|
|
|
2023-10-12 15:41:57 +03:00
|
|
|
let input_var_needs_thunking typ io_in =
|
2022-11-03 17:18:51 +03:00
|
|
|
(* For "context" (or reentrant) variables, we thunk them as [(fun () -> e)] so
|
|
|
|
that we can put them in default terms at the initialisation of the function
|
|
|
|
body, allowing an empty error to recover the default value. *)
|
2023-10-12 15:41:57 +03:00
|
|
|
match Mark.remove io_in.Desugared.Ast.io_input, typ with
|
|
|
|
| Runtime.Reentrant, TArrow _ ->
|
|
|
|
false (* we don't need to thunk expressions that are already functions *)
|
|
|
|
| Runtime.Reentrant, _ -> true
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let input_var_typ typ io_in =
|
|
|
|
let pos = Mark.get io_in.Desugared.Ast.io_input in
|
|
|
|
if input_var_needs_thunking typ io_in then
|
|
|
|
TArrow ([TLit TUnit, pos], (typ, pos)), pos
|
|
|
|
else typ, pos
|
|
|
|
|
|
|
|
let thunk_scope_arg var_ctx e =
|
|
|
|
match var_ctx.scope_input_io, var_ctx.scope_input_thunked with
|
|
|
|
| (Runtime.NoInput, _), _ -> invalid_arg "thunk_scope_arg"
|
2023-11-03 12:29:32 +03:00
|
|
|
| (Runtime.OnlyInput, _), false -> e
|
2023-10-12 15:41:57 +03:00
|
|
|
| (Runtime.Reentrant, _), false -> e
|
|
|
|
| (Runtime.Reentrant, pos), true ->
|
|
|
|
Expr.make_abs [| Var.make "_" |] e [TLit TUnit, pos] pos
|
|
|
|
| _ -> assert false
|
2022-10-24 19:25:20 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let rec translate_expr (ctx : 'm ctx) (e : 'm Scopelang.Ast.expr) :
|
|
|
|
'm Ast.expr boxed =
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = Mark.get e in
|
|
|
|
match Mark.remove e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EMatch { e = e1; name; cases = e_cases } ->
|
2023-08-30 18:49:29 +03:00
|
|
|
let enum_sig = EnumName.Map.find name ctx.decl_ctx.ctx_enums in
|
2021-01-14 02:17:24 +03:00
|
|
|
let d_cases, remaining_e_cases =
|
2022-11-17 19:13:35 +03:00
|
|
|
(* FIXME: these checks should probably be moved to a better place *)
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumConstructor.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun constructor _ (d_cases, e_cases) ->
|
2021-01-21 01:29:50 +03:00
|
|
|
let case_e =
|
2022-11-21 12:12:45 +03:00
|
|
|
try EnumConstructor.Map.find constructor e_cases
|
2023-08-15 17:57:52 +03:00
|
|
|
with EnumConstructor.Map.Not_found _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-08-30 18:49:29 +03:00
|
|
|
"The constructor %a of enum %a is missing from this pattern \
|
2021-01-21 01:29:50 +03:00
|
|
|
matching"
|
2023-08-30 18:49:29 +03:00
|
|
|
EnumConstructor.format constructor EnumName.format name
|
2022-07-11 12:34:01 +03:00
|
|
|
in
|
2021-01-21 01:29:50 +03:00
|
|
|
let case_d = translate_expr ctx case_e in
|
2022-11-21 12:12:45 +03:00
|
|
|
( EnumConstructor.Map.add constructor case_d d_cases,
|
|
|
|
EnumConstructor.Map.remove constructor e_cases ))
|
2022-11-17 19:13:35 +03:00
|
|
|
enum_sig
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.empty, e_cases)
|
2022-07-11 12:34:01 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
if not (EnumConstructor.Map.is_empty remaining_e_cases) then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
2023-08-30 18:49:29 +03:00
|
|
|
"Pattern matching is incomplete for enum %a: missing cases %a"
|
|
|
|
EnumName.format name
|
2023-07-12 12:48:46 +03:00
|
|
|
(EnumConstructor.Map.format_keys ~pp_sep:(fun fmt () ->
|
|
|
|
Format.fprintf fmt ", "))
|
|
|
|
remaining_e_cases;
|
2022-11-17 19:13:35 +03:00
|
|
|
let e1 = translate_expr ctx e1 in
|
2023-08-10 17:52:39 +03:00
|
|
|
Expr.ematch ~e:e1 ~name ~cases:d_cases m
|
2023-08-30 18:49:29 +03:00
|
|
|
| EScopeCall { scope; args } ->
|
2022-10-21 16:47:17 +03:00
|
|
|
let pos = Expr.mark_pos m in
|
2023-08-30 18:49:29 +03:00
|
|
|
let sc_sig = module_scope_sig ctx.scopes_parameters scope in
|
2022-10-24 19:25:20 +03:00
|
|
|
let in_var_map =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.merge
|
2022-12-02 18:42:29 +03:00
|
|
|
(fun var_name (str_field : scope_input_var_ctx option) expr ->
|
2022-10-24 19:25:20 +03:00
|
|
|
match str_field, expr with
|
2023-10-12 15:41:57 +03:00
|
|
|
| None, None -> assert false
|
|
|
|
| Some ({ scope_input_io = Reentrant, iopos; _ } as var_ctx), None ->
|
|
|
|
let ty0 =
|
|
|
|
match var_ctx.scope_input_typ with
|
|
|
|
| TArrow ([_], ty) -> ty
|
|
|
|
| _ -> assert false
|
|
|
|
(* reentrant field must be thunked with correct function type at
|
|
|
|
this point *)
|
|
|
|
in
|
|
|
|
Some
|
|
|
|
( var_ctx.scope_input_name,
|
|
|
|
Expr.make_abs
|
|
|
|
[| Var.make "_" |]
|
|
|
|
(Expr.eemptyerror (Expr.with_ty m ty0))
|
|
|
|
[TAny, iopos]
|
|
|
|
pos )
|
2022-12-02 18:42:29 +03:00
|
|
|
| Some var_ctx, Some e ->
|
|
|
|
Some
|
|
|
|
( var_ctx.scope_input_name,
|
2023-10-12 15:41:57 +03:00
|
|
|
thunk_scope_arg var_ctx (translate_expr ctx e) )
|
2022-12-02 18:42:29 +03:00
|
|
|
| Some var_ctx, None ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-10-24 19:25:20 +03:00
|
|
|
[
|
|
|
|
None, pos;
|
2022-10-25 12:24:35 +03:00
|
|
|
( Some "Declaration of the missing input variable",
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get (StructField.get_info var_ctx.scope_input_name) );
|
2022-10-24 19:25:20 +03:00
|
|
|
]
|
|
|
|
"Definition of input variable '%a' missing in this scope call"
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeVar.format var_name
|
2023-11-07 16:25:30 +03:00
|
|
|
| None, Some e ->
|
|
|
|
Message.raise_multispanned_error_full
|
2023-11-07 20:25:57 +03:00
|
|
|
~suggestion:
|
|
|
|
(List.map
|
|
|
|
(fun v -> Mark.remove (ScopeVar.get_info v))
|
|
|
|
(ScopeVar.Map.keys sc_sig.scope_sig_in_fields))
|
2022-10-24 19:25:20 +03:00
|
|
|
[
|
2023-11-07 16:25:30 +03:00
|
|
|
None, Expr.pos e;
|
2023-11-07 20:25:57 +03:00
|
|
|
( Some
|
|
|
|
(fun ppf ->
|
|
|
|
Format.fprintf ppf "Declaration of scope %a"
|
|
|
|
ScopeName.format scope),
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get (ScopeName.get_info scope) );
|
2022-10-24 19:25:20 +03:00
|
|
|
]
|
|
|
|
"Unknown input variable '%a' in scope call of '%a'"
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeVar.format var_name ScopeName.format scope)
|
2022-11-17 19:13:35 +03:00
|
|
|
sc_sig.scope_sig_in_fields args
|
2022-10-24 19:25:20 +03:00
|
|
|
in
|
|
|
|
let field_map =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.fold
|
|
|
|
(fun _ (fld, e) acc -> StructField.Map.add fld e acc)
|
|
|
|
in_var_map StructField.Map.empty
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
|
|
|
let arg_struct =
|
2023-08-10 17:52:39 +03:00
|
|
|
Expr.estruct ~name:sc_sig.scope_sig_input_struct ~fields:field_map
|
|
|
|
(mark_tany m pos)
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
2023-01-05 20:56:06 +03:00
|
|
|
let called_func =
|
2023-08-10 17:52:39 +03:00
|
|
|
let m = mark_tany m pos in
|
|
|
|
let e =
|
|
|
|
match sc_sig.scope_sig_scope_ref with
|
|
|
|
| Local_scope_ref v -> Expr.evar v m
|
2023-08-30 18:49:29 +03:00
|
|
|
| External_scope_ref name ->
|
2023-08-10 17:52:39 +03:00
|
|
|
Expr.eexternal ~name:(Mark.map (fun s -> External_scope s) name) m
|
|
|
|
in
|
2023-01-05 20:56:06 +03:00
|
|
|
tag_with_log_entry e BeginCall
|
2023-05-17 16:44:57 +03:00
|
|
|
[ScopeName.get_info scope; Mark.add (Expr.pos e) "direct"]
|
2023-01-05 20:56:06 +03:00
|
|
|
in
|
|
|
|
let single_arg =
|
|
|
|
tag_with_log_entry arg_struct
|
2023-05-26 17:54:52 +03:00
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = TStruct sc_sig.scope_sig_input_struct;
|
|
|
|
log_io_output = false;
|
|
|
|
log_io_input = OnlyInput;
|
|
|
|
})
|
2023-01-05 20:56:06 +03:00
|
|
|
[
|
|
|
|
ScopeName.get_info scope;
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add (Expr.pos e) "direct";
|
|
|
|
Mark.add (Expr.pos e) "input";
|
2023-01-05 20:56:06 +03:00
|
|
|
]
|
|
|
|
in
|
|
|
|
let direct_output_info =
|
|
|
|
[
|
|
|
|
ScopeName.get_info scope;
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add (Expr.pos e) "direct";
|
|
|
|
Mark.add (Expr.pos e) "output";
|
2023-01-05 20:56:06 +03:00
|
|
|
]
|
|
|
|
in
|
2023-01-11 12:42:21 +03:00
|
|
|
(* calling_expr = scope_function scope_input_struct *)
|
2023-01-05 20:56:06 +03:00
|
|
|
let calling_expr = Expr.eapp called_func [single_arg] m in
|
|
|
|
(* For the purposes of log parsing explained in Runtime.EventParser, we need
|
|
|
|
to wrap this function call in a flurry of log tags. Specifically, we are
|
|
|
|
mascarading this scope call as a function call. In a normal function
|
|
|
|
call, the log parser expects the output of the function to be defined as
|
|
|
|
a default, hence the production of the output should yield a
|
|
|
|
PosRecordIfTrueBool (which is not the case here). To remedy this absence
|
|
|
|
we fabricate a fake PosRecordIfTrueBool attached to a silent let binding
|
2023-01-07 22:22:36 +03:00
|
|
|
to "true" before returning the output value.
|
|
|
|
|
|
|
|
But this is not sufficient. Indeed for the tricky case of
|
|
|
|
[tests/test_scope/scope_call3.catala_en], when a scope returns a
|
|
|
|
function, because we insert loggins calls at the call site of the
|
|
|
|
function and not during its definition, then we're missing the call log
|
|
|
|
instructions of the function returned. To avoid this trap, we need to
|
|
|
|
rebind the resulting scope output struct by eta-expanding the functions
|
2023-03-03 13:39:55 +03:00
|
|
|
to insert logging instructions. *)
|
2023-01-05 20:56:06 +03:00
|
|
|
let result_var = Var.make "result" in
|
2023-01-07 22:22:36 +03:00
|
|
|
let result_eta_expanded_var = Var.make "result" in
|
2023-01-11 12:42:21 +03:00
|
|
|
(* result_eta_expanded = { struct_output_function_field = lambda x -> log
|
|
|
|
(struct_output.struct_output_function_field x) ... } *)
|
2023-01-07 22:22:36 +03:00
|
|
|
let result_eta_expanded =
|
2023-08-10 17:52:39 +03:00
|
|
|
Expr.estruct ~name:sc_sig.scope_sig_output_struct
|
|
|
|
~fields:
|
|
|
|
(StructField.Map.mapi
|
2023-01-07 22:22:36 +03:00
|
|
|
(fun field typ ->
|
|
|
|
let original_field_expr =
|
|
|
|
Expr.estructaccess
|
2023-08-10 17:52:39 +03:00
|
|
|
~e:
|
|
|
|
(Expr.make_var result_var
|
2023-01-07 22:22:36 +03:00
|
|
|
(Expr.with_ty m
|
|
|
|
(TStruct sc_sig.scope_sig_output_struct, Expr.pos e)))
|
2023-08-10 17:52:39 +03:00
|
|
|
~field ~name:sc_sig.scope_sig_output_struct
|
2023-05-17 16:44:57 +03:00
|
|
|
(Expr.with_ty m typ)
|
2023-02-28 11:32:25 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
match Mark.remove typ with
|
2023-02-28 11:32:25 +03:00
|
|
|
| TArrow (ts_in, t_out) ->
|
|
|
|
(* Here the output scope struct field is a function so we
|
|
|
|
eta-expand it and insert logging instructions. Invariant:
|
|
|
|
works because there is no partial evaluation. *)
|
|
|
|
let params_vars =
|
|
|
|
ListLabels.mapi ts_in ~f:(fun i _ ->
|
|
|
|
Var.make ("param" ^ string_of_int i))
|
2023-08-16 01:04:45 +03:00
|
|
|
in
|
2023-02-28 11:32:25 +03:00
|
|
|
let f_markings =
|
|
|
|
[ScopeName.get_info scope; StructField.get_info field]
|
2023-08-16 01:04:45 +03:00
|
|
|
in
|
2023-02-28 11:32:25 +03:00
|
|
|
Expr.make_abs
|
|
|
|
(Array.of_list params_vars)
|
|
|
|
(tag_with_log_entry
|
|
|
|
(tag_with_log_entry
|
|
|
|
(Expr.eapp
|
|
|
|
(tag_with_log_entry original_field_expr BeginCall
|
|
|
|
f_markings)
|
|
|
|
(ListLabels.mapi (List.combine params_vars ts_in)
|
|
|
|
~f:(fun i (param_var, t_in) ->
|
|
|
|
tag_with_log_entry
|
|
|
|
(Expr.make_var param_var
|
|
|
|
(Expr.with_ty m t_in))
|
2023-05-26 17:54:52 +03:00
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = Mark.remove t_in;
|
|
|
|
log_io_output = false;
|
|
|
|
log_io_input = OnlyInput;
|
|
|
|
})
|
2023-02-28 11:32:25 +03:00
|
|
|
(f_markings
|
|
|
|
@ [
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add (Expr.pos e)
|
2023-02-28 11:32:25 +03:00
|
|
|
("input" ^ string_of_int i);
|
|
|
|
])))
|
|
|
|
(Expr.with_ty m t_out))
|
2023-05-26 17:54:52 +03:00
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = Mark.remove t_out;
|
|
|
|
log_io_output = true;
|
|
|
|
log_io_input = NoInput;
|
|
|
|
})
|
2023-05-17 16:44:57 +03:00
|
|
|
(f_markings @ [Mark.add (Expr.pos e) "output"]))
|
2023-02-28 11:32:25 +03:00
|
|
|
EndCall f_markings)
|
|
|
|
ts_in (Expr.pos e)
|
|
|
|
| _ -> original_field_expr)
|
2023-08-30 18:49:29 +03:00
|
|
|
(StructName.Map.find sc_sig.scope_sig_output_struct
|
|
|
|
ctx.decl_ctx.ctx_structs))
|
2023-01-07 22:22:36 +03:00
|
|
|
(Expr.with_ty m (TStruct sc_sig.scope_sig_output_struct, Expr.pos e))
|
|
|
|
in
|
2023-01-11 12:42:21 +03:00
|
|
|
(* Here we have to go through an if statement that records a decision being
|
|
|
|
taken with a log. We can't just do a let-in with the true boolean value
|
|
|
|
enclosed in the log because it might get optimized by a compiler later
|
|
|
|
down the chain. *)
|
|
|
|
(* if_then_else_returned = if log true then result_eta_expanded else
|
2023-01-20 23:21:42 +03:00
|
|
|
result_eta_expanded *)
|
2023-01-05 20:56:06 +03:00
|
|
|
let if_then_else_returned =
|
|
|
|
Expr.eifthenelse
|
|
|
|
(tag_with_log_entry
|
|
|
|
(Expr.box
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.add
|
2023-01-05 20:56:06 +03:00
|
|
|
(Expr.with_ty m (TLit TBool, Expr.pos e))
|
|
|
|
(ELit (LBool true))))
|
|
|
|
PosRecordIfTrueBool direct_output_info)
|
2023-01-07 22:22:36 +03:00
|
|
|
(Expr.make_var result_eta_expanded_var
|
2023-01-05 20:56:06 +03:00
|
|
|
(Expr.with_ty m (TStruct sc_sig.scope_sig_output_struct, Expr.pos e)))
|
2023-01-20 23:21:42 +03:00
|
|
|
(Expr.make_var result_eta_expanded_var
|
|
|
|
(Expr.with_ty m (TStruct sc_sig.scope_sig_output_struct, Expr.pos e)))
|
2023-01-05 20:56:06 +03:00
|
|
|
(Expr.with_ty m (TStruct sc_sig.scope_sig_output_struct, Expr.pos e))
|
|
|
|
in
|
2023-01-11 12:42:21 +03:00
|
|
|
(* let result_var = calling_expr in let result_eta_expanded_var =
|
|
|
|
result_eta_expaneded in log (if_then_else_returned ) *)
|
2023-01-05 20:56:06 +03:00
|
|
|
Expr.make_let_in result_var
|
|
|
|
(TStruct sc_sig.scope_sig_output_struct, Expr.pos e)
|
|
|
|
calling_expr
|
2023-01-07 22:22:36 +03:00
|
|
|
(Expr.make_let_in result_eta_expanded_var
|
|
|
|
(TStruct sc_sig.scope_sig_output_struct, Expr.pos e)
|
|
|
|
result_eta_expanded
|
|
|
|
(tag_with_log_entry
|
|
|
|
(tag_with_log_entry if_then_else_returned
|
2023-05-26 17:54:52 +03:00
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = TStruct sc_sig.scope_sig_output_struct;
|
|
|
|
log_io_output = true;
|
|
|
|
log_io_input = NoInput;
|
|
|
|
})
|
2023-01-07 22:22:36 +03:00
|
|
|
direct_output_info)
|
|
|
|
EndCall
|
2023-05-17 16:44:57 +03:00
|
|
|
[ScopeName.get_info scope; Mark.add (Expr.pos e) "direct"])
|
2023-01-07 22:22:36 +03:00
|
|
|
(Expr.pos e))
|
2023-01-05 20:56:06 +03:00
|
|
|
(Expr.pos e)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp { f; args } ->
|
2022-06-15 20:33:24 +03:00
|
|
|
(* We insert various log calls to record arguments and outputs of
|
|
|
|
user-defined functions belonging to scopes *)
|
2022-11-17 19:13:35 +03:00
|
|
|
let e1_func = translate_expr ctx f in
|
2023-01-23 14:19:36 +03:00
|
|
|
let markings =
|
2023-05-17 16:44:57 +03:00
|
|
|
match ctx.scope_name, Mark.remove f with
|
2023-01-23 14:19:36 +03:00
|
|
|
| Some sname, ELocation loc -> (
|
|
|
|
match loc with
|
2023-08-10 17:52:39 +03:00
|
|
|
| ScopelangScopeVar { name = v, _; _ } ->
|
2023-01-23 14:19:36 +03:00
|
|
|
[ScopeName.get_info sname; ScopeVar.get_info v]
|
2023-08-10 17:52:39 +03:00
|
|
|
| SubScopeVar { scope; var = v, _; _ } ->
|
|
|
|
[ScopeName.get_info scope; ScopeVar.get_info v]
|
2023-02-13 17:00:23 +03:00
|
|
|
| ToplevelVar _ -> [])
|
2023-01-23 14:19:36 +03:00
|
|
|
| _ -> []
|
2022-05-30 12:20:48 +03:00
|
|
|
in
|
2021-01-21 01:29:50 +03:00
|
|
|
let e1_func =
|
2023-01-23 14:19:36 +03:00
|
|
|
match markings with
|
|
|
|
| [] -> e1_func
|
|
|
|
| m -> tag_with_log_entry e1_func BeginCall m
|
2022-07-11 12:34:01 +03:00
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
let new_args = List.map (translate_expr ctx) args in
|
2023-02-20 19:58:29 +03:00
|
|
|
let input_typs, output_typ =
|
2023-08-10 17:52:39 +03:00
|
|
|
(* NOTE: this is a temporary solution, it works because it's assumed that
|
|
|
|
all function calls are from scope variables. However, this will change
|
2022-05-30 12:20:48 +03:00
|
|
|
-- for more information see
|
2022-06-15 20:33:24 +03:00
|
|
|
https://github.com/CatalaLang/catala/pull/280#discussion_r898851693. *)
|
2022-05-30 12:20:48 +03:00
|
|
|
let retrieve_in_and_out_typ_or_any var vars =
|
2023-05-17 16:44:57 +03:00
|
|
|
let _, typ, _ = ScopeVar.Map.find (Mark.remove var) vars in
|
2022-06-15 15:34:15 +03:00
|
|
|
match typ with
|
2022-08-12 23:42:39 +03:00
|
|
|
| TArrow (marked_input_typ, marked_output_typ) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
List.map Mark.remove marked_input_typ, Mark.remove marked_output_typ
|
2023-02-21 16:16:50 +03:00
|
|
|
| _ -> ListLabels.map new_args ~f:(fun _ -> TAny), TAny
|
2022-06-15 15:34:15 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove f with
|
2023-08-10 17:52:39 +03:00
|
|
|
| ELocation (ScopelangScopeVar { name = var }) ->
|
2022-06-15 15:34:15 +03:00
|
|
|
retrieve_in_and_out_typ_or_any var ctx.scope_vars
|
2023-08-10 17:52:39 +03:00
|
|
|
| ELocation (SubScopeVar { alias; var; _ }) ->
|
2022-06-15 15:34:15 +03:00
|
|
|
ctx.subscope_vars
|
2023-08-10 17:52:39 +03:00
|
|
|
|> SubScopeName.Map.find (Mark.remove alias)
|
2022-06-15 15:34:15 +03:00
|
|
|
|> retrieve_in_and_out_typ_or_any var
|
2023-08-30 18:49:29 +03:00
|
|
|
| ELocation (ToplevelVar { name }) -> (
|
|
|
|
let decl_ctx =
|
|
|
|
Program.module_ctx ctx.decl_ctx (TopdefName.path (Mark.remove name))
|
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let typ = TopdefName.Map.find (Mark.remove name) decl_ctx.ctx_topdefs in
|
|
|
|
match Mark.remove typ with
|
|
|
|
| TArrow (tin, (tout, _)) -> List.map Mark.remove tin, tout
|
|
|
|
| _ ->
|
|
|
|
Message.raise_spanned_error (Expr.pos e)
|
|
|
|
"Application of non-function toplevel variable")
|
2023-02-21 16:16:50 +03:00
|
|
|
| _ -> ListLabels.map new_args ~f:(fun _ -> TAny), TAny
|
2022-07-11 12:34:01 +03:00
|
|
|
in
|
2023-02-21 16:16:50 +03:00
|
|
|
|
2023-06-13 12:27:45 +03:00
|
|
|
(* Message.emit_debug "new_args %d, input_typs: %d, input_typs %a"
|
2023-02-21 16:16:50 +03:00
|
|
|
(List.length new_args) (List.length input_typs) (Format.pp_print_list
|
2023-05-17 16:44:57 +03:00
|
|
|
Print.typ_debug) (List.map (Mark.add Pos.no_pos) input_typs); *)
|
2021-01-21 01:29:50 +03:00
|
|
|
let new_args =
|
2023-02-20 19:58:29 +03:00
|
|
|
ListLabels.mapi (List.combine new_args input_typs)
|
|
|
|
~f:(fun i (new_arg, input_typ) ->
|
|
|
|
match markings with
|
|
|
|
| _ :: _ as m ->
|
2023-05-26 17:54:52 +03:00
|
|
|
tag_with_log_entry new_arg
|
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = input_typ;
|
|
|
|
log_io_output = false;
|
|
|
|
log_io_input = OnlyInput;
|
|
|
|
})
|
2023-05-17 16:44:57 +03:00
|
|
|
(m @ [Mark.add (Expr.pos e) ("input" ^ string_of_int i)])
|
2023-02-20 19:58:29 +03:00
|
|
|
| _ -> new_arg)
|
2021-01-21 01:29:50 +03:00
|
|
|
in
|
2023-02-20 19:58:29 +03:00
|
|
|
|
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
|
|
|
let new_e = Expr.eapp e1_func new_args m in
|
2021-01-21 01:29:50 +03:00
|
|
|
let new_e =
|
2023-01-23 14:19:36 +03:00
|
|
|
match markings with
|
|
|
|
| [] -> new_e
|
|
|
|
| m ->
|
2021-01-21 23:33:04 +03:00
|
|
|
tag_with_log_entry
|
2023-05-26 17:54:52 +03:00
|
|
|
(tag_with_log_entry new_e
|
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = output_typ;
|
|
|
|
log_io_output = true;
|
|
|
|
log_io_input = NoInput;
|
|
|
|
})
|
2023-05-17 16:44:57 +03:00
|
|
|
(m @ [Mark.add (Expr.pos e) "output"]))
|
2023-01-23 14:19:36 +03:00
|
|
|
EndCall m
|
2021-01-21 01:29:50 +03:00
|
|
|
in
|
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
|
|
|
new_e
|
2022-11-17 19:13:35 +03:00
|
|
|
| EDefault { excepts; just; cons } ->
|
2022-05-25 15:54:59 +03:00
|
|
|
let excepts = collapse_similar_outcomes excepts in
|
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
|
|
|
Expr.edefault
|
2023-11-08 20:25:50 +03:00
|
|
|
~excepts:(List.map (translate_expr ctx) excepts)
|
|
|
|
~just:(translate_expr ctx just) ~cons:(translate_expr ctx cons) m
|
|
|
|
| EPureDefault e -> Expr.epuredefault (translate_expr ctx e) m
|
2023-08-10 17:52:39 +03:00
|
|
|
| ELocation (ScopelangScopeVar { name = a }) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
let v, _, _ = ScopeVar.Map.find (Mark.remove a) ctx.scope_vars in
|
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
|
|
|
Expr.evar v m
|
2023-08-10 17:52:39 +03:00
|
|
|
| ELocation (SubScopeVar { alias = s; var = a; _ }) -> (
|
2022-07-11 12:34:01 +03:00
|
|
|
try
|
2022-02-05 02:04:19 +03:00
|
|
|
let v, _, _ =
|
2023-05-17 16:44:57 +03:00
|
|
|
ScopeVar.Map.find (Mark.remove a)
|
|
|
|
(SubScopeName.Map.find (Mark.remove s) ctx.subscope_vars)
|
2020-11-23 20:51:06 +03:00
|
|
|
in
|
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
|
|
|
Expr.evar v m
|
2023-08-15 17:57:52 +03:00
|
|
|
with ScopeVar.Map.Not_found _ | SubScopeName.Map.Not_found _ ->
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-02-07 20:18:23 +03:00
|
|
|
[
|
2022-08-26 16:21:47 +03:00
|
|
|
Some "Incriminated variable usage:", Expr.pos e;
|
2022-02-07 20:18:23 +03:00
|
|
|
( Some "Incriminated subscope variable declaration:",
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get (ScopeVar.get_info (Mark.remove a)) );
|
2022-02-07 20:18:23 +03:00
|
|
|
( Some "Incriminated subscope declaration:",
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get (SubScopeName.get_info (Mark.remove s)) );
|
2022-03-08 15:04:27 +03:00
|
|
|
]
|
2022-10-25 15:03:35 +03:00
|
|
|
"The variable %a.%a cannot be used here, as it is not part of subscope \
|
2022-03-08 15:04:27 +03:00
|
|
|
%a's results. Maybe you forgot to qualify it as an output?"
|
2023-07-12 12:48:46 +03:00
|
|
|
SubScopeName.format (Mark.remove s) ScopeVar.format (Mark.remove a)
|
|
|
|
SubScopeName.format (Mark.remove s))
|
2023-08-30 18:49:29 +03:00
|
|
|
| ELocation (ToplevelVar { name }) ->
|
|
|
|
let path = TopdefName.path (Mark.remove name) in
|
|
|
|
if path = [] then
|
|
|
|
let v, _ = TopdefName.Map.find (Mark.remove name) ctx.toplevel_vars in
|
|
|
|
Expr.evar v m
|
|
|
|
else Expr.eexternal ~name:(Mark.map (fun n -> External_value n) name) m
|
2023-03-30 16:33:00 +03:00
|
|
|
| EOp { op = Add_dat_dur _; tys } ->
|
|
|
|
Expr.eop (Add_dat_dur ctx.date_rounding) tys m
|
2023-05-11 18:39:38 +03:00
|
|
|
| EOp { op; tys } -> Expr.eop (Operator.translate op) tys m
|
2023-08-10 17:52:39 +03:00
|
|
|
| ( EVar _ | EAbs _ | ELit _ | EStruct _ | EStructAccess _ | ETuple _
|
2023-05-11 17:40:47 +03:00
|
|
|
| ETupleAccess _ | EInj _ | EEmptyError | EErrorOnEmpty _ | EArray _
|
|
|
|
| EIfThenElse _ ) as e ->
|
|
|
|
Expr.map ~f:(translate_expr ctx) (e, m)
|
2020-11-23 18:12:45 +03:00
|
|
|
|
2021-10-28 16:24:39 +03:00
|
|
|
(** The result of a rule translation is a list of assignment, with variables and
|
|
|
|
expressions. We also return the new translation context available after the
|
2022-04-02 15:51:11 +03:00
|
|
|
assignment to use in later rule translations. The list is actually a
|
|
|
|
continuation yielding a [Dcalc.scope_body_expr] by giving it what should
|
|
|
|
come later in the chain of let-bindings. *)
|
2021-10-28 16:24:39 +03:00
|
|
|
let translate_rule
|
2022-09-23 18:43:48 +03:00
|
|
|
(ctx : 'm ctx)
|
2022-11-07 15:50:28 +03:00
|
|
|
(rule : 'm Scopelang.Ast.rule)
|
2022-11-21 12:46:17 +03:00
|
|
|
((sigma_name, pos_sigma) : Uid.MarkedString.info) :
|
2022-11-07 15:50:28 +03:00
|
|
|
('m Ast.expr scope_body_expr Bindlib.box ->
|
|
|
|
'm Ast.expr scope_body_expr Bindlib.box)
|
2022-09-23 18:43:48 +03:00
|
|
|
* 'm ctx =
|
2020-11-23 20:51:06 +03:00
|
|
|
match rule with
|
2023-08-10 17:52:39 +03:00
|
|
|
| Definition ((ScopelangScopeVar { name = a }, var_def_pos), tau, a_io, e) ->
|
2023-11-03 12:29:32 +03:00
|
|
|
let pos_mark, _ = pos_mark_mk e in
|
2023-05-17 16:44:57 +03:00
|
|
|
let a_name = ScopeVar.get_info (Mark.remove a) in
|
|
|
|
let a_var = Var.make (Mark.remove a_name) in
|
2020-11-26 17:48:26 +03:00
|
|
|
let new_e = translate_expr ctx e in
|
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
|
|
|
let a_expr = Expr.make_var a_var (pos_mark var_def_pos) in
|
2023-05-17 16:44:57 +03:00
|
|
|
let is_func = match Mark.remove tau with TArrow _ -> true | _ -> false in
|
2020-12-10 18:58:32 +03:00
|
|
|
let merged_expr =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove a_io.io_input with
|
2022-12-02 18:42:29 +03:00
|
|
|
| OnlyInput -> failwith "should not happen"
|
|
|
|
(* scopelang should not contain any definitions of input only variables *)
|
2023-02-20 17:48:56 +03:00
|
|
|
| Reentrant -> merge_defaults ~is_func a_expr new_e
|
2023-11-03 12:29:32 +03:00
|
|
|
| NoInput -> new_e
|
2020-12-09 13:23:03 +03:00
|
|
|
in
|
2020-12-11 12:51:46 +03:00
|
|
|
let merged_expr =
|
2021-04-05 20:06:32 +03:00
|
|
|
tag_with_log_entry merged_expr
|
2023-05-26 17:54:52 +03:00
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = Mark.remove tau;
|
|
|
|
log_io_output = Mark.remove a_io.io_output;
|
|
|
|
log_io_input = Mark.remove a_io.io_input;
|
|
|
|
})
|
2021-04-05 20:06:32 +03:00
|
|
|
[sigma_name, pos_sigma; a_name]
|
2020-12-11 12:51:46 +03:00
|
|
|
in
|
2022-04-02 15:51:11 +03:00
|
|
|
( (fun next ->
|
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun next merged_expr ->
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeLet
|
2022-04-02 15:51:11 +03:00
|
|
|
{
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_next = next;
|
|
|
|
scope_let_typ = tau;
|
|
|
|
scope_let_expr = merged_expr;
|
|
|
|
scope_let_kind = ScopeVarDefinition;
|
2023-05-17 16:44:57 +03:00
|
|
|
scope_let_pos = Mark.get a;
|
2022-04-02 15:51:11 +03:00
|
|
|
})
|
|
|
|
(Bindlib.bind_var a_var next)
|
2022-10-21 16:33:05 +03:00
|
|
|
(Expr.Box.lift merged_expr)),
|
2021-10-28 16:24:39 +03:00
|
|
|
{
|
|
|
|
ctx with
|
2022-02-05 02:04:19 +03:00
|
|
|
scope_vars =
|
2023-05-17 16:44:57 +03:00
|
|
|
ScopeVar.Map.add (Mark.remove a)
|
|
|
|
(a_var, Mark.remove tau, a_io)
|
2022-02-07 12:30:36 +03:00
|
|
|
ctx.scope_vars;
|
2021-10-28 16:24:39 +03:00
|
|
|
} )
|
2022-02-10 12:09:58 +03:00
|
|
|
| Definition
|
2023-10-13 17:12:15 +03:00
|
|
|
((SubScopeVar { alias = subs_index; var = subs_var; _ }, _), tau, a_io, e)
|
|
|
|
->
|
2020-11-26 19:06:32 +03:00
|
|
|
let a_name =
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.map
|
2022-02-09 17:56:48 +03:00
|
|
|
(fun str ->
|
2023-05-17 16:44:57 +03:00
|
|
|
str ^ "." ^ Mark.remove (ScopeVar.get_info (Mark.remove subs_var)))
|
|
|
|
(SubScopeName.get_info (Mark.remove subs_index))
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
let a_var = Var.make (Mark.remove a_name) in
|
2022-04-02 15:51:11 +03:00
|
|
|
let new_e =
|
2021-10-28 16:24:39 +03:00
|
|
|
tag_with_log_entry (translate_expr ctx e)
|
2023-05-26 17:54:52 +03:00
|
|
|
(VarDef
|
|
|
|
{
|
|
|
|
log_typ = Mark.remove tau;
|
|
|
|
log_io_output = false;
|
|
|
|
log_io_input = Mark.remove a_io.Desugared.Ast.io_input;
|
|
|
|
})
|
2022-01-31 20:09:14 +03:00
|
|
|
[sigma_name, pos_sigma; a_name]
|
2022-02-06 20:52:18 +03:00
|
|
|
in
|
2022-11-07 15:50:28 +03:00
|
|
|
let thunked_or_nonempty_new_e =
|
2023-10-12 15:41:57 +03:00
|
|
|
match a_io.Desugared.Ast.io_input with
|
|
|
|
| Runtime.NoInput, _ -> assert false
|
2023-11-03 12:29:32 +03:00
|
|
|
| Runtime.OnlyInput, _ -> new_e
|
2023-10-12 15:41:57 +03:00
|
|
|
| Runtime.Reentrant, pos -> (
|
|
|
|
match Mark.remove tau with
|
2023-11-07 20:25:57 +03:00
|
|
|
| TArrow _ -> new_e
|
|
|
|
| _ -> Expr.thunk_term new_e (Expr.with_pos pos (Mark.get new_e)))
|
2022-11-07 15:50:28 +03:00
|
|
|
in
|
2022-02-06 20:52:18 +03:00
|
|
|
( (fun next ->
|
2022-04-02 15:51:11 +03:00
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun next thunked_or_nonempty_new_e ->
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeLet
|
2022-04-02 15:51:11 +03:00
|
|
|
{
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_next = next;
|
2023-05-17 16:44:57 +03:00
|
|
|
scope_let_pos = Mark.get a_name;
|
2023-10-12 15:41:57 +03:00
|
|
|
scope_let_typ = input_var_typ (Mark.remove tau) a_io;
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_expr = thunked_or_nonempty_new_e;
|
|
|
|
scope_let_kind = SubScopeVarDefinition;
|
2022-04-02 15:51:11 +03:00
|
|
|
})
|
|
|
|
(Bindlib.bind_var a_var next)
|
2022-10-21 16:33:05 +03:00
|
|
|
(Expr.Box.lift thunked_or_nonempty_new_e)),
|
2022-05-04 18:40:55 +03:00
|
|
|
{
|
|
|
|
ctx with
|
2022-04-02 15:51:11 +03:00
|
|
|
subscope_vars =
|
2023-05-17 16:44:57 +03:00
|
|
|
SubScopeName.Map.update (Mark.remove subs_index)
|
2021-10-28 16:24:39 +03:00
|
|
|
(fun map ->
|
|
|
|
match map with
|
|
|
|
| Some map ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Some
|
2023-05-17 16:44:57 +03:00
|
|
|
(ScopeVar.Map.add (Mark.remove subs_var)
|
|
|
|
(a_var, Mark.remove tau, a_io)
|
2022-05-04 18:40:55 +03:00
|
|
|
map)
|
2021-10-28 16:24:39 +03:00
|
|
|
| None ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Some
|
2023-05-17 16:44:57 +03:00
|
|
|
(ScopeVar.Map.singleton (Mark.remove subs_var)
|
|
|
|
(a_var, Mark.remove tau, a_io)))
|
2021-10-28 16:24:39 +03:00
|
|
|
ctx.subscope_vars;
|
2022-05-04 18:40:55 +03:00
|
|
|
} )
|
2023-02-13 17:00:23 +03:00
|
|
|
| Definition ((ToplevelVar _, _), _, _, _) ->
|
2023-01-23 14:19:36 +03:00
|
|
|
assert false
|
2023-02-13 17:00:23 +03:00
|
|
|
(* A global variable can't be defined locally. The [Definition] constructor
|
|
|
|
could be made more specific to avoid this case, but the added complexity
|
|
|
|
didn't seem worth it *)
|
2023-08-30 18:49:29 +03:00
|
|
|
| Call (subname, subindex, m) ->
|
|
|
|
let subscope_sig = module_scope_sig ctx.scopes_parameters subname in
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope_sig_decl =
|
|
|
|
ScopeName.Map.find subname
|
2023-08-30 18:49:29 +03:00
|
|
|
(Program.module_ctx ctx.decl_ctx (ScopeName.path subname)).ctx_scopes
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
2022-04-12 11:53:07 +03:00
|
|
|
let all_subscope_vars = subscope_sig.scope_sig_local_vars in
|
|
|
|
let all_subscope_input_vars =
|
|
|
|
List.filter
|
2022-02-07 12:30:36 +03:00
|
|
|
(fun var_ctx ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove var_ctx.scope_var_io.Desugared.Ast.io_input with
|
2022-02-07 12:30:36 +03:00
|
|
|
| NoInput -> false
|
|
|
|
| _ -> true)
|
2022-02-06 20:52:18 +03:00
|
|
|
all_subscope_vars
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2023-11-03 19:15:55 +03:00
|
|
|
let called_scope_input_struct = subscope_sig.scope_sig_input_struct in
|
|
|
|
let called_scope_return_struct = subscope_sig.scope_sig_output_struct in
|
2022-02-06 20:52:18 +03:00
|
|
|
let all_subscope_output_vars =
|
2023-11-03 19:15:55 +03:00
|
|
|
List.filter_map
|
2022-11-07 15:50:28 +03:00
|
|
|
(fun var_ctx ->
|
2023-11-07 20:25:57 +03:00
|
|
|
if Mark.remove var_ctx.scope_var_io.Desugared.Ast.io_output then
|
|
|
|
(* Retrieve the actual expected output type through the scope output
|
|
|
|
struct definition *)
|
|
|
|
let str =
|
|
|
|
StructName.Map.find called_scope_return_struct
|
|
|
|
ctx.decl_ctx.ctx_structs
|
|
|
|
in
|
|
|
|
let fld =
|
|
|
|
ScopeVar.Map.find var_ctx.scope_var_name
|
|
|
|
scope_sig_decl.out_struct_fields
|
|
|
|
in
|
|
|
|
let ty = StructField.Map.find fld str in
|
|
|
|
Some { var_ctx with scope_var_typ = Mark.remove ty }
|
|
|
|
else None)
|
2022-02-06 20:52:18 +03:00
|
|
|
all_subscope_vars
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let pos_call = Mark.get (SubScopeName.get_info subindex) in
|
|
|
|
let scope_dcalc_ref =
|
|
|
|
let m = mark_tany m pos_call in
|
|
|
|
match subscope_sig.scope_sig_scope_ref with
|
|
|
|
| Local_scope_ref var -> Expr.make_var var m
|
2023-08-30 18:49:29 +03:00
|
|
|
| External_scope_ref name ->
|
|
|
|
Expr.eexternal ~name:(Mark.map (fun n -> External_scope n) name) m
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
2022-04-12 11:53:07 +03:00
|
|
|
let subscope_vars_defined =
|
2022-11-21 12:12:45 +03:00
|
|
|
try SubScopeName.Map.find subindex ctx.subscope_vars
|
2023-08-15 17:57:52 +03:00
|
|
|
with SubScopeName.Map.Not_found _ -> ScopeVar.Map.empty
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2020-11-27 13:37:21 +03:00
|
|
|
let subscope_var_not_yet_defined subvar =
|
2022-11-21 12:12:45 +03:00
|
|
|
not (ScopeVar.Map.mem subvar subscope_vars_defined)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2020-11-27 13:37:21 +03:00
|
|
|
let subscope_args =
|
2022-11-17 19:13:35 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun acc (subvar : scope_var_ctx) ->
|
|
|
|
let e =
|
|
|
|
if subscope_var_not_yet_defined subvar.scope_var_name then
|
|
|
|
(* This is a redundant check. Normally, all subscope variables
|
|
|
|
should have been defined (even an empty definition, if they're
|
|
|
|
not defined by any rule in the source code) by the translation
|
|
|
|
from desugared to the scope language. *)
|
|
|
|
Expr.empty_thunked_term m
|
|
|
|
else
|
|
|
|
let a_var, _, _ =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.find subvar.scope_var_name subscope_vars_defined
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
Expr.make_var a_var (mark_tany m pos_call)
|
|
|
|
in
|
|
|
|
let field =
|
2022-12-07 17:32:08 +03:00
|
|
|
(ScopeVar.Map.find subvar.scope_var_name
|
2022-12-02 18:42:29 +03:00
|
|
|
subscope_sig.scope_sig_in_fields)
|
|
|
|
.scope_input_name
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.Map.add field e acc)
|
|
|
|
StructField.Map.empty all_subscope_input_vars
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-04-02 15:51:11 +03:00
|
|
|
let subscope_struct_arg =
|
2023-08-10 17:52:39 +03:00
|
|
|
Expr.estruct ~name:called_scope_input_struct ~fields:subscope_args
|
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_tany m pos_call)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-04-02 15:51:11 +03:00
|
|
|
let all_subscope_output_vars_dcalc =
|
2022-05-04 18:40:55 +03:00
|
|
|
List.map
|
2022-01-31 20:09:14 +03:00
|
|
|
(fun (subvar : scope_var_ctx) ->
|
2020-11-27 13:37:21 +03:00
|
|
|
let sub_dcalc_var =
|
2022-07-28 11:36:36 +03:00
|
|
|
Var.make
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.remove (SubScopeName.get_info subindex)
|
2022-06-03 17:40:03 +03:00
|
|
|
^ "."
|
2023-05-17 16:44:57 +03:00
|
|
|
^ Mark.remove (ScopeVar.get_info subvar.scope_var_name))
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-01-31 20:09:14 +03:00
|
|
|
subvar, sub_dcalc_var)
|
2022-02-06 20:52:18 +03:00
|
|
|
all_subscope_output_vars
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2020-12-11 12:51:46 +03:00
|
|
|
let subscope_func =
|
2021-01-21 23:33:04 +03:00
|
|
|
tag_with_log_entry scope_dcalc_ref BeginCall
|
2022-05-04 18:40:55 +03:00
|
|
|
[
|
2021-01-21 23:33:04 +03:00
|
|
|
sigma_name, pos_sigma;
|
2022-08-17 18:14:29 +03:00
|
|
|
SubScopeName.get_info subindex;
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeName.get_info subname;
|
2022-05-04 18:40:55 +03:00
|
|
|
]
|
|
|
|
in
|
2020-11-27 13:37:21 +03:00
|
|
|
let call_expr =
|
2021-01-21 23:33:04 +03:00
|
|
|
tag_with_log_entry
|
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
|
|
|
(Expr.eapp subscope_func [subscope_struct_arg] (mark_tany m pos_call))
|
2022-08-12 23:42:39 +03:00
|
|
|
EndCall
|
2022-05-04 18:40:55 +03:00
|
|
|
[
|
2022-04-02 15:51:11 +03:00
|
|
|
sigma_name, pos_sigma;
|
2022-08-17 18:14:29 +03:00
|
|
|
SubScopeName.get_info subindex;
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeName.get_info subname;
|
2022-05-04 18:40:55 +03:00
|
|
|
]
|
|
|
|
in
|
2022-07-28 11:36:36 +03:00
|
|
|
let result_tuple_var = Var.make "result" in
|
2022-08-23 16:23:52 +03:00
|
|
|
let result_tuple_typ = TStruct called_scope_return_struct, pos_sigma in
|
2022-05-31 19:38:14 +03:00
|
|
|
let call_scope_let next =
|
2022-04-02 15:51:11 +03:00
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun next call_expr ->
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeLet
|
2022-05-04 18:40:55 +03:00
|
|
|
{
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_next = next;
|
|
|
|
scope_let_pos = pos_sigma;
|
|
|
|
scope_let_kind = CallingSubScope;
|
|
|
|
scope_let_typ = result_tuple_typ;
|
|
|
|
scope_let_expr = call_expr;
|
2022-05-04 18:40:55 +03:00
|
|
|
})
|
2022-04-02 15:51:11 +03:00
|
|
|
(Bindlib.bind_var result_tuple_var next)
|
2022-10-21 16:33:05 +03:00
|
|
|
(Expr.Box.lift call_expr)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-05-31 19:38:14 +03:00
|
|
|
let result_bindings_lets next =
|
2021-01-28 15:58:59 +03:00
|
|
|
List.fold_right
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun (var_ctx, v) next ->
|
|
|
|
let field =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.find var_ctx.scope_var_name
|
2023-08-10 17:52:39 +03:00
|
|
|
scope_sig_decl.out_struct_fields
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun next r ->
|
|
|
|
ScopeLet
|
|
|
|
{
|
|
|
|
scope_let_next = next;
|
|
|
|
scope_let_pos = pos_sigma;
|
|
|
|
scope_let_typ = var_ctx.scope_var_typ, pos_sigma;
|
|
|
|
scope_let_kind = DestructuringSubScopeResults;
|
|
|
|
scope_let_expr =
|
|
|
|
( EStructAccess
|
|
|
|
{ name = called_scope_return_struct; e = r; field },
|
|
|
|
mark_tany m pos_sigma );
|
|
|
|
})
|
|
|
|
(Bindlib.bind_var v next)
|
|
|
|
(Expr.Box.lift
|
|
|
|
(Expr.make_var result_tuple_var (mark_tany m pos_sigma))))
|
|
|
|
all_subscope_output_vars_dcalc next
|
2021-12-09 20:42:36 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
( (fun next -> call_scope_let (result_bindings_lets next)),
|
2021-10-28 16:24:39 +03:00
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
subscope_vars =
|
2022-11-21 12:12:45 +03:00
|
|
|
SubScopeName.Map.add subindex
|
2021-10-28 16:24:39 +03:00
|
|
|
(List.fold_left
|
2022-01-31 20:09:14 +03:00
|
|
|
(fun acc (var_ctx, dvar) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.add var_ctx.scope_var_name
|
2022-02-07 12:30:36 +03:00
|
|
|
(dvar, var_ctx.scope_var_typ, var_ctx.scope_var_io)
|
2022-02-05 02:04:19 +03:00
|
|
|
acc)
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.empty all_subscope_output_vars_dcalc)
|
2021-10-28 16:24:39 +03:00
|
|
|
ctx.subscope_vars;
|
|
|
|
} )
|
2020-12-10 20:11:43 +03:00
|
|
|
| Assertion e ->
|
|
|
|
let new_e = translate_expr ctx e in
|
2022-10-03 18:07:06 +03:00
|
|
|
let scope_let_pos = Expr.pos e in
|
|
|
|
let scope_let_typ = TLit TUnit, scope_let_pos in
|
2022-04-02 15:51:11 +03:00
|
|
|
( (fun next ->
|
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun next new_e ->
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeLet
|
2022-04-02 15:51:11 +03:00
|
|
|
{
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_next = next;
|
2022-10-03 18:07:06 +03:00
|
|
|
scope_let_pos;
|
|
|
|
scope_let_typ;
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_expr =
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.add
|
|
|
|
(Expr.map_ty (fun _ -> scope_let_typ) (Mark.get e))
|
2023-11-03 19:15:55 +03:00
|
|
|
(EAssert new_e);
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_let_kind = Assertion;
|
2022-04-02 15:51:11 +03:00
|
|
|
})
|
2022-07-28 11:36:36 +03:00
|
|
|
(Bindlib.bind_var (Var.make "_") next)
|
2022-10-21 16:33:05 +03:00
|
|
|
(Expr.Box.lift new_e)),
|
2021-10-28 16:24:39 +03:00
|
|
|
ctx )
|
2020-11-23 20:51:06 +03:00
|
|
|
|
2021-10-28 16:24:39 +03:00
|
|
|
let translate_rules
|
2022-09-23 18:43:48 +03:00
|
|
|
(ctx : 'm ctx)
|
2023-08-10 17:52:39 +03:00
|
|
|
(scope_name : ScopeName.t)
|
2022-11-07 15:50:28 +03:00
|
|
|
(rules : 'm Scopelang.Ast.rule list)
|
2022-11-21 12:46:17 +03:00
|
|
|
((sigma_name, pos_sigma) : Uid.MarkedString.info)
|
2022-09-30 19:30:06 +03:00
|
|
|
(mark : 'm mark)
|
2022-11-17 19:13:35 +03:00
|
|
|
(scope_sig : 'm scope_sig_ctx) :
|
2022-11-07 15:50:28 +03:00
|
|
|
'm Ast.expr scope_body_expr Bindlib.box * 'm ctx =
|
2021-12-09 13:58:42 +03:00
|
|
|
let scope_lets, new_ctx =
|
2021-10-28 16:24:39 +03:00
|
|
|
List.fold_left
|
2021-12-09 13:58:42 +03:00
|
|
|
(fun (scope_lets, ctx) rule ->
|
|
|
|
let new_scope_lets, new_ctx =
|
|
|
|
translate_rule ctx rule (sigma_name, pos_sigma)
|
|
|
|
in
|
2022-04-02 15:51:11 +03:00
|
|
|
(fun next -> scope_lets (new_scope_lets next)), new_ctx)
|
|
|
|
((fun next -> next), ctx)
|
|
|
|
rules
|
2021-10-28 16:24:39 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope_sig_decl = ScopeName.Map.find scope_name ctx.decl_ctx.ctx_scopes in
|
2021-10-28 16:24:39 +03:00
|
|
|
let return_exp =
|
2023-08-10 17:52:39 +03:00
|
|
|
Expr.estruct ~name:scope_sig.scope_sig_output_struct
|
|
|
|
~fields:
|
|
|
|
(ScopeVar.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun var (dcalc_var, _, io) acc ->
|
2023-05-17 16:44:57 +03:00
|
|
|
if Mark.remove io.Desugared.Ast.io_output then
|
2023-08-10 17:52:39 +03:00
|
|
|
let field =
|
|
|
|
ScopeVar.Map.find var scope_sig_decl.out_struct_fields
|
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.Map.add field
|
2022-11-17 19:13:35 +03:00
|
|
|
(Expr.make_var dcalc_var (mark_tany mark pos_sigma))
|
|
|
|
acc
|
|
|
|
else acc)
|
2022-11-21 12:12:45 +03:00
|
|
|
new_ctx.scope_vars StructField.Map.empty)
|
2022-11-17 19:13:35 +03:00
|
|
|
(mark_tany mark pos_sigma)
|
2021-10-28 16:24:39 +03:00
|
|
|
in
|
2022-04-02 15:51:11 +03:00
|
|
|
( scope_lets
|
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
|
|
|
(Bindlib.box_apply
|
|
|
|
(fun return_exp -> Result return_exp)
|
2022-10-21 16:33:05 +03:00
|
|
|
(Expr.Box.lift return_exp)),
|
2022-04-02 15:51:11 +03:00
|
|
|
new_ctx )
|
2020-11-26 15:38:42 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
(* From a scope declaration and definitions, create the corresponding scope body
|
|
|
|
wrapped in the appropriate call convention. *)
|
2020-12-04 18:40:17 +03:00
|
|
|
let translate_scope_decl
|
2023-01-23 14:19:36 +03:00
|
|
|
(ctx : 'm ctx)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_name : ScopeName.t)
|
2023-10-09 13:48:43 +03:00
|
|
|
(sigma : 'm Scopelang.Ast.scope_decl) =
|
2022-08-12 23:42:39 +03:00
|
|
|
let sigma_info = ScopeName.get_info sigma.scope_decl_name in
|
2023-01-23 14:19:36 +03:00
|
|
|
let scope_sig =
|
2023-08-10 17:52:39 +03:00
|
|
|
ScopeName.Map.find sigma.scope_decl_name ctx.scopes_parameters.scope_sigs
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
2022-01-31 20:09:14 +03:00
|
|
|
let scope_variables = scope_sig.scope_sig_local_vars in
|
2023-01-23 14:19:36 +03:00
|
|
|
let ctx = { ctx with scope_name = Some scope_name } in
|
2022-02-09 17:34:13 +03:00
|
|
|
let ctx =
|
|
|
|
(* the context must be initialized for fresh variables for all only-input
|
|
|
|
scope variables *)
|
|
|
|
List.fold_left
|
|
|
|
(fun ctx scope_var ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove scope_var.scope_var_io.io_input with
|
2022-02-09 17:34:13 +03:00
|
|
|
| OnlyInput ->
|
2022-08-17 18:14:29 +03:00
|
|
|
let scope_var_name = ScopeVar.get_info scope_var.scope_var_name in
|
2023-05-17 16:44:57 +03:00
|
|
|
let scope_var_dcalc = Var.make (Mark.remove scope_var_name) in
|
2022-02-09 17:34:13 +03:00
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
scope_vars =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.add scope_var.scope_var_name
|
2022-02-09 17:34:13 +03:00
|
|
|
( scope_var_dcalc,
|
|
|
|
scope_var.scope_var_typ,
|
|
|
|
scope_var.scope_var_io )
|
|
|
|
ctx.scope_vars;
|
|
|
|
}
|
|
|
|
| _ -> ctx)
|
2023-01-23 14:19:36 +03:00
|
|
|
ctx scope_variables
|
2022-02-09 17:34:13 +03:00
|
|
|
in
|
2023-03-10 11:36:05 +03:00
|
|
|
let date_rounding : date_rounding =
|
2023-03-10 11:49:16 +03:00
|
|
|
match
|
|
|
|
List.find_opt
|
2023-03-16 20:51:01 +03:00
|
|
|
(function Desugared.Ast.DateRounding _, _ -> true)
|
2023-03-10 11:49:16 +03:00
|
|
|
sigma.scope_options
|
|
|
|
with
|
2023-03-16 20:51:01 +03:00
|
|
|
| Some (Desugared.Ast.DateRounding Desugared.Ast.Increasing, _) -> RoundUp
|
|
|
|
| Some (DateRounding Decreasing, _) -> RoundDown
|
2023-03-10 11:49:16 +03:00
|
|
|
| None -> AbortOnRound
|
2023-01-20 20:18:53 +03:00
|
|
|
in
|
|
|
|
let ctx = { ctx with date_rounding } in
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope_input_var =
|
|
|
|
Var.make (Mark.remove (ScopeName.get_info scope_name) ^ "_in")
|
|
|
|
in
|
2022-01-31 20:09:14 +03:00
|
|
|
let scope_input_struct_name = scope_sig.scope_sig_input_struct in
|
|
|
|
let scope_return_struct_name = scope_sig.scope_sig_output_struct in
|
2023-05-17 16:44:57 +03:00
|
|
|
let pos_sigma = Mark.get sigma_info in
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope_mark =
|
|
|
|
(* Find a witness of a mark in the definitions *)
|
|
|
|
match sigma.scope_decl_rules with
|
|
|
|
| [] ->
|
|
|
|
(* Todo: are we sure this can't happen in normal code ? E.g. is calling a
|
|
|
|
scope which only defines input variables already an error at this stage
|
|
|
|
or not ? *)
|
|
|
|
Message.raise_spanned_error pos_sigma "Scope %a has no content"
|
|
|
|
ScopeName.format scope_name
|
|
|
|
| (Definition (_, _, _, (_, m)) | Assertion (_, m) | Call (_, _, m)) :: _ ->
|
|
|
|
m
|
|
|
|
in
|
2022-04-02 15:51:11 +03:00
|
|
|
let rules_with_return_expr, ctx =
|
2023-08-10 17:52:39 +03:00
|
|
|
translate_rules ctx scope_name sigma.scope_decl_rules sigma_info scope_mark
|
2022-11-17 19:13:35 +03:00
|
|
|
scope_sig
|
2021-12-09 13:58:42 +03:00
|
|
|
in
|
2020-11-27 13:37:21 +03:00
|
|
|
let scope_variables =
|
|
|
|
List.map
|
2022-01-31 20:09:14 +03:00
|
|
|
(fun var_ctx ->
|
2022-02-05 02:04:19 +03:00
|
|
|
let dcalc_x, _, _ =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.find var_ctx.scope_var_name ctx.scope_vars
|
2022-02-05 02:04:19 +03:00
|
|
|
in
|
2022-01-31 20:09:14 +03:00
|
|
|
var_ctx, dcalc_x)
|
2020-11-27 13:37:21 +03:00
|
|
|
scope_variables
|
|
|
|
in
|
2021-02-01 17:57:19 +03:00
|
|
|
(* first we create variables from the fields of the input struct *)
|
2022-02-05 02:04:19 +03:00
|
|
|
let scope_input_variables =
|
2022-02-07 12:30:36 +03:00
|
|
|
List.filter
|
|
|
|
(fun (var_ctx, _) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove var_ctx.scope_var_io.io_input with
|
2022-02-07 12:30:36 +03:00
|
|
|
| NoInput -> false
|
|
|
|
| _ -> true)
|
|
|
|
scope_variables
|
2022-02-05 02:04:19 +03:00
|
|
|
in
|
2022-05-31 19:38:14 +03:00
|
|
|
let input_destructurings next =
|
2022-11-17 19:13:35 +03:00
|
|
|
List.fold_right
|
|
|
|
(fun (var_ctx, v) next ->
|
|
|
|
let field =
|
2022-12-07 17:32:08 +03:00
|
|
|
(ScopeVar.Map.find var_ctx.scope_var_name
|
|
|
|
scope_sig.scope_sig_in_fields)
|
2022-12-02 18:42:29 +03:00
|
|
|
.scope_input_name
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun next r ->
|
|
|
|
ScopeLet
|
|
|
|
{
|
|
|
|
scope_let_kind = DestructuringInputStruct;
|
|
|
|
scope_let_next = next;
|
|
|
|
scope_let_pos = pos_sigma;
|
2023-10-12 15:41:57 +03:00
|
|
|
scope_let_typ =
|
|
|
|
input_var_typ var_ctx.scope_var_typ var_ctx.scope_var_io;
|
2022-11-17 19:13:35 +03:00
|
|
|
scope_let_expr =
|
|
|
|
( EStructAccess
|
|
|
|
{ name = scope_input_struct_name; e = r; field },
|
2023-08-10 17:52:39 +03:00
|
|
|
mark_tany scope_mark pos_sigma );
|
2022-11-17 19:13:35 +03:00
|
|
|
})
|
|
|
|
(Bindlib.bind_var v next)
|
|
|
|
(Expr.Box.lift
|
2023-08-10 17:52:39 +03:00
|
|
|
(Expr.make_var scope_input_var (mark_tany scope_mark pos_sigma))))
|
2022-11-17 19:13:35 +03:00
|
|
|
scope_input_variables next
|
2021-02-01 17:57:19 +03:00
|
|
|
in
|
2023-10-09 13:48:43 +03:00
|
|
|
Bindlib.box_apply
|
|
|
|
(fun scope_body_expr ->
|
2023-10-12 15:41:57 +03:00
|
|
|
{
|
|
|
|
scope_body_expr;
|
|
|
|
scope_body_input_struct = scope_input_struct_name;
|
|
|
|
scope_body_output_struct = scope_return_struct_name;
|
|
|
|
})
|
2023-10-09 13:48:43 +03:00
|
|
|
(Bindlib.bind_var scope_input_var
|
|
|
|
(input_destructurings rules_with_return_expr))
|
2020-11-27 13:37:21 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let translate_program (prgm : 'm Scopelang.Ast.program) : 'm Ast.program =
|
2023-02-13 17:00:23 +03:00
|
|
|
let defs_dependencies = Scopelang.Dependency.build_program_dep_graph prgm in
|
|
|
|
Scopelang.Dependency.check_for_cycle_in_defs defs_dependencies;
|
|
|
|
let defs_ordering =
|
|
|
|
Scopelang.Dependency.get_defs_ordering defs_dependencies
|
2022-11-07 15:50:28 +03:00
|
|
|
in
|
2022-08-25 13:09:51 +03:00
|
|
|
let decl_ctx = prgm.program_ctx in
|
2022-09-30 17:52:35 +03:00
|
|
|
let sctx : 'm scope_sigs_ctx =
|
2023-08-30 18:49:29 +03:00
|
|
|
let process_scope_sig scope_name scope =
|
|
|
|
let scope_path = ScopeName.path scope_name in
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope_ref =
|
2023-08-30 18:49:29 +03:00
|
|
|
if scope_path = [] then
|
2023-08-10 17:52:39 +03:00
|
|
|
let v = Var.make (Mark.remove (ScopeName.get_info scope_name)) in
|
|
|
|
Local_scope_ref v
|
2023-08-30 18:49:29 +03:00
|
|
|
else
|
2023-08-10 17:52:39 +03:00
|
|
|
External_scope_ref
|
2023-08-30 18:49:29 +03:00
|
|
|
(Mark.copy (ScopeName.get_info scope_name) scope_name)
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
|
|
|
let scope_info =
|
|
|
|
try
|
|
|
|
ScopeName.Map.find scope_name
|
|
|
|
(Program.module_ctx decl_ctx scope_path).ctx_scopes
|
2023-08-15 17:57:52 +03:00
|
|
|
with ScopeName.Map.Not_found _ ->
|
|
|
|
Message.raise_spanned_error
|
|
|
|
(Mark.get (ScopeName.get_info scope_name))
|
2023-08-30 18:49:29 +03:00
|
|
|
"Could not find scope %a" ScopeName.format scope_name
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
|
|
|
let scope_sig_in_fields =
|
|
|
|
(* Output fields have already been generated and added to the program
|
|
|
|
ctx at this point, because they are visible to the user (manipulated
|
|
|
|
as the return type of ScopeCalls) ; but input fields are used purely
|
|
|
|
internally and need to be created here to implement the call
|
|
|
|
convention for scopes. *)
|
2023-11-03 19:15:55 +03:00
|
|
|
let module S = Scopelang.Ast in
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.filter_map
|
2023-11-03 19:15:55 +03:00
|
|
|
(fun dvar svar ->
|
|
|
|
match Mark.remove svar.S.svar_io.Desugared.Ast.io_input with
|
2022-10-24 19:25:20 +03:00
|
|
|
| NoInput -> None
|
|
|
|
| OnlyInput | Reentrant ->
|
|
|
|
let info = ScopeVar.get_info dvar in
|
2023-05-17 16:44:57 +03:00
|
|
|
let s = Mark.remove info ^ "_in" in
|
|
|
|
Some
|
2022-02-07 12:30:36 +03:00
|
|
|
{
|
|
|
|
scope_input_name = StructField.fresh (s, Mark.get info);
|
2023-11-03 19:15:55 +03:00
|
|
|
scope_input_io = svar.S.svar_io.Desugared.Ast.io_input;
|
2023-10-12 15:41:57 +03:00
|
|
|
scope_input_typ =
|
2023-11-07 20:25:57 +03:00
|
|
|
Mark.remove
|
|
|
|
(input_var_typ
|
|
|
|
(Mark.remove svar.S.svar_in_ty)
|
|
|
|
svar.S.svar_io);
|
2023-10-12 15:41:57 +03:00
|
|
|
scope_input_thunked =
|
2023-11-07 20:25:57 +03:00
|
|
|
input_var_needs_thunking
|
|
|
|
(Mark.remove svar.S.svar_in_ty)
|
|
|
|
svar.S.svar_io;
|
2022-02-07 12:30:36 +03:00
|
|
|
})
|
2023-11-03 19:15:55 +03:00
|
|
|
scope.S.scope_sig
|
2023-08-16 01:04:45 +03:00
|
|
|
in
|
|
|
|
{
|
2022-01-31 20:09:14 +03:00
|
|
|
scope_sig_local_vars =
|
2023-08-16 01:04:45 +03:00
|
|
|
List.map
|
2023-11-03 19:15:55 +03:00
|
|
|
(fun (scope_var, svar) ->
|
2023-08-16 01:04:45 +03:00
|
|
|
{
|
2023-05-17 16:44:57 +03:00
|
|
|
scope_var_name = scope_var;
|
2023-11-03 19:15:55 +03:00
|
|
|
scope_var_typ = Mark.remove svar.Scopelang.Ast.svar_in_ty;
|
|
|
|
scope_var_io = svar.Scopelang.Ast.svar_io;
|
2023-08-16 01:04:45 +03:00
|
|
|
})
|
2022-11-21 12:12:45 +03:00
|
|
|
(ScopeVar.Map.bindings scope.scope_sig);
|
2023-08-10 17:52:39 +03:00
|
|
|
scope_sig_scope_ref = scope_ref;
|
|
|
|
scope_sig_input_struct = scope_info.in_struct_name;
|
|
|
|
scope_sig_output_struct = scope_info.out_struct_name;
|
2022-10-24 19:25:20 +03:00
|
|
|
scope_sig_in_fields;
|
2023-08-10 17:52:39 +03:00
|
|
|
}
|
|
|
|
in
|
2023-08-30 18:49:29 +03:00
|
|
|
let rec process_modules prg =
|
2023-08-10 17:52:39 +03:00
|
|
|
{
|
|
|
|
scope_sigs =
|
|
|
|
ScopeName.Map.mapi
|
|
|
|
(fun scope_name (scope_decl, _) ->
|
2023-08-30 18:49:29 +03:00
|
|
|
process_scope_sig scope_name scope_decl)
|
2023-08-10 17:52:39 +03:00
|
|
|
prg.Scopelang.Ast.program_scopes;
|
|
|
|
scope_sigs_modules =
|
|
|
|
ModuleName.Map.map process_modules prg.Scopelang.Ast.program_modules;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
{
|
|
|
|
scope_sigs =
|
|
|
|
ScopeName.Map.mapi
|
|
|
|
(fun scope_name (scope_decl, _) ->
|
2023-08-30 18:49:29 +03:00
|
|
|
process_scope_sig scope_name scope_decl)
|
2023-08-10 17:52:39 +03:00
|
|
|
prgm.Scopelang.Ast.program_scopes;
|
|
|
|
scope_sigs_modules =
|
|
|
|
ModuleName.Map.map process_modules prgm.Scopelang.Ast.program_modules;
|
|
|
|
}
|
|
|
|
in
|
2023-10-09 13:48:43 +03:00
|
|
|
let add_scope_in_structs scope_sigs structs =
|
|
|
|
ScopeName.Map.fold
|
|
|
|
(fun _ scope_sig_ctx acc ->
|
2023-10-12 15:41:57 +03:00
|
|
|
let fields =
|
|
|
|
ScopeVar.Map.fold
|
|
|
|
(fun _ sivc acc ->
|
|
|
|
let pos = Mark.get (StructField.get_info sivc.scope_input_name) in
|
|
|
|
StructField.Map.add sivc.scope_input_name
|
|
|
|
(sivc.scope_input_typ, pos)
|
|
|
|
acc)
|
|
|
|
scope_sig_ctx.scope_sig_in_fields StructField.Map.empty
|
|
|
|
in
|
|
|
|
StructName.Map.add scope_sig_ctx.scope_sig_input_struct fields acc)
|
2023-10-09 13:48:43 +03:00
|
|
|
scope_sigs.scope_sigs structs
|
|
|
|
in
|
2023-08-30 18:49:29 +03:00
|
|
|
let rec gather_module_in_structs acc sctx =
|
2023-08-10 17:52:39 +03:00
|
|
|
(* Expose all added in_structs from submodules at toplevel *)
|
|
|
|
ModuleName.Map.fold
|
2023-08-30 18:49:29 +03:00
|
|
|
(fun _ scope_sigs acc ->
|
2023-10-09 13:48:43 +03:00
|
|
|
add_scope_in_structs scope_sigs
|
|
|
|
(gather_module_in_structs acc scope_sigs.scope_sigs_modules))
|
2023-08-10 17:52:39 +03:00
|
|
|
sctx acc
|
2023-08-16 01:04:45 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let decl_ctx =
|
2023-08-16 01:04:45 +03:00
|
|
|
{
|
2023-08-10 17:52:39 +03:00
|
|
|
decl_ctx with
|
|
|
|
ctx_structs =
|
2023-10-09 13:48:43 +03:00
|
|
|
add_scope_in_structs sctx
|
|
|
|
(gather_module_in_structs decl_ctx.ctx_structs sctx.scope_sigs_modules);
|
2023-08-16 01:04:45 +03:00
|
|
|
}
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
|
|
|
let top_ctx =
|
2023-02-13 17:00:23 +03:00
|
|
|
let toplevel_vars =
|
2023-01-23 14:19:36 +03:00
|
|
|
TopdefName.Map.mapi
|
|
|
|
(fun name (_, ty) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Var.make (Mark.remove (TopdefName.get_info name)), Mark.remove ty)
|
2023-02-13 17:00:23 +03:00
|
|
|
prgm.Scopelang.Ast.program_topdefs
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
|
|
|
{
|
2023-08-10 17:52:39 +03:00
|
|
|
decl_ctx;
|
2023-01-23 14:19:36 +03:00
|
|
|
scope_name = None;
|
|
|
|
scopes_parameters = sctx;
|
|
|
|
scope_vars = ScopeVar.Map.empty;
|
|
|
|
subscope_vars = SubScopeName.Map.empty;
|
2023-02-13 17:00:23 +03:00
|
|
|
toplevel_vars;
|
2023-01-20 20:18:53 +03:00
|
|
|
date_rounding = AbortOnRound;
|
2023-01-23 14:19:36 +03:00
|
|
|
}
|
2020-11-27 13:37:21 +03:00
|
|
|
in
|
2020-11-25 20:00:34 +03:00
|
|
|
(* the resulting expression is the list of definitions of all the scopes,
|
2022-10-24 19:25:20 +03:00
|
|
|
ending with the top-level scope. The decl_ctx is filled in left-to-right
|
2022-10-21 16:47:17 +03:00
|
|
|
order, then the chained scopes aggregated from the right. *)
|
2023-01-23 14:19:36 +03:00
|
|
|
let rec translate_defs ctx = function
|
2023-10-09 13:48:43 +03:00
|
|
|
| [] -> Bindlib.box Nil
|
2023-01-23 14:19:36 +03:00
|
|
|
| def :: next ->
|
2023-10-09 13:48:43 +03:00
|
|
|
let dvar, def =
|
2023-01-23 14:19:36 +03:00
|
|
|
match def with
|
2023-02-13 17:00:23 +03:00
|
|
|
| Scopelang.Dependency.Topdef gname ->
|
|
|
|
let expr, ty = TopdefName.Map.find gname prgm.program_topdefs in
|
2023-01-23 14:19:36 +03:00
|
|
|
let expr = translate_expr ctx expr in
|
2023-10-09 13:48:43 +03:00
|
|
|
( fst (TopdefName.Map.find gname ctx.toplevel_vars),
|
2023-01-23 14:19:36 +03:00
|
|
|
Bindlib.box_apply
|
|
|
|
(fun e -> Topdef (gname, ty, e))
|
|
|
|
(Expr.Box.lift expr) )
|
|
|
|
| Scopelang.Dependency.Scope scope_name ->
|
|
|
|
let scope = ScopeName.Map.find scope_name prgm.program_scopes in
|
2023-10-09 13:48:43 +03:00
|
|
|
let scope_body =
|
2023-08-10 17:52:39 +03:00
|
|
|
translate_scope_decl ctx scope_name (Mark.remove scope)
|
|
|
|
in
|
|
|
|
let scope_var =
|
|
|
|
match
|
|
|
|
(ScopeName.Map.find scope_name sctx.scope_sigs)
|
|
|
|
.scope_sig_scope_ref
|
|
|
|
with
|
|
|
|
| Local_scope_ref v -> v
|
|
|
|
| External_scope_ref _ -> assert false
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
2023-10-09 13:48:43 +03:00
|
|
|
( scope_var,
|
2023-01-23 14:19:36 +03:00
|
|
|
Bindlib.box_apply
|
|
|
|
(fun body -> ScopeDef (scope_name, body))
|
|
|
|
scope_body )
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
2023-10-09 13:48:43 +03:00
|
|
|
let scope_next = translate_defs ctx next in
|
2023-01-23 14:19:36 +03:00
|
|
|
let next_bind = Bindlib.bind_var dvar scope_next in
|
2023-10-09 13:48:43 +03:00
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun item next_bind -> Cons (item, next_bind))
|
|
|
|
def next_bind
|
2021-01-28 15:58:59 +03:00
|
|
|
in
|
2023-10-09 13:48:43 +03:00
|
|
|
let items = translate_defs top_ctx defs_ordering in
|
2023-10-13 16:09:00 +03:00
|
|
|
Expr.Box.assert_closed items;
|
2023-09-22 18:50:19 +03:00
|
|
|
{
|
|
|
|
code_items = Bindlib.unbox items;
|
2023-10-09 13:48:43 +03:00
|
|
|
decl_ctx;
|
2023-09-19 12:44:18 +03:00
|
|
|
module_name = prgm.Scopelang.Ast.program_module_name;
|
2023-09-22 18:50:19 +03:00
|
|
|
lang = prgm.program_lang;
|
|
|
|
}
|