2020-06-22 17:16:55 +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. *)
|
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Abstract syntax tree of the desugared representation *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2020-11-23 14:20:38 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** {1 Names, Maps and Keys} *)
|
|
|
|
|
2020-11-23 14:20:38 +03:00
|
|
|
(** Inside a scope, a definition can refer either to a scope def, or a subscope
|
|
|
|
def *)
|
|
|
|
module ScopeDef = struct
|
2023-04-18 11:31:44 +03:00
|
|
|
module Base = struct
|
2024-03-28 19:05:12 +03:00
|
|
|
type kind =
|
|
|
|
| Var of StateName.t option
|
2024-04-10 19:29:48 +03:00
|
|
|
| SubScopeInput of {
|
|
|
|
name : ScopeName.t;
|
|
|
|
var_within_origin_scope : ScopeVar.t;
|
|
|
|
}
|
2024-03-28 19:05:12 +03:00
|
|
|
|
|
|
|
type t = ScopeVar.t Mark.pos * kind
|
|
|
|
|
2024-04-04 11:56:56 +03:00
|
|
|
let equal_kind k1 k2 =
|
|
|
|
match k1, k2 with
|
2024-03-29 20:39:20 +03:00
|
|
|
| Var s1, Var s2 -> Option.equal StateName.equal s1 s2
|
2024-04-10 19:29:48 +03:00
|
|
|
| ( SubScopeInput { var_within_origin_scope = v1; _ },
|
|
|
|
SubScopeInput { var_within_origin_scope = v2; _ } ) ->
|
2024-04-04 11:56:56 +03:00
|
|
|
ScopeVar.equal v1 v2
|
2024-04-10 19:29:48 +03:00
|
|
|
| (Var _ | SubScopeInput _), _ -> false
|
2024-03-29 20:39:20 +03:00
|
|
|
|
|
|
|
let equal (v1, k1) (v2, k2) =
|
2024-04-04 11:56:56 +03:00
|
|
|
ScopeVar.equal (Mark.remove v1) (Mark.remove v2) && equal_kind k1 k2
|
2024-03-29 20:39:20 +03:00
|
|
|
|
2024-04-04 11:56:56 +03:00
|
|
|
let compare_kind k1 k2 =
|
|
|
|
match k1, k2 with
|
|
|
|
| Var st1, Var st2 -> Option.compare StateName.compare st1 st2
|
2024-04-10 19:29:48 +03:00
|
|
|
| ( SubScopeInput { var_within_origin_scope = v1; _ },
|
|
|
|
SubScopeInput { var_within_origin_scope = v2; _ } ) ->
|
2024-04-04 11:56:56 +03:00
|
|
|
ScopeVar.compare v1 v2
|
2024-04-10 19:29:48 +03:00
|
|
|
| Var _, SubScopeInput _ -> -1
|
|
|
|
| SubScopeInput _, Var _ -> 1
|
2024-03-28 19:05:12 +03:00
|
|
|
|
|
|
|
let compare (v1, k1) (v2, k2) =
|
|
|
|
match Mark.compare ScopeVar.compare v1 v2 with
|
|
|
|
| 0 -> compare_kind k1 k2
|
|
|
|
| n -> n
|
|
|
|
|
|
|
|
let get_position (v, _) = Mark.get v
|
|
|
|
|
2024-03-29 20:39:20 +03:00
|
|
|
let format_kind ppf = function
|
2024-03-28 19:05:12 +03:00
|
|
|
| Var None -> ()
|
2024-03-29 20:39:20 +03:00
|
|
|
| Var (Some st) -> Format.fprintf ppf "@%a" StateName.format st
|
2024-04-10 19:29:48 +03:00
|
|
|
| SubScopeInput { var_within_origin_scope = v; _ } ->
|
2024-04-04 11:56:56 +03:00
|
|
|
Format.fprintf ppf ".%a" ScopeVar.format v
|
2024-03-28 19:05:12 +03:00
|
|
|
|
2024-03-29 20:39:20 +03:00
|
|
|
let format ppf (v, k) =
|
|
|
|
ScopeVar.format ppf (Mark.remove v);
|
|
|
|
format_kind ppf k
|
|
|
|
|
2024-05-24 18:24:14 +03:00
|
|
|
open Hash.Op
|
|
|
|
|
|
|
|
let hash_kind ~strip = function
|
|
|
|
| Var v -> !`Var % Hash.option StateName.hash v
|
|
|
|
| SubScopeInput { name; var_within_origin_scope } ->
|
|
|
|
!`SubScopeInput
|
|
|
|
% ScopeName.hash ~strip name
|
|
|
|
% ScopeVar.hash var_within_origin_scope
|
2024-03-29 20:39:20 +03:00
|
|
|
|
2024-05-24 18:24:14 +03:00
|
|
|
let hash ~strip (v, k) =
|
|
|
|
Hash.Op.(ScopeVar.hash (Mark.remove v) % hash_kind ~strip k)
|
2023-04-18 11:31:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
include Base
|
|
|
|
module Map = Map.Make (Base)
|
|
|
|
module Set = Set.Make (Base)
|
2020-11-23 14:20:38 +03:00
|
|
|
end
|
|
|
|
|
2023-09-01 11:43:46 +03:00
|
|
|
module AssertionName =
|
|
|
|
Uid.Gen
|
|
|
|
(struct
|
|
|
|
let style = Ocolor_types.(Fg (C4 hi_blue))
|
|
|
|
end)
|
|
|
|
()
|
2023-04-28 15:15:43 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** {1 AST} *)
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
type location = desugared glocation
|
2022-02-28 19:19:06 +03:00
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
module LocationSet : Set.S with type elt = location Mark.pos = Set.Make (struct
|
|
|
|
type t = location Mark.pos
|
2022-02-28 19:19:06 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
let compare = Expr.compare_location
|
2022-02-28 19:19:06 +03:00
|
|
|
end)
|
|
|
|
|
2023-05-17 17:15:00 +03:00
|
|
|
type expr = (desugared, untyped) gexpr
|
2022-05-25 15:41:04 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
module ExprMap = Map.Make (struct
|
2022-08-25 17:35:08 +03:00
|
|
|
type t = expr
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
let compare = Expr.compare
|
2023-07-12 12:48:46 +03:00
|
|
|
let format = Expr.format
|
2022-08-25 17:08:08 +03:00
|
|
|
end)
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2023-05-26 17:54:52 +03:00
|
|
|
type io = { io_output : bool Mark.pos; io_input : Runtime.io_input Mark.pos }
|
2022-11-07 15:50:28 +03:00
|
|
|
|
2022-07-13 16:00:57 +03:00
|
|
|
type exception_situation =
|
|
|
|
| BaseCase
|
2023-05-17 16:44:57 +03:00
|
|
|
| ExceptionToLabel of LabelName.t Mark.pos
|
|
|
|
| ExceptionToRule of RuleName.t Mark.pos
|
2022-07-13 16:00:57 +03:00
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
type label_situation = ExplicitlyLabeled of LabelName.t Mark.pos | Unlabeled
|
2022-07-13 16:00:57 +03:00
|
|
|
|
2020-11-25 12:10:27 +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;
|
2023-05-17 16:44:57 +03:00
|
|
|
rule_parameter : (expr Var.t Mark.pos * typ) list Mark.pos option;
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_exception : exception_situation;
|
|
|
|
rule_label : label_situation;
|
2020-11-25 12:10:27 +03:00
|
|
|
}
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-05-25 15:41:04 +03:00
|
|
|
module Rule = struct
|
|
|
|
type t = rule
|
|
|
|
|
|
|
|
(** Structural equality (otherwise, you should just compare the [rule_id]
|
|
|
|
fields) *)
|
|
|
|
let compare r1 r2 =
|
|
|
|
match r1.rule_parameter, r2.rule_parameter with
|
|
|
|
| None, None -> (
|
2023-05-04 19:28:24 +03:00
|
|
|
let j1, j1m = r1.rule_just in
|
|
|
|
let j2, j2m = r2.rule_just in
|
|
|
|
match
|
|
|
|
Bindlib.unbox
|
|
|
|
(Bindlib.box_apply2
|
|
|
|
(fun j1 j2 -> Expr.compare (j1, j1m) (j2, j2m))
|
|
|
|
j1 j2)
|
|
|
|
with
|
2022-05-25 15:41:04 +03:00
|
|
|
| 0 ->
|
2023-05-04 19:28:24 +03:00
|
|
|
let c1, c1m = r1.rule_cons in
|
|
|
|
let c2, c2m = r2.rule_cons in
|
|
|
|
Bindlib.unbox
|
|
|
|
(Bindlib.box_apply2
|
|
|
|
(fun c1 c2 -> Expr.compare (c1, c1m) (c2, c2m))
|
|
|
|
c1 c2)
|
2022-05-25 15:41:04 +03:00
|
|
|
| n -> n)
|
2023-02-28 16:40:05 +03:00
|
|
|
| Some (l1, _), Some (l2, _) ->
|
|
|
|
ListLabels.compare l1 l2 ~cmp:(fun ((v1, _), t1) ((v2, _), t2) ->
|
2023-02-20 19:21:44 +03:00
|
|
|
match Type.compare t1 t2 with
|
|
|
|
| 0 -> (
|
|
|
|
let open Bindlib in
|
2023-05-04 19:28:24 +03:00
|
|
|
let b1 = bind_var v1 (Expr.Box.lift r1.rule_just) in
|
|
|
|
let b2 = bind_var v2 (Expr.Box.lift r2.rule_just) in
|
|
|
|
match
|
|
|
|
Bindlib.unbox
|
|
|
|
(Bindlib.box_apply2
|
|
|
|
(fun b1 b2 ->
|
|
|
|
let _, j1, j2 = unbind2 b1 b2 in
|
|
|
|
Expr.compare j1 j2)
|
|
|
|
b1 b2)
|
|
|
|
with
|
2023-02-20 19:21:44 +03:00
|
|
|
| 0 ->
|
2023-05-04 19:28:24 +03:00
|
|
|
let b1 = bind_var v1 (Expr.Box.lift r1.rule_cons) in
|
|
|
|
let b2 = bind_var v2 (Expr.Box.lift r2.rule_cons) in
|
|
|
|
Bindlib.unbox
|
|
|
|
(Bindlib.box_apply2
|
|
|
|
(fun b1 b2 ->
|
|
|
|
let _, c1, c2 = unbind2 b1 b2 in
|
|
|
|
Expr.compare c1 c2)
|
|
|
|
b1 b2)
|
2023-02-20 19:21:44 +03:00
|
|
|
| n -> n)
|
|
|
|
| n -> n)
|
2022-05-25 15:41:04 +03:00
|
|
|
| None, Some _ -> -1
|
|
|
|
| Some _, None -> 1
|
|
|
|
end
|
|
|
|
|
2023-02-27 11:50:42 +03:00
|
|
|
let empty_rule
|
|
|
|
(pos : Pos.t)
|
2023-05-17 16:44:57 +03:00
|
|
|
(parameters : (Uid.MarkedString.info * typ) list Mark.pos option) : rule =
|
2020-11-25 12:10:27 +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
|
|
|
rule_just = Expr.box (ELit (LBool false), Untyped { pos });
|
2024-04-26 19:31:26 +03:00
|
|
|
rule_cons = Expr.box (EEmpty, Untyped { pos });
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_parameter =
|
2023-02-27 11:50:42 +03:00
|
|
|
Option.map
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.map (List.map (fun (lbl, typ) -> Mark.map Var.make lbl, typ)))
|
2023-02-27 11:50:42 +03:00
|
|
|
parameters;
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_exception = BaseCase;
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id = RuleName.fresh ("empty", pos);
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_label = Unlabeled;
|
2020-11-25 12:10:27 +03:00
|
|
|
}
|
2020-08-10 00:01:42 +03:00
|
|
|
|
2023-02-27 11:50:42 +03:00
|
|
|
let always_false_rule
|
|
|
|
(pos : Pos.t)
|
2023-05-17 16:44:57 +03:00
|
|
|
(parameters : (Uid.MarkedString.info * typ) list Mark.pos option) : rule =
|
2020-12-31 02:28:26 +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
|
|
|
rule_just = Expr.box (ELit (LBool true), Untyped { pos });
|
|
|
|
rule_cons = Expr.box (ELit (LBool false), Untyped { pos });
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_parameter =
|
2023-02-27 11:50:42 +03:00
|
|
|
Option.map
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.map (List.map (fun (lbl, typ) -> Mark.map Var.make lbl, typ)))
|
2023-02-27 11:50:42 +03:00
|
|
|
parameters;
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_exception = BaseCase;
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id = RuleName.fresh ("always_false", pos);
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_label = Unlabeled;
|
2020-12-31 02:28:26 +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
|
2020-06-22 17:16:55 +03:00
|
|
|
type variation_typ = Increasing | Decreasing
|
|
|
|
type reference_typ = Decree | Law
|
2023-01-20 20:18:53 +03:00
|
|
|
type catala_option = DateRounding of variation_typ
|
2020-06-22 17:16:55 +03:00
|
|
|
|
|
|
|
type meta_assertion =
|
2023-05-17 16:44:57 +03:00
|
|
|
| FixedBy of reference_typ Mark.pos
|
|
|
|
| VariesWith of unit * variation_typ Mark.pos option
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-01-04 20:19:15 +03:00
|
|
|
type scope_def = {
|
2022-11-21 12:12:45 +03:00
|
|
|
scope_def_rules : rule RuleName.Map.t;
|
2022-08-25 18:29:00 +03:00
|
|
|
scope_def_typ : typ;
|
2023-05-17 16:44:57 +03:00
|
|
|
scope_def_parameters : (Uid.MarkedString.info * typ) list Mark.pos option;
|
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
|
|
|
|
|
2024-05-24 15:26:44 +03:00
|
|
|
(* If fields are added, make sure to consider including them in the hash
|
|
|
|
computations below *)
|
2020-06-22 17:16:55 +03:00
|
|
|
type scope = {
|
2022-11-21 12:12:45 +03:00
|
|
|
scope_vars : var_or_states ScopeVar.Map.t;
|
2024-03-21 18:59:12 +03:00
|
|
|
scope_sub_scopes : ScopeName.t ScopeVar.Map.t;
|
2022-08-12 23:42:39 +03:00
|
|
|
scope_uid : ScopeName.t;
|
2023-04-18 11:31:44 +03:00
|
|
|
scope_defs : scope_def ScopeDef.Map.t;
|
2023-04-28 15:15:43 +03:00
|
|
|
scope_assertions : assertion AssertionName.Map.t;
|
2023-05-17 16:44:57 +03:00
|
|
|
scope_options : catala_option Mark.pos list;
|
2020-09-13 01:33:56 +03:00
|
|
|
scope_meta_assertions : meta_assertion list;
|
2024-05-24 15:26:44 +03:00
|
|
|
scope_visibility : visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
type topdef = {
|
|
|
|
topdef_expr : expr option;
|
|
|
|
topdef_type : typ;
|
|
|
|
topdef_visibility : visibility;
|
2020-06-22 17:16:55 +03:00
|
|
|
}
|
|
|
|
|
2023-11-20 18:01:06 +03:00
|
|
|
type modul = {
|
|
|
|
module_scopes : scope ScopeName.Map.t;
|
2024-05-24 15:26:44 +03:00
|
|
|
module_topdefs : topdef TopdefName.Map.t;
|
2023-11-20 18:01:06 +03:00
|
|
|
}
|
|
|
|
|
2022-11-21 12:12:45 +03:00
|
|
|
type program = {
|
2024-05-27 12:26:14 +03:00
|
|
|
program_module_name : (ModuleName.t * module_intf_id) option;
|
2022-11-21 12:12:45 +03:00
|
|
|
program_ctx : decl_ctx;
|
2023-11-20 18:01:06 +03:00
|
|
|
program_modules : modul ModuleName.Map.t;
|
|
|
|
program_root : modul;
|
2024-03-15 16:23:30 +03:00
|
|
|
program_lang : Global.backend_lang;
|
2022-11-21 12:12:45 +03:00
|
|
|
}
|
2020-11-25 13:53:56 +03:00
|
|
|
|
2024-05-24 15:26:44 +03:00
|
|
|
module Hash = struct
|
|
|
|
open Hash.Op
|
|
|
|
|
|
|
|
let var_or_state = function
|
|
|
|
| WholeVar -> !`WholeVar
|
|
|
|
| States s -> !`States % Hash.list StateName.hash s
|
|
|
|
|
|
|
|
let io x =
|
|
|
|
!(Mark.remove x.io_input : Runtime.io_input)
|
|
|
|
% !(Mark.remove x.io_output : bool)
|
|
|
|
|
|
|
|
let scope_decl ~strip d =
|
|
|
|
(* scope_def_rules is ignored (not part of the interface) *)
|
|
|
|
Type.hash ~strip d.scope_def_typ
|
|
|
|
% Hash.option
|
|
|
|
(fun (lst, _) ->
|
|
|
|
List.fold_left
|
|
|
|
(fun acc (name, ty) ->
|
|
|
|
acc % Uid.MarkedString.hash name % Type.hash ~strip ty)
|
|
|
|
!`SDparams lst)
|
|
|
|
d.scope_def_parameters
|
|
|
|
% !(d.scope_def_is_condition : bool)
|
|
|
|
% io d.scope_def_io
|
|
|
|
|
|
|
|
let scope ~strip s =
|
|
|
|
Hash.map ScopeVar.Map.fold ScopeVar.hash var_or_state s.scope_vars
|
|
|
|
% Hash.map ScopeVar.Map.fold ScopeVar.hash (ScopeName.hash ~strip)
|
|
|
|
s.scope_sub_scopes
|
|
|
|
% ScopeName.hash ~strip s.scope_uid
|
2024-05-24 18:24:14 +03:00
|
|
|
% Hash.map ScopeDef.Map.fold (ScopeDef.hash ~strip) (scope_decl ~strip)
|
2024-05-24 15:26:44 +03:00
|
|
|
s.scope_defs
|
|
|
|
(* assertions, options, etc. are not expected to be part of interfaces *)
|
|
|
|
|
2024-05-24 18:24:14 +03:00
|
|
|
let modul ?(strip = []) m =
|
2024-05-24 15:26:44 +03:00
|
|
|
Hash.map ScopeName.Map.fold (ScopeName.hash ~strip) (scope ~strip)
|
|
|
|
(ScopeName.Map.filter
|
|
|
|
(fun _ s -> s.scope_visibility = Public)
|
|
|
|
m.module_scopes)
|
|
|
|
% Hash.map TopdefName.Map.fold (TopdefName.hash ~strip)
|
|
|
|
(fun td -> Type.hash ~strip td.topdef_type)
|
|
|
|
(TopdefName.Map.filter
|
|
|
|
(fun _ td -> td.topdef_visibility = Public)
|
|
|
|
m.module_topdefs)
|
|
|
|
|
2024-05-24 18:24:14 +03:00
|
|
|
let module_binding modname m =
|
|
|
|
ModuleName.hash modname % modul ~strip:[modname] m
|
2024-05-24 15:26:44 +03:00
|
|
|
end
|
|
|
|
|
2022-10-10 16:15:36 +03:00
|
|
|
let rec locations_used e : LocationSet.t =
|
|
|
|
match e with
|
2024-04-04 11:56:56 +03:00
|
|
|
| ELocation l, m -> LocationSet.singleton (l, Expr.mark_pos m)
|
2022-10-10 16:15:36 +03:00
|
|
|
| e ->
|
|
|
|
Expr.shallow_fold
|
|
|
|
(fun e -> LocationSet.union (locations_used e))
|
|
|
|
e LocationSet.empty
|
2022-02-28 19:19:06 +03:00
|
|
|
|
2023-04-18 11:31:44 +03:00
|
|
|
let free_variables (def : rule RuleName.Map.t) : Pos.t ScopeDef.Map.t =
|
|
|
|
let add_locs (acc : Pos.t ScopeDef.Map.t) (locs : LocationSet.t) :
|
|
|
|
Pos.t ScopeDef.Map.t =
|
2022-02-28 19:19:06 +03:00
|
|
|
LocationSet.fold
|
2020-12-03 23:02:28 +03:00
|
|
|
(fun (loc, loc_pos) acc ->
|
2023-01-23 14:19:36 +03:00
|
|
|
let usage =
|
|
|
|
match loc with
|
2024-04-04 11:56:56 +03:00
|
|
|
| DesugaredScopeVar { name; state } -> Some (name, ScopeDef.Var state)
|
2023-02-13 17:00:23 +03:00
|
|
|
| ToplevelVar _ -> None
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
2023-04-18 11:31:44 +03:00
|
|
|
match usage with
|
|
|
|
| Some u -> ScopeDef.Map.add u loc_pos acc
|
|
|
|
| None -> acc)
|
2020-12-03 23:02:28 +03:00
|
|
|
locs acc
|
2020-11-25 13:53:56 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
RuleName.Map.fold
|
2020-11-25 16:35:26 +03:00
|
|
|
(fun _ rule acc ->
|
2020-11-27 18:27:10 +03:00
|
|
|
let locs =
|
2022-02-28 19:19:06 +03:00
|
|
|
LocationSet.union
|
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
|
|
|
(locations_used (Expr.unbox rule.rule_just))
|
|
|
|
(locations_used (Expr.unbox rule.rule_cons))
|
2020-11-27 18:27:10 +03:00
|
|
|
in
|
2020-11-25 13:53:56 +03:00
|
|
|
add_locs acc locs)
|
2023-04-18 11:31:44 +03:00
|
|
|
def ScopeDef.Map.empty
|
2023-03-30 19:52:29 +03:00
|
|
|
|
|
|
|
let fold_exprs ~(f : 'a -> expr -> 'a) ~(init : 'a) (p : program) : 'a =
|
|
|
|
let acc =
|
|
|
|
ScopeName.Map.fold
|
|
|
|
(fun _ scope acc ->
|
|
|
|
let acc =
|
2023-04-18 11:31:44 +03:00
|
|
|
ScopeDef.Map.fold
|
2023-03-30 19:52:29 +03:00
|
|
|
(fun _ scope_def acc ->
|
|
|
|
RuleName.Map.fold
|
|
|
|
(fun _ rule acc ->
|
|
|
|
f
|
|
|
|
(f acc (Expr.unbox rule.rule_just))
|
|
|
|
(Expr.unbox rule.rule_cons))
|
|
|
|
scope_def.scope_def_rules acc)
|
|
|
|
scope.scope_defs acc
|
|
|
|
in
|
|
|
|
let acc =
|
2023-04-28 15:15:43 +03:00
|
|
|
AssertionName.Map.fold
|
|
|
|
(fun _ assertion acc -> f acc (Expr.unbox assertion))
|
|
|
|
scope.scope_assertions acc
|
2023-03-30 19:52:29 +03:00
|
|
|
in
|
|
|
|
acc)
|
2023-11-20 18:01:06 +03:00
|
|
|
p.program_root.module_scopes init
|
2023-03-30 19:52:29 +03:00
|
|
|
in
|
2023-05-11 18:39:38 +03:00
|
|
|
TopdefName.Map.fold
|
2024-05-24 15:26:44 +03:00
|
|
|
(fun _ tdef acc -> Option.fold ~none:acc ~some:(f acc) tdef.topdef_expr)
|
2023-11-20 18:01:06 +03:00
|
|
|
p.program_root.module_topdefs acc
|