2021-02-12 19:20:14 +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:
|
|
|
|
Nicolas Chataing <nicolas.chataing@ens.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. *)
|
|
|
|
|
|
|
|
(** Abstract syntax tree of the desugared representation *)
|
|
|
|
|
|
|
|
open Utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
(** Inside a scope, a definition can refer either to a scope def, or a subscope
|
|
|
|
def *)
|
|
|
|
module ScopeDef : sig
|
|
|
|
type t =
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var of ScopeVar.t * StateName.t option
|
2022-09-20 15:04:07 +03:00
|
|
|
| SubScopeVar of SubScopeName.t * ScopeVar.t * Pos.t
|
2021-02-12 20:16:06 +03:00
|
|
|
|
2021-02-12 19:20:14 +03:00
|
|
|
val compare : t -> t -> int
|
|
|
|
val get_position : t -> Pos.t
|
|
|
|
val format_t : Format.formatter -> t -> unit
|
|
|
|
val hash : t -> int
|
|
|
|
end
|
|
|
|
|
|
|
|
module ScopeDefMap : Map.S with type key = ScopeDef.t
|
|
|
|
module ScopeDefSet : Set.S with type elt = ScopeDef.t
|
|
|
|
|
|
|
|
(** {1 AST} *)
|
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
(** {2 Expressions} *)
|
2022-02-28 19:19:06 +03:00
|
|
|
|
2022-08-26 16:21:47 +03:00
|
|
|
type expr = (desugared, untyped mark) gexpr
|
2022-08-25 17:31:32 +03:00
|
|
|
(** See {!type:Shared_ast.naked_gexpr} for the complete definition *)
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
type location = desugared glocation
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
module LocationSet : Set.S with type elt = location Marked.pos
|
2022-08-25 17:35:08 +03:00
|
|
|
module ExprMap : Map.S with type key = expr
|
2022-02-28 20:34:32 +03:00
|
|
|
|
|
|
|
(** {2 Rules and scopes}*)
|
|
|
|
|
2022-07-13 16:00:57 +03:00
|
|
|
type exception_situation =
|
|
|
|
| BaseCase
|
|
|
|
| ExceptionToLabel of LabelName.t Marked.pos
|
|
|
|
| ExceptionToRule of RuleName.t Marked.pos
|
|
|
|
|
|
|
|
type label_situation = ExplicitlyLabeled of LabelName.t Marked.pos | Unlabeled
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type rule = {
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id : RuleName.t;
|
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
|
|
|
rule_just : expr boxed;
|
|
|
|
rule_cons : expr boxed;
|
2022-08-25 20:46:13 +03:00
|
|
|
rule_parameter : (expr Var.t * typ) option;
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_exception : exception_situation;
|
|
|
|
rule_label : label_situation;
|
2021-02-11 20:48:59 +03:00
|
|
|
}
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-05-25 15:41:04 +03:00
|
|
|
module Rule : Set.OrderedType with type t = rule
|
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
val empty_rule : Pos.t -> typ option -> rule
|
|
|
|
val always_false_rule : Pos.t -> typ option -> rule
|
2021-02-12 19:20:14 +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
|
|
|
type assertion = expr boxed
|
2021-02-11 20:48:59 +03:00
|
|
|
type variation_typ = Increasing | Decreasing
|
|
|
|
type reference_typ = Decree | Law
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type meta_assertion =
|
2022-05-30 12:20:48 +03:00
|
|
|
| FixedBy of reference_typ Marked.pos
|
|
|
|
| VariesWith of unit * variation_typ Marked.pos option
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
(** This type characterizes the three levels of visibility for a given scope
|
|
|
|
variable with regards to the scope's input and possible redefinitions inside
|
|
|
|
the scope.. *)
|
|
|
|
type io_input =
|
|
|
|
| NoInput
|
|
|
|
(** For an internal variable defined only in the scope, and does not
|
|
|
|
appear in the input. *)
|
|
|
|
| OnlyInput
|
|
|
|
(** For variables that should not be redefined in the scope, because they
|
|
|
|
appear in the input. *)
|
|
|
|
| Reentrant
|
|
|
|
(** For variables defined in the scope that can also be redefined by the
|
|
|
|
caller as they appear in the input. *)
|
|
|
|
|
|
|
|
type io = {
|
|
|
|
io_output : bool Marked.pos;
|
|
|
|
(** [true] is present in the output of the scope. *)
|
|
|
|
io_input : io_input Marked.pos;
|
|
|
|
}
|
|
|
|
(** Characterization of the input/output status of a scope variable. *)
|
|
|
|
|
2022-01-04 20:19:15 +03:00
|
|
|
type scope_def = {
|
|
|
|
scope_def_rules : rule RuleMap.t;
|
2022-08-25 18:29:00 +03:00
|
|
|
scope_def_typ : typ;
|
2022-01-04 20:19:15 +03:00
|
|
|
scope_def_is_condition : bool;
|
2022-11-07 15:50:28 +03:00
|
|
|
scope_def_io : io;
|
2022-01-04 20:19:15 +03:00
|
|
|
}
|
|
|
|
|
2022-02-28 19:19:06 +03:00
|
|
|
type var_or_states = WholeVar | States of StateName.t list
|
|
|
|
|
2021-02-11 20:48:59 +03:00
|
|
|
type scope = {
|
2022-02-28 19:19:06 +03:00
|
|
|
scope_vars : var_or_states ScopeVarMap.t;
|
2022-09-14 16:36:24 +03:00
|
|
|
scope_sub_scopes : ScopeName.t SubScopeMap.t;
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_uid : ScopeName.t;
|
2022-01-04 20:19:15 +03:00
|
|
|
scope_defs : scope_def ScopeDefMap.t;
|
2021-02-11 20:48:59 +03:00
|
|
|
scope_assertions : assertion list;
|
|
|
|
scope_meta_assertions : meta_assertion list;
|
|
|
|
}
|
2021-02-12 19:20:14 +03:00
|
|
|
|
2022-09-14 16:36:24 +03:00
|
|
|
type program = { program_scopes : scope ScopeMap.t; program_ctx : decl_ctx }
|
2021-02-12 19:20:14 +03:00
|
|
|
|
|
|
|
(** {1 Helpers} *)
|
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
val locations_used : expr -> LocationSet.t
|
2021-02-12 19:20:14 +03:00
|
|
|
val free_variables : rule RuleMap.t -> Pos.t ScopeDefMap.t
|