2020-11-23 11:22:47 +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> 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-11-07 15:50:28 +03:00
|
|
|
module SurfacePrint = Surface.Print
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2022-07-19 20:17:02 +03:00
|
|
|
module Runtime = Runtime_ocaml.Runtime
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Translation from {!module: Surface.Ast} to {!module: Desugaring.Ast}.
|
|
|
|
|
|
|
|
- Removes syntactic sugars
|
|
|
|
- Separate code from legislation *)
|
|
|
|
|
|
|
|
(** {1 Translating expressions} *)
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2022-11-22 22:57:59 +03:00
|
|
|
let translate_op_kind (k : Surface.Ast.op_kind) : desugared op_kind =
|
2020-12-10 13:35:56 +03:00
|
|
|
match k with
|
2022-11-22 22:57:59 +03:00
|
|
|
| Surface.Ast.KInt -> KInt
|
|
|
|
| Surface.Ast.KDec -> KRat
|
|
|
|
| Surface.Ast.KMoney -> KMoney
|
|
|
|
| Surface.Ast.KDate -> KDate
|
|
|
|
| Surface.Ast.KDuration -> KDuration
|
2020-12-09 16:51:22 +03:00
|
|
|
|
2022-11-22 22:57:59 +03:00
|
|
|
let translate_binop (op : Surface.Ast.binop) : desugared binop =
|
2020-11-24 17:48:57 +03:00
|
|
|
match op with
|
|
|
|
| And -> And
|
|
|
|
| Or -> Or
|
2021-03-16 22:34:03 +03:00
|
|
|
| Xor -> Xor
|
2020-12-09 16:51:22 +03:00
|
|
|
| Add l -> Add (translate_op_kind l)
|
|
|
|
| Sub l -> Sub (translate_op_kind l)
|
|
|
|
| Mult l -> Mult (translate_op_kind l)
|
|
|
|
| Div l -> Div (translate_op_kind l)
|
|
|
|
| Lt l -> Lt (translate_op_kind l)
|
|
|
|
| Lte l -> Lte (translate_op_kind l)
|
|
|
|
| Gt l -> Gt (translate_op_kind l)
|
|
|
|
| Gte l -> Gte (translate_op_kind l)
|
2020-11-24 17:48:57 +03:00
|
|
|
| Eq -> Eq
|
|
|
|
| Neq -> Neq
|
2021-07-08 17:27:46 +03:00
|
|
|
| Concat -> Concat
|
2020-11-24 17:48:57 +03:00
|
|
|
|
2022-11-22 22:57:59 +03:00
|
|
|
let translate_unop (op : Surface.Ast.unop) : desugared unop =
|
2020-12-09 16:51:22 +03:00
|
|
|
match op with Not -> Not | Minus l -> Minus (translate_op_kind l)
|
2020-11-24 17:48:57 +03:00
|
|
|
|
2021-01-26 19:41:20 +03:00
|
|
|
let disambiguate_constructor
|
|
|
|
(ctxt : Name_resolution.context)
|
2022-05-30 12:20:48 +03:00
|
|
|
(constructor : (string Marked.pos option * string Marked.pos) list)
|
2022-08-12 23:42:39 +03:00
|
|
|
(pos : Pos.t) : EnumName.t * EnumConstructor.t =
|
2021-01-26 19:41:20 +03:00
|
|
|
let enum, constructor =
|
2021-01-18 18:21:55 +03:00
|
|
|
match constructor with
|
|
|
|
| [c] -> c
|
|
|
|
| _ ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos
|
|
|
|
"The deep pattern matching syntactic sugar is not yet supported"
|
2021-01-18 18:21:55 +03:00
|
|
|
in
|
|
|
|
let possible_c_uids =
|
2022-11-22 22:57:59 +03:00
|
|
|
try IdentName.Map.find (Marked.unmark constructor) ctxt.constructor_idmap
|
2021-01-18 18:21:55 +03:00
|
|
|
with Not_found ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.get_mark constructor)
|
2021-01-18 18:21:55 +03:00
|
|
|
"The name of this constructor has not been defined before, maybe it is \
|
|
|
|
a typo?"
|
|
|
|
in
|
2021-01-26 19:41:20 +03:00
|
|
|
match enum with
|
|
|
|
| None ->
|
2022-11-21 12:12:45 +03:00
|
|
|
if EnumName.Map.cardinal possible_c_uids > 1 then
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.get_mark constructor)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This constructor name is ambiguous, it can belong to %a. Disambiguate \
|
|
|
|
it by prefixing it with the enum name."
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt " or ")
|
|
|
|
(fun fmt (s_name, _) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
Format.fprintf fmt "%a" EnumName.format_t s_name))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumName.Map.bindings possible_c_uids);
|
|
|
|
EnumName.Map.choose possible_c_uids
|
2021-01-26 19:41:20 +03:00
|
|
|
| Some enum -> (
|
2022-05-12 16:10:55 +03:00
|
|
|
try
|
2021-01-26 19:41:20 +03:00
|
|
|
(* The path is fully qualified *)
|
2022-10-21 16:47:17 +03:00
|
|
|
let e_uid = Name_resolution.get_enum ctxt enum in
|
2021-01-26 19:41:20 +03:00
|
|
|
try
|
2022-11-21 12:12:45 +03:00
|
|
|
let c_uid = EnumName.Map.find e_uid possible_c_uids in
|
2021-01-26 19:41:20 +03:00
|
|
|
e_uid, c_uid
|
|
|
|
with Not_found ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos "Enum %s does not contain case %s"
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.unmark enum)
|
|
|
|
(Marked.unmark constructor)
|
2021-01-26 19:41:20 +03:00
|
|
|
with Not_found ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark enum)
|
|
|
|
"Enum %s has not been defined before" (Marked.unmark enum))
|
2021-01-18 18:21:55 +03:00
|
|
|
|
2022-08-25 17:35:08 +03:00
|
|
|
(** Usage: [translate_expr scope ctxt naked_expr]
|
2020-12-14 17:23:04 +03:00
|
|
|
|
2022-10-21 16:47:17 +03:00
|
|
|
Translates [expr] into its desugared equivalent. [scope] is used to
|
2022-07-26 14:40:43 +03:00
|
|
|
disambiguate the scope and subscopes variables than occur in the expression *)
|
2022-02-28 20:34:32 +03:00
|
|
|
let rec translate_expr
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2022-11-07 15:50:28 +03:00
|
|
|
(inside_definition_of : Ast.ScopeDef.t Marked.pos option)
|
2022-02-28 20:34:32 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(expr : Surface.Ast.expression Marked.pos) : Ast.expr boxed =
|
2022-11-21 12:12:45 +03:00
|
|
|
let scope_ctxt = ScopeName.Map.find scope ctxt.scopes in
|
2022-02-28 20:34:32 +03:00
|
|
|
let rec_helper = translate_expr scope inside_definition_of ctxt in
|
2022-08-26 16:21:47 +03:00
|
|
|
let pos = Marked.get_mark expr in
|
|
|
|
let emark = Untyped { pos } in
|
|
|
|
match Marked.unmark expr with
|
2021-01-18 18:21:55 +03:00
|
|
|
| Binop
|
2022-11-07 15:50:28 +03:00
|
|
|
( (Surface.Ast.And, _pos_op),
|
2021-01-18 18:21:55 +03:00
|
|
|
( TestMatchCase (e1_sub, ((constructors, Some binding), pos_pattern)),
|
|
|
|
_pos_e1 ),
|
|
|
|
e2 ) ->
|
|
|
|
(* This sugar corresponds to [e is P x && e'] and should desugar to [match e
|
|
|
|
with P x -> e' | _ -> false] *)
|
|
|
|
let enum_uid, c_uid =
|
|
|
|
disambiguate_constructor ctxt constructors pos_pattern
|
|
|
|
in
|
|
|
|
let cases =
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumConstructor.Map.mapi
|
2021-01-18 18:21:55 +03:00
|
|
|
(fun c_uid' tau ->
|
2022-08-12 23:42:39 +03:00
|
|
|
if EnumConstructor.compare c_uid c_uid' <> 0 then
|
2022-08-25 17:08:08 +03:00
|
|
|
let nop_var = Var.make "_" 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.make_abs [| nop_var |]
|
|
|
|
(Expr.elit (LBool false) emark)
|
|
|
|
[tau] pos
|
2021-01-18 18:21:55 +03:00
|
|
|
else
|
|
|
|
let ctxt, binding_var =
|
2022-06-03 17:40:03 +03:00
|
|
|
Name_resolution.add_def_local_var ctxt (Marked.unmark binding)
|
2021-01-18 18:21:55 +03:00
|
|
|
in
|
2022-02-28 20:34:32 +03:00
|
|
|
let e2 = translate_expr scope inside_definition_of ctxt e2 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.make_abs [| binding_var |] e2 [tau] pos)
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumName.Map.find enum_uid ctxt.enums)
|
2021-01-18 18:21:55 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.ematch
|
2022-02-28 20:34:32 +03:00
|
|
|
(translate_expr scope inside_definition_of ctxt e1_sub)
|
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
|
|
|
enum_uid cases emark
|
2020-11-24 17:48:57 +03:00
|
|
|
| IfThenElse (e_if, e_then, e_else) ->
|
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.eifthenelse (rec_helper e_if) (rec_helper e_then) (rec_helper e_else)
|
|
|
|
emark
|
2022-08-26 16:21:47 +03:00
|
|
|
| Binop ((op, pos), e1, e2) ->
|
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 op_term = Expr.eop (Binop (translate_binop op)) (Untyped { pos }) in
|
|
|
|
Expr.eapp op_term [rec_helper e1; rec_helper e2] emark
|
2022-08-26 16:21:47 +03:00
|
|
|
| Unop ((op, pos), e) ->
|
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 op_term = Expr.eop (Unop (translate_unop op)) (Untyped { pos }) in
|
|
|
|
Expr.eapp op_term [rec_helper e] emark
|
2020-11-24 17:48:57 +03:00
|
|
|
| Literal l ->
|
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 lit =
|
2020-11-24 17:48:57 +03:00
|
|
|
match l with
|
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
|
|
|
| LNumber ((Int i, _), None) -> LInt (Runtime.integer_of_string i)
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Int i, _), Some (Percent, _)) ->
|
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
|
|
|
LRat Runtime.(decimal_of_string i /& decimal_of_string "100")
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Dec (i, f), _), None) ->
|
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
|
|
|
LRat Runtime.(decimal_of_string (i ^ "." ^ f))
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Dec (i, f), _), Some (Percent, _)) ->
|
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
|
|
|
LRat
|
|
|
|
Runtime.(decimal_of_string (i ^ "." ^ f) /& decimal_of_string "100")
|
|
|
|
| LBool b -> LBool b
|
2021-01-20 18:06:04 +03:00
|
|
|
| LMoneyAmount i ->
|
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
|
|
|
LMoney
|
|
|
|
Runtime.(
|
|
|
|
money_of_cents_integer
|
|
|
|
((integer_of_string i.money_amount_units *! integer_of_int 100)
|
|
|
|
+! integer_of_string i.money_amount_cents))
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Int i, _), Some (Year, _)) ->
|
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
|
|
|
LDuration (Runtime.duration_of_numbers (int_of_string i) 0 0)
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Int i, _), Some (Month, _)) ->
|
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
|
|
|
LDuration (Runtime.duration_of_numbers 0 (int_of_string i) 0)
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Int i, _), Some (Day, _)) ->
|
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
|
|
|
LDuration (Runtime.duration_of_numbers 0 0 (int_of_string i))
|
2021-01-20 18:06:04 +03:00
|
|
|
| LNumber ((Dec (_, _), _), Some ((Year | Month | Day), _)) ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos
|
|
|
|
"Impossible to specify decimal amounts of days, months or years"
|
2021-01-20 18:06:04 +03:00
|
|
|
| LDate date ->
|
2022-07-21 15:14:22 +03:00
|
|
|
if date.literal_date_month > 12 then
|
|
|
|
Errors.raise_spanned_error pos
|
2022-03-08 15:04:27 +03:00
|
|
|
"There is an error in this date: the month number is bigger than 12";
|
2022-07-21 15:14:22 +03:00
|
|
|
if date.literal_date_day > 31 then
|
|
|
|
Errors.raise_spanned_error pos
|
2022-03-08 15:04:27 +03:00
|
|
|
"There is an error in this date: the day number is bigger than 31";
|
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
|
|
|
LDate
|
|
|
|
(try
|
|
|
|
Runtime.date_of_numbers date.literal_date_year
|
|
|
|
date.literal_date_month date.literal_date_day
|
|
|
|
with Runtime.ImpossibleDate ->
|
|
|
|
Errors.raise_spanned_error pos
|
|
|
|
"There is an error in this date, it does not correspond to a \
|
|
|
|
correct calendar day")
|
2020-11-24 17:48:57 +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.elit lit emark
|
2020-11-24 17:48:57 +03:00
|
|
|
| Ident x -> (
|
|
|
|
(* first we check whether this is a local var, then we resort to scope-wide
|
2022-02-28 20:34:32 +03:00
|
|
|
variables *)
|
2022-11-22 22:57:59 +03:00
|
|
|
match IdentName.Map.find_opt x ctxt.local_var_idmap with
|
2022-02-28 20:34:32 +03:00
|
|
|
| None -> (
|
2022-11-22 22:57:59 +03:00
|
|
|
match IdentName.Map.find_opt x scope_ctxt.var_idmap with
|
2022-10-21 16:47:17 +03:00
|
|
|
| Some (ScopeVar uid) ->
|
2022-02-28 20:34:32 +03:00
|
|
|
(* If the referenced variable has states, then here are the rules to
|
|
|
|
desambiguate. In general, only the last state can be referenced.
|
|
|
|
Except if defining a state of the same variable, then it references
|
|
|
|
the previous state in the chain. *)
|
2022-11-21 12:12:45 +03:00
|
|
|
let x_sig = ScopeVar.Map.find uid ctxt.var_typs in
|
2022-02-28 20:34:32 +03:00
|
|
|
let x_state =
|
|
|
|
match x_sig.var_sig_states_list with
|
|
|
|
| [] -> None
|
|
|
|
| states -> (
|
|
|
|
match inside_definition_of with
|
2022-08-25 13:09:51 +03:00
|
|
|
| Some (Var (x'_uid, sx'), _) when ScopeVar.compare uid x'_uid = 0
|
|
|
|
-> (
|
2022-02-28 20:34:32 +03:00
|
|
|
match sx' with
|
|
|
|
| None ->
|
2022-05-12 16:10:55 +03:00
|
|
|
failwith
|
2022-02-28 20:34:32 +03:00
|
|
|
"inconsistent state: inside a definition of a variable with \
|
|
|
|
no state but variable has states"
|
|
|
|
| Some inside_def_state ->
|
2022-08-25 13:09:51 +03:00
|
|
|
if StateName.compare inside_def_state (List.hd states) = 0 then
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos
|
2022-03-06 19:13:40 +03:00
|
|
|
"It is impossible to refer to the variable you are \
|
|
|
|
defining when defining its first state."
|
2022-05-12 16:10:55 +03:00
|
|
|
else
|
2022-02-28 20:34:32 +03:00
|
|
|
(* Tricky: we have to retrieve in the list the previous state
|
|
|
|
with respect to the state that we are defining. *)
|
|
|
|
let correct_state = ref None in
|
2022-05-12 16:10:55 +03:00
|
|
|
ignore
|
2022-02-28 20:34:32 +03:00
|
|
|
(List.fold_left
|
|
|
|
(fun previous_state state ->
|
2022-08-25 13:09:51 +03:00
|
|
|
if StateName.equal inside_def_state state then
|
|
|
|
correct_state := previous_state;
|
2022-02-28 20:34:32 +03:00
|
|
|
Some state)
|
|
|
|
None states);
|
|
|
|
!correct_state)
|
2022-05-12 16:10:55 +03:00
|
|
|
| _ ->
|
2022-02-28 20:34:32 +03:00
|
|
|
(* we take the last state in the chain *)
|
|
|
|
Some (List.hd (List.rev states)))
|
2022-05-12 16:10:55 +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.elocation (DesugaredScopeVar ((uid, pos), x_state)) emark
|
2022-10-21 16:47:17 +03:00
|
|
|
| Some (SubScope _) | None ->
|
2020-12-17 22:09:33 +03:00
|
|
|
Name_resolution.raise_unknown_identifier
|
|
|
|
"for a local or scope-wide variable" (x, pos))
|
2020-12-06 19:48:15 +03:00
|
|
|
| Some uid ->
|
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.make_var uid emark
|
2022-02-28 20:34:32 +03:00
|
|
|
(* the whole box thing is to accomodate for this case *))
|
2021-01-26 07:07:12 +03:00
|
|
|
| Dotted (e, c, x) -> (
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark e with
|
2020-12-05 19:27:08 +03:00
|
|
|
| Ident y when Name_resolution.is_subscope_uid scope ctxt y ->
|
|
|
|
(* In this case, y.x is a subscope variable *)
|
2022-10-21 16:47:17 +03:00
|
|
|
let subscope_uid, subscope_real_uid =
|
2022-11-22 22:57:59 +03:00
|
|
|
match IdentName.Map.find y scope_ctxt.var_idmap with
|
2022-10-21 16:47:17 +03:00
|
|
|
| SubScope (sub, sc) -> sub, sc
|
|
|
|
| ScopeVar _ -> assert false
|
2020-11-24 17:48:57 +03:00
|
|
|
in
|
|
|
|
let subscope_var_uid =
|
|
|
|
Name_resolution.get_var_uid subscope_real_uid ctxt x
|
2022-05-12 16:10:55 +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.elocation
|
|
|
|
(SubScopeVar
|
|
|
|
(subscope_real_uid, (subscope_uid, pos), (subscope_var_uid, pos)))
|
|
|
|
emark
|
2022-11-22 22:57:59 +03:00
|
|
|
| _ ->
|
2020-12-05 19:27:08 +03:00
|
|
|
(* In this case e.x is the struct field x access of expression e *)
|
2022-02-28 20:34:32 +03:00
|
|
|
let e = translate_expr scope inside_definition_of ctxt e in
|
2022-11-22 22:57:59 +03:00
|
|
|
let str =
|
|
|
|
Option.map
|
|
|
|
(fun c ->
|
|
|
|
try Name_resolution.get_struct ctxt c
|
|
|
|
with Not_found ->
|
|
|
|
Errors.raise_spanned_error (Marked.get_mark c)
|
|
|
|
"Struct %s has not been defined before" (Marked.unmark c))
|
|
|
|
c
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-11-22 22:57:59 +03:00
|
|
|
Expr.edstructaccess e (Marked.unmark x) str emark)
|
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
|
|
|
| FunCall (f, arg) -> Expr.eapp (rec_helper f) [rec_helper arg] emark
|
2022-10-21 16:47:17 +03:00
|
|
|
| ScopeCall (sc_name, fields) ->
|
|
|
|
let called_scope = Name_resolution.get_scope ctxt sc_name in
|
2022-11-21 12:12:45 +03:00
|
|
|
let scope_def = ScopeName.Map.find called_scope ctxt.scopes in
|
2022-10-21 16:47:17 +03:00
|
|
|
let in_struct =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc (fld_id, e) ->
|
|
|
|
let var =
|
|
|
|
match
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.find_opt (Marked.unmark fld_id) scope_def.var_idmap
|
2022-10-21 16:47:17 +03:00
|
|
|
with
|
2022-10-25 12:24:35 +03:00
|
|
|
| Some (ScopeVar v) -> v
|
|
|
|
| Some (SubScope _) | None ->
|
|
|
|
Errors.raise_multispanned_error
|
|
|
|
[
|
|
|
|
None, Marked.get_mark fld_id;
|
|
|
|
( Some
|
2022-11-03 17:18:51 +03:00
|
|
|
(Format.asprintf "Scope %a declared here"
|
2022-10-25 12:24:35 +03:00
|
|
|
ScopeName.format_t called_scope),
|
|
|
|
Marked.get_mark (ScopeName.get_info called_scope) );
|
|
|
|
]
|
2022-11-03 17:18:51 +03:00
|
|
|
"Scope %a has no input variable %a" ScopeName.format_t
|
|
|
|
called_scope Print.lit_style (Marked.unmark fld_id)
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.update var
|
2022-10-25 12:24:35 +03:00
|
|
|
(function
|
|
|
|
| None -> Some (rec_helper e)
|
|
|
|
| Some _ ->
|
|
|
|
Errors.raise_spanned_error (Marked.get_mark fld_id)
|
|
|
|
"Duplicate definition of scope input variable '%a'"
|
|
|
|
ScopeVar.format_t var)
|
|
|
|
acc)
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.empty fields
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
|
|
|
Expr.escopecall called_scope in_struct emark
|
2022-07-26 14:40:43 +03:00
|
|
|
| LetIn (x, e1, e2) ->
|
|
|
|
let ctxt, v = Name_resolution.add_def_local_var ctxt (Marked.unmark x) in
|
2022-08-25 13:09:51 +03:00
|
|
|
let tau = TAny, Marked.get_mark x in
|
2022-07-26 14:40:43 +03:00
|
|
|
let fn =
|
2022-08-25 17:08:08 +03:00
|
|
|
Expr.make_abs [| v |]
|
2022-07-26 14:40:43 +03:00
|
|
|
(translate_expr scope inside_definition_of ctxt e2)
|
2022-09-12 18:03:44 +03:00
|
|
|
[tau] pos
|
2022-07-26 14:40:43 +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.eapp fn [rec_helper e1] emark
|
2020-12-05 19:27:08 +03:00
|
|
|
| StructLit (s_name, fields) ->
|
|
|
|
let s_uid =
|
2022-11-22 22:57:59 +03:00
|
|
|
match IdentName.Map.find_opt (Marked.unmark s_name) ctxt.typedefs with
|
2022-10-21 16:47:17 +03:00
|
|
|
| Some (Name_resolution.TStruct s_uid) -> s_uid
|
|
|
|
| _ ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark s_name)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This identifier should refer to a struct name"
|
2020-12-05 19:27:08 +03:00
|
|
|
in
|
2022-03-08 15:04:27 +03:00
|
|
|
|
2020-12-05 19:27:08 +03:00
|
|
|
let s_fields =
|
|
|
|
List.fold_left
|
|
|
|
(fun s_fields (f_name, f_e) ->
|
|
|
|
let f_uid =
|
|
|
|
try
|
2022-11-21 12:12:45 +03:00
|
|
|
StructName.Map.find s_uid
|
2022-11-22 22:57:59 +03:00
|
|
|
(IdentName.Map.find (Marked.unmark f_name) ctxt.field_idmap)
|
2020-12-05 19:27:08 +03:00
|
|
|
with Not_found ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark f_name)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This identifier should refer to a field of struct %s"
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.unmark s_name)
|
2020-12-05 19:27:08 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
(match StructField.Map.find_opt f_uid s_fields with
|
2020-12-09 12:36:09 +03:00
|
|
|
| None -> ()
|
|
|
|
| Some e_field ->
|
|
|
|
Errors.raise_multispanned_error
|
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
|
|
|
[None, Marked.get_mark f_e; None, Expr.pos e_field]
|
2022-11-21 12:12:45 +03:00
|
|
|
"The field %a has been defined twice:" StructField.format_t f_uid);
|
2022-02-28 20:34:32 +03:00
|
|
|
let f_e = translate_expr scope inside_definition_of ctxt f_e in
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.Map.add f_uid f_e s_fields)
|
|
|
|
StructField.Map.empty fields
|
2020-12-05 19:27:08 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
let expected_s_fields = StructName.Map.find s_uid ctxt.structs in
|
|
|
|
StructField.Map.iter
|
2021-04-29 18:46:56 +03:00
|
|
|
(fun expected_f _ ->
|
2022-11-21 12:12:45 +03:00
|
|
|
if not (StructField.Map.mem expected_f s_fields) then
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos
|
|
|
|
"Missing field for structure %a: \"%a\"" StructName.format_t s_uid
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.format_t expected_f)
|
2021-04-29 18:46:56 +03:00
|
|
|
expected_s_fields;
|
|
|
|
|
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.estruct s_uid s_fields emark
|
2022-08-26 16:21:47 +03:00
|
|
|
| EnumInject (enum, (constructor, pos_constructor), payload) -> (
|
2020-12-06 19:48:15 +03:00
|
|
|
let possible_c_uids =
|
2022-11-22 22:57:59 +03:00
|
|
|
try IdentName.Map.find constructor ctxt.constructor_idmap
|
2020-12-06 19:48:15 +03:00
|
|
|
with Not_found ->
|
2022-08-26 16:21:47 +03:00
|
|
|
Errors.raise_spanned_error pos_constructor
|
2021-01-18 18:21:55 +03:00
|
|
|
"The name of this constructor has not been defined before, maybe it \
|
2020-12-06 19:48:15 +03:00
|
|
|
is a typo?"
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-08-26 16:21:47 +03:00
|
|
|
let mark_constructor = Untyped { pos = pos_constructor } in
|
2022-05-12 16:10:55 +03:00
|
|
|
|
2021-01-26 18:38:10 +03:00
|
|
|
match enum with
|
|
|
|
| None ->
|
2022-05-12 16:10:55 +03:00
|
|
|
if
|
2021-01-26 18:38:10 +03:00
|
|
|
(* No constructor name was specified *)
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumName.Map.cardinal possible_c_uids > 1
|
2022-05-12 16:10:55 +03:00
|
|
|
then
|
2022-08-26 16:21:47 +03:00
|
|
|
Errors.raise_spanned_error pos_constructor
|
2022-03-08 15:04:27 +03:00
|
|
|
"This constructor name is ambiguous, it can belong to %a. \
|
|
|
|
Desambiguate it by prefixing it with the enum name."
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt " or ")
|
|
|
|
(fun fmt (s_name, _) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
Format.fprintf fmt "%a" EnumName.format_t s_name))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumName.Map.bindings possible_c_uids)
|
2022-05-12 16:10:55 +03:00
|
|
|
else
|
2022-11-21 12:12:45 +03:00
|
|
|
let e_uid, c_uid = EnumName.Map.choose possible_c_uids in
|
2022-02-28 20:34:32 +03:00
|
|
|
let payload =
|
|
|
|
Option.map (translate_expr scope inside_definition_of ctxt) payload
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.einj
|
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
|
|
|
(match payload with
|
|
|
|
| Some e' -> e'
|
|
|
|
| None -> Expr.elit LUnit mark_constructor)
|
|
|
|
c_uid e_uid emark
|
2021-01-26 18:38:10 +03:00
|
|
|
| Some enum -> (
|
2022-05-12 16:10:55 +03:00
|
|
|
try
|
2021-01-26 18:38:10 +03:00
|
|
|
(* The path has been fully qualified *)
|
2022-10-21 16:47:17 +03:00
|
|
|
let e_uid = Name_resolution.get_enum ctxt enum in
|
2020-12-06 19:48:15 +03:00
|
|
|
try
|
2022-11-21 12:12:45 +03:00
|
|
|
let c_uid = EnumName.Map.find e_uid possible_c_uids in
|
2022-02-28 20:34:32 +03:00
|
|
|
let payload =
|
2020-12-06 19:48:15 +03:00
|
|
|
Option.map (translate_expr scope inside_definition_of ctxt) payload
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.einj
|
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
|
|
|
(match payload with
|
|
|
|
| Some e' -> e'
|
|
|
|
| None -> Expr.elit LUnit mark_constructor)
|
|
|
|
c_uid e_uid emark
|
2020-12-06 19:48:15 +03:00
|
|
|
with Not_found ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos "Enum %s does not contain case %s"
|
2022-08-26 16:21:47 +03:00
|
|
|
(Marked.unmark enum) constructor
|
2021-03-16 22:34:03 +03:00
|
|
|
with Not_found ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark enum)
|
|
|
|
"Enum %s has not been defined before" (Marked.unmark enum)))
|
2020-12-06 19:48:15 +03:00
|
|
|
| MatchWith (e1, (cases, _cases_pos)) ->
|
2022-02-28 20:34:32 +03:00
|
|
|
let e1 = translate_expr scope inside_definition_of ctxt e1 in
|
|
|
|
let cases_d, e_uid =
|
|
|
|
disambiguate_match_and_build_expression scope inside_definition_of ctxt
|
|
|
|
cases
|
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.ematch e1 e_uid cases_d emark
|
2021-01-18 18:21:55 +03:00
|
|
|
| TestMatchCase (e1, pattern) ->
|
2022-05-30 12:20:48 +03:00
|
|
|
(match snd (Marked.unmark pattern) with
|
2021-01-18 18:21:55 +03:00
|
|
|
| None -> ()
|
|
|
|
| Some binding ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.format_spanned_warning (Marked.get_mark binding)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This binding will be ignored (remove it to suppress warning)");
|
2021-01-18 18:21:55 +03:00
|
|
|
let enum_uid, c_uid =
|
|
|
|
disambiguate_constructor ctxt
|
2022-05-30 12:20:48 +03:00
|
|
|
(fst (Marked.unmark pattern))
|
|
|
|
(Marked.get_mark pattern)
|
2021-01-06 19:04:46 +03:00
|
|
|
in
|
|
|
|
let cases =
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumConstructor.Map.mapi
|
2021-01-06 19:04:46 +03:00
|
|
|
(fun c_uid' tau ->
|
2022-08-25 17:08:08 +03:00
|
|
|
let nop_var = Var.make "_" 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.make_abs [| nop_var |]
|
|
|
|
(Expr.elit (LBool (EnumConstructor.compare c_uid c_uid' = 0)) emark)
|
|
|
|
[tau] pos)
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumName.Map.find enum_uid ctxt.enums)
|
2021-01-06 19:04:46 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.ematch
|
2022-02-28 20:34:32 +03:00
|
|
|
(translate_expr scope inside_definition_of ctxt e1)
|
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
|
|
|
enum_uid cases emark
|
|
|
|
| ArrayLit es -> Expr.earray (List.map rec_helper es) emark
|
2021-01-10 20:11:46 +03:00
|
|
|
| CollectionOp
|
2022-11-07 15:50:28 +03:00
|
|
|
( (((Surface.Ast.Filter | Surface.Ast.Map) as op'), _pos_op'),
|
2021-01-10 20:11:46 +03:00
|
|
|
param',
|
|
|
|
collection,
|
|
|
|
predicate ) ->
|
|
|
|
let collection = rec_helper collection in
|
2022-06-03 17:40:03 +03:00
|
|
|
let ctxt, param =
|
|
|
|
Name_resolution.add_def_local_var ctxt (Marked.unmark param')
|
|
|
|
in
|
2021-01-10 20:11:46 +03:00
|
|
|
let f_pred =
|
2022-08-25 17:08:08 +03:00
|
|
|
Expr.make_abs [| param |]
|
2022-02-28 20:34:32 +03:00
|
|
|
(translate_expr scope inside_definition_of ctxt predicate)
|
2022-08-25 13:09:51 +03:00
|
|
|
[TAny, pos]
|
2022-09-12 18:03:44 +03:00
|
|
|
pos
|
2021-01-10 20:11:46 +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.eapp
|
|
|
|
(Expr.eop
|
|
|
|
(match op' with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Map -> Binop Map
|
|
|
|
| Surface.Ast.Filter -> Binop Filter
|
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
|
|
|
| _ -> assert false (* should not happen *))
|
|
|
|
emark)
|
|
|
|
[f_pred; collection] emark
|
2021-01-10 19:07:41 +03:00
|
|
|
| CollectionOp
|
2022-11-07 15:50:28 +03:00
|
|
|
( ( Surface.Ast.Aggregate
|
|
|
|
(Surface.Ast.AggregateArgExtremum (max_or_min, pred_typ, init)),
|
2021-01-10 19:07:41 +03:00
|
|
|
pos_op' ),
|
|
|
|
param',
|
|
|
|
collection,
|
|
|
|
predicate ) ->
|
|
|
|
let init = rec_helper init in
|
|
|
|
let collection = rec_helper collection in
|
2022-06-03 17:40:03 +03:00
|
|
|
let ctxt, param =
|
|
|
|
Name_resolution.add_def_local_var ctxt (Marked.unmark param')
|
|
|
|
in
|
2021-01-10 19:07:41 +03:00
|
|
|
let op_kind =
|
|
|
|
match pred_typ with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Integer -> KInt
|
|
|
|
| Surface.Ast.Decimal -> KRat
|
|
|
|
| Surface.Ast.Money -> KMoney
|
|
|
|
| Surface.Ast.Duration -> KDuration
|
|
|
|
| Surface.Ast.Date -> KDate
|
2021-01-10 19:07:41 +03:00
|
|
|
| _ ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos
|
|
|
|
"It is impossible to compute the arg-%s of two values of type %a"
|
|
|
|
(if max_or_min then "max" else "min")
|
2022-08-17 17:14:14 +03:00
|
|
|
SurfacePrint.format_primitive_typ pred_typ
|
2021-01-10 19:07:41 +03:00
|
|
|
in
|
2022-08-12 23:42:39 +03:00
|
|
|
let cmp_op = if max_or_min then Gt op_kind else Lt op_kind in
|
2021-01-10 19:07:41 +03:00
|
|
|
let f_pred =
|
2022-08-25 17:08:08 +03:00
|
|
|
Expr.make_abs [| param |]
|
2022-02-28 20:34:32 +03:00
|
|
|
(translate_expr scope inside_definition_of ctxt predicate)
|
2022-08-25 13:09:51 +03:00
|
|
|
[TAny, pos]
|
2022-09-12 18:03:44 +03:00
|
|
|
pos
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-08-25 17:08:08 +03:00
|
|
|
let f_pred_var = Var.make "predicate" in
|
2022-08-26 16:21:47 +03:00
|
|
|
let f_pred_var_e =
|
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.make_var f_pred_var (Untyped { pos = Marked.get_mark predicate })
|
2022-08-26 16:21:47 +03:00
|
|
|
in
|
2022-08-25 17:08:08 +03:00
|
|
|
let acc_var = Var.make "acc" 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 acc_var_e = Expr.make_var acc_var emark in
|
2022-08-25 17:08:08 +03:00
|
|
|
let item_var = Var.make "item" 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 item_var_e = Expr.make_var item_var (Marked.get_mark collection) in
|
2020-12-30 00:26:10 +03:00
|
|
|
let fold_body =
|
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.eifthenelse
|
|
|
|
(Expr.eapp
|
|
|
|
(Expr.eop (Binop cmp_op) (Untyped { pos = pos_op' }))
|
|
|
|
[
|
|
|
|
Expr.eapp f_pred_var_e [acc_var_e] emark;
|
|
|
|
Expr.eapp f_pred_var_e [item_var_e] emark;
|
|
|
|
]
|
|
|
|
emark)
|
|
|
|
acc_var_e item_var_e emark
|
2022-02-28 20:34:32 +03:00
|
|
|
in
|
|
|
|
let fold_f =
|
2022-09-12 18:03:44 +03:00
|
|
|
Expr.make_abs [| acc_var; item_var |] fold_body [TAny, pos; TAny, pos] pos
|
2021-01-10 19:07:41 +03:00
|
|
|
in
|
|
|
|
let fold =
|
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 (Expr.eop (Ternop Fold) emark) [fold_f; init; collection] emark
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-08-26 16:21:47 +03:00
|
|
|
Expr.make_let_in f_pred_var (TAny, pos) f_pred fold pos
|
2022-02-28 20:34:32 +03:00
|
|
|
| CollectionOp (op', param', collection, predicate) ->
|
2022-06-03 17:40:03 +03:00
|
|
|
let ctxt, param =
|
|
|
|
Name_resolution.add_def_local_var ctxt (Marked.unmark param')
|
|
|
|
in
|
2020-12-30 00:26:10 +03:00
|
|
|
let collection = rec_helper collection in
|
2022-08-26 16:21:47 +03:00
|
|
|
let mark = Untyped { pos = Marked.get_mark op' } in
|
2020-12-30 00:26:10 +03:00
|
|
|
let init =
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark op' with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Map | Surface.Ast.Filter
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateArgExtremum _) ->
|
2022-02-28 20:34:32 +03:00
|
|
|
assert false (* should not happen *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Exists -> Expr.elit (LBool false) mark
|
|
|
|
| Surface.Ast.Forall -> Expr.elit (LBool true) mark
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Integer) ->
|
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.elit (LInt (Runtime.integer_of_int 0)) mark
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Decimal) ->
|
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.elit (LRat (Runtime.decimal_of_string "0")) mark
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Money) ->
|
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.elit
|
|
|
|
(LMoney (Runtime.money_of_cents_integer (Runtime.integer_of_int 0)))
|
|
|
|
mark
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Duration) ->
|
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.elit (LDuration (Runtime.duration_of_numbers 0 0 0)) mark
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum t) ->
|
2021-01-10 19:07:41 +03:00
|
|
|
Errors.raise_spanned_error pos
|
|
|
|
"It is impossible to sum two values of type %a together"
|
2022-08-17 17:14:14 +03:00
|
|
|
SurfacePrint.format_primitive_typ t
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateExtremum (_, _, init)) ->
|
|
|
|
rec_helper init
|
|
|
|
| Surface.Ast.Aggregate Surface.Ast.AggregateCount ->
|
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.elit (LInt (Runtime.integer_of_int 0)) mark
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-08-25 17:08:08 +03:00
|
|
|
let acc_var = Var.make "acc" in
|
2022-08-26 16:21:47 +03:00
|
|
|
let acc =
|
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.make_var acc_var (Untyped { pos = Marked.get_mark param' })
|
2022-08-26 16:21:47 +03:00
|
|
|
in
|
2022-02-28 20:34:32 +03:00
|
|
|
let f_body =
|
2022-11-22 22:57:59 +03:00
|
|
|
let make_body (op : desugared binop) =
|
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 (Expr.eop (Binop op) mark)
|
|
|
|
[acc; translate_expr scope inside_definition_of ctxt predicate]
|
|
|
|
emark
|
2021-01-10 19:07:41 +03:00
|
|
|
in
|
2022-11-22 22:57:59 +03:00
|
|
|
let make_extr_body (cmp_op : desugared binop) (t : typ) =
|
2022-08-25 17:08:08 +03:00
|
|
|
let tmp_var = Var.make "tmp" in
|
2022-08-26 16:21:47 +03:00
|
|
|
let tmp =
|
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.make_var tmp_var (Untyped { pos = Marked.get_mark param' })
|
2022-08-26 16:21:47 +03:00
|
|
|
in
|
|
|
|
Expr.make_let_in tmp_var t
|
2020-12-30 01:43:00 +03:00
|
|
|
(translate_expr scope inside_definition_of ctxt predicate)
|
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.eifthenelse
|
|
|
|
(Expr.eapp (Expr.eop (Binop cmp_op) mark) [acc; tmp] emark)
|
|
|
|
acc tmp emark)
|
2022-08-25 17:08:08 +03:00
|
|
|
pos
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark op' with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Map | Surface.Ast.Filter
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateArgExtremum _) ->
|
2021-01-10 19:07:41 +03:00
|
|
|
assert false (* should not happen *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Exists -> make_body Or
|
|
|
|
| Surface.Ast.Forall -> make_body And
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Integer) ->
|
|
|
|
make_body (Add KInt)
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Decimal) ->
|
|
|
|
make_body (Add KRat)
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Money) ->
|
|
|
|
make_body (Add KMoney)
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Duration) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
make_body (Add KDuration)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum _) ->
|
2021-01-10 19:07:41 +03:00
|
|
|
assert false (* should not happen *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateExtremum (max_or_min, t, _))
|
|
|
|
->
|
2021-01-06 17:19:03 +03:00
|
|
|
let op_kind, typ =
|
2021-01-06 14:41:24 +03:00
|
|
|
match t with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Integer -> KInt, (TLit TInt, pos)
|
|
|
|
| Surface.Ast.Decimal -> KRat, (TLit TRat, pos)
|
|
|
|
| Surface.Ast.Money -> KMoney, (TLit TMoney, pos)
|
|
|
|
| Surface.Ast.Duration -> KDuration, (TLit TDuration, pos)
|
|
|
|
| Surface.Ast.Date -> KDate, (TLit TDate, pos)
|
2022-05-12 16:10:55 +03:00
|
|
|
| _ ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_spanned_error pos
|
2022-09-12 18:03:44 +03:00
|
|
|
"It is impossible to compute the %s of two values of type %a"
|
2022-03-08 15:04:27 +03:00
|
|
|
(if max_or_min then "max" else "min")
|
2022-08-17 17:14:14 +03:00
|
|
|
SurfacePrint.format_primitive_typ t
|
2020-12-30 01:43:00 +03:00
|
|
|
in
|
2022-08-12 23:42:39 +03:00
|
|
|
let cmp_op = if max_or_min then Gt op_kind else Lt op_kind in
|
2020-12-30 01:43:00 +03:00
|
|
|
make_extr_body cmp_op typ
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate Surface.Ast.AggregateCount ->
|
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 predicate =
|
|
|
|
translate_expr scope inside_definition_of ctxt predicate
|
|
|
|
in
|
|
|
|
Expr.eifthenelse predicate
|
|
|
|
(Expr.eapp
|
|
|
|
(Expr.eop (Binop (Add KInt)) mark)
|
|
|
|
[
|
|
|
|
acc;
|
|
|
|
Expr.elit
|
|
|
|
(LInt (Runtime.integer_of_int 1))
|
|
|
|
(Marked.get_mark predicate);
|
|
|
|
]
|
|
|
|
emark)
|
|
|
|
acc emark
|
2020-12-30 13:50:19 +03:00
|
|
|
in
|
|
|
|
let f =
|
2022-08-12 23:42:39 +03:00
|
|
|
let make_f (t : typ_lit) =
|
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.eabs
|
|
|
|
(Expr.bind [| acc_var; param |] f_body)
|
|
|
|
[
|
|
|
|
TLit t, Marked.get_mark op';
|
|
|
|
TAny, pos
|
|
|
|
(* we put any here because the type of the elements of the arrays is
|
|
|
|
not always the type of the accumulator; for instance in
|
|
|
|
AggregateCount. *);
|
|
|
|
]
|
|
|
|
emark
|
2020-12-30 13:50:19 +03:00
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark op' with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Map | Surface.Ast.Filter
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateArgExtremum _) ->
|
2021-01-10 19:07:41 +03:00
|
|
|
assert false (* should not happen *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Exists -> make_f TBool
|
|
|
|
| Surface.Ast.Forall -> make_f TBool
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Integer)
|
|
|
|
| Surface.Ast.Aggregate
|
|
|
|
(Surface.Ast.AggregateExtremum (_, Surface.Ast.Integer, _)) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
make_f TInt
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Decimal)
|
|
|
|
| Surface.Ast.Aggregate
|
|
|
|
(Surface.Ast.AggregateExtremum (_, Surface.Ast.Decimal, _)) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
make_f TRat
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Money)
|
|
|
|
| Surface.Ast.Aggregate
|
|
|
|
(Surface.Ast.AggregateExtremum (_, Surface.Ast.Money, _)) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
make_f TMoney
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum Surface.Ast.Duration)
|
|
|
|
| Surface.Ast.Aggregate
|
|
|
|
(Surface.Ast.AggregateExtremum (_, Surface.Ast.Duration, _)) ->
|
2022-08-12 23:42:39 +03:00
|
|
|
make_f TDuration
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateSum _)
|
|
|
|
| Surface.Ast.Aggregate (Surface.Ast.AggregateExtremum _) ->
|
2021-01-10 19:07:41 +03:00
|
|
|
assert false (* should not happen *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Aggregate Surface.Ast.AggregateCount -> make_f TInt
|
2022-05-12 16:10:55 +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.eapp (Expr.eop (Ternop Fold) emark) [f; init; collection] emark
|
2020-12-30 13:50:19 +03:00
|
|
|
| MemCollection (member, collection) ->
|
2022-08-25 17:08:08 +03:00
|
|
|
let param_var = Var.make "collection_member" 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 param = Expr.make_var param_var emark in
|
2020-12-30 13:50:19 +03:00
|
|
|
let collection = rec_helper collection 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 init = Expr.elit (LBool false) emark in
|
2022-08-25 17:08:08 +03:00
|
|
|
let acc_var = Var.make "acc" 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 acc = Expr.make_var acc_var emark in
|
2020-12-30 13:50:19 +03:00
|
|
|
let f_body =
|
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 member = translate_expr scope inside_definition_of ctxt member in
|
|
|
|
Expr.eapp
|
|
|
|
(Expr.eop (Binop Or) emark)
|
|
|
|
[Expr.eapp (Expr.eop (Binop Eq) emark) [member; param] emark; acc]
|
|
|
|
emark
|
2022-02-28 20:34:32 +03:00
|
|
|
in
|
|
|
|
let f =
|
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.eabs
|
|
|
|
(Expr.bind [| acc_var; param_var |] f_body)
|
|
|
|
[TLit TBool, pos; TAny, pos]
|
|
|
|
emark
|
2022-05-12 16:10:55 +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.eapp (Expr.eop (Ternop Fold) emark) [f; init; collection] emark
|
|
|
|
| Builtin IntToDec -> Expr.eop (Unop IntToRat) emark
|
|
|
|
| Builtin MoneyToDec -> Expr.eop (Unop MoneyToRat) emark
|
|
|
|
| Builtin DecToMoney -> Expr.eop (Unop RatToMoney) emark
|
|
|
|
| Builtin Cardinal -> Expr.eop (Unop Length) emark
|
|
|
|
| Builtin GetDay -> Expr.eop (Unop GetDay) emark
|
|
|
|
| Builtin GetMonth -> Expr.eop (Unop GetMonth) emark
|
|
|
|
| Builtin GetYear -> Expr.eop (Unop GetYear) emark
|
|
|
|
| Builtin FirstDayOfMonth -> Expr.eop (Unop FirstDayOfMonth) emark
|
|
|
|
| Builtin LastDayOfMonth -> Expr.eop (Unop LastDayOfMonth) emark
|
|
|
|
| Builtin RoundMoney -> Expr.eop (Unop RoundMoney) emark
|
|
|
|
| Builtin RoundDecimal -> Expr.eop (Unop RoundDecimal) emark
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2021-01-18 18:21:55 +03:00
|
|
|
and disambiguate_match_and_build_expression
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2022-11-07 15:50:28 +03:00
|
|
|
(inside_definition_of : Ast.ScopeDef.t Marked.pos option)
|
2021-01-18 18:21:55 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(cases : Surface.Ast.match_case Marked.pos list) :
|
2022-11-21 12:12:45 +03:00
|
|
|
Ast.expr boxed EnumConstructor.Map.t * EnumName.t =
|
2021-06-03 13:09:54 +03:00
|
|
|
let create_var = function
|
2022-08-25 17:08:08 +03:00
|
|
|
| None -> ctxt, Var.make "_"
|
2021-06-03 13:09:54 +03:00
|
|
|
| Some param ->
|
|
|
|
let ctxt, param_var = Name_resolution.add_def_local_var ctxt param in
|
2022-06-03 17:40:03 +03:00
|
|
|
ctxt, param_var
|
2021-06-03 13:09:54 +03:00
|
|
|
in
|
2021-06-03 17:38:07 +03:00
|
|
|
let bind_case_body
|
2022-08-12 23:42:39 +03:00
|
|
|
(c_uid : EnumConstructor.t)
|
|
|
|
(e_uid : EnumName.t)
|
2021-06-03 17:38:07 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
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
|
|
|
case_body
|
|
|
|
e_binder =
|
|
|
|
Expr.eabs e_binder
|
|
|
|
[
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumConstructor.Map.find c_uid
|
|
|
|
(EnumName.Map.find e_uid ctxt.Name_resolution.enums);
|
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
|
|
|
]
|
|
|
|
(Marked.get_mark case_body)
|
2021-06-03 13:09:54 +03:00
|
|
|
in
|
2021-06-03 12:24:44 +03:00
|
|
|
let bind_match_cases (cases_d, e_uid, curr_index) (case, case_pos) =
|
2021-06-03 11:24:01 +03:00
|
|
|
match case with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.MatchCase case ->
|
|
|
|
let constructor, binding =
|
|
|
|
Marked.unmark case.Surface.Ast.match_case_pattern
|
|
|
|
in
|
2021-06-03 11:24:01 +03:00
|
|
|
let e_uid', c_uid =
|
|
|
|
disambiguate_constructor ctxt constructor
|
2022-11-07 15:50:28 +03:00
|
|
|
(Marked.get_mark case.Surface.Ast.match_case_pattern)
|
2021-06-03 11:24:01 +03:00
|
|
|
in
|
|
|
|
let e_uid =
|
|
|
|
match e_uid with
|
|
|
|
| None -> e_uid'
|
|
|
|
| Some e_uid ->
|
|
|
|
if e_uid = e_uid' then e_uid
|
|
|
|
else
|
|
|
|
Errors.raise_spanned_error
|
2022-11-07 15:50:28 +03:00
|
|
|
(Marked.get_mark case.Surface.Ast.match_case_pattern)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This case matches a constructor of enumeration %a but previous \
|
|
|
|
case were matching constructors of enumeration %a"
|
2022-08-12 23:42:39 +03:00
|
|
|
EnumName.format_t e_uid EnumName.format_t e_uid'
|
2022-03-08 15:04:27 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
(match EnumConstructor.Map.find_opt c_uid cases_d with
|
2021-06-03 11:24:01 +03:00
|
|
|
| None -> ()
|
|
|
|
| Some e_case ->
|
2022-03-08 15:04:27 +03:00
|
|
|
Errors.raise_multispanned_error
|
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
|
|
|
[None, Marked.get_mark case.match_case_expr; None, Expr.pos e_case]
|
2022-03-08 15:04:27 +03:00
|
|
|
"The constructor %a has been matched twice:" EnumConstructor.format_t
|
2022-08-12 23:42:39 +03:00
|
|
|
c_uid);
|
2022-06-03 17:40:03 +03:00
|
|
|
let ctxt, param_var = create_var (Option.map Marked.unmark binding) in
|
2022-02-28 20:34:32 +03:00
|
|
|
let case_body =
|
2022-11-07 15:50:28 +03:00
|
|
|
translate_expr scope inside_definition_of ctxt
|
|
|
|
case.Surface.Ast.match_case_expr
|
2022-05-12 16:10:55 +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
|
|
|
let e_binder = Expr.bind [| param_var |] case_body in
|
2022-06-03 17:40:03 +03:00
|
|
|
let case_expr = bind_case_body c_uid e_uid ctxt case_body e_binder in
|
2022-11-21 12:12:45 +03:00
|
|
|
( EnumConstructor.Map.add c_uid case_expr cases_d,
|
|
|
|
Some e_uid,
|
|
|
|
curr_index + 1 )
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.WildCard match_case_expr -> (
|
2021-06-03 12:24:44 +03:00
|
|
|
let nb_cases = List.length cases in
|
2021-06-03 12:37:06 +03:00
|
|
|
let raise_wildcard_not_last_case_err () =
|
2021-06-03 11:24:01 +03:00
|
|
|
Errors.raise_multispanned_error
|
2022-05-12 16:10:55 +03:00
|
|
|
[
|
2021-06-03 12:24:44 +03:00
|
|
|
Some "Not ending wildcard:", case_pos;
|
2022-03-08 15:04:27 +03:00
|
|
|
( Some "Next reachable case:",
|
2022-05-30 12:20:48 +03:00
|
|
|
curr_index + 1 |> List.nth cases |> Marked.get_mark );
|
2022-05-12 16:10:55 +03:00
|
|
|
]
|
2022-03-08 15:04:27 +03:00
|
|
|
"Wildcard must be the last match case"
|
2022-05-12 16:10:55 +03:00
|
|
|
in
|
2021-06-03 11:24:01 +03:00
|
|
|
match e_uid with
|
|
|
|
| None ->
|
2022-03-08 15:04:27 +03:00
|
|
|
if 1 = nb_cases then
|
|
|
|
Errors.raise_spanned_error case_pos
|
|
|
|
"Couldn't infer the enumeration name from lonely wildcard \
|
|
|
|
(wildcard cannot be used as single match case)"
|
|
|
|
else raise_wildcard_not_last_case_err ()
|
2021-11-07 01:16:58 +03:00
|
|
|
| Some e_uid ->
|
2021-06-03 12:37:06 +03:00
|
|
|
if curr_index < nb_cases - 1 then raise_wildcard_not_last_case_err ();
|
2022-03-08 15:04:27 +03:00
|
|
|
let missing_constructors =
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumName.Map.find e_uid ctxt.Name_resolution.enums
|
|
|
|
|> EnumConstructor.Map.filter_map (fun c_uid _ ->
|
|
|
|
match EnumConstructor.Map.find_opt c_uid cases_d with
|
2022-03-08 15:04:27 +03:00
|
|
|
| Some _ -> None
|
2021-06-03 13:51:57 +03:00
|
|
|
| None -> Some c_uid)
|
2021-06-03 11:24:01 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
if EnumConstructor.Map.is_empty missing_constructors then
|
2021-06-03 11:24:01 +03:00
|
|
|
Errors.format_spanned_warning case_pos
|
2022-03-08 15:04:27 +03:00
|
|
|
"Unreachable match case, all constructors of the enumeration %a \
|
|
|
|
are already specified"
|
2022-08-12 23:42:39 +03:00
|
|
|
EnumName.format_t e_uid;
|
2022-03-08 15:04:27 +03:00
|
|
|
(* The current used strategy is to replace the wildcard branch:
|
2021-06-03 17:38:07 +03:00
|
|
|
match foo with
|
|
|
|
| Case1 x -> x
|
2022-05-12 16:10:55 +03:00
|
|
|
| _ -> 1
|
|
|
|
with:
|
2021-06-03 13:09:54 +03:00
|
|
|
let wildcard_payload = 1 in
|
2021-06-03 17:38:07 +03:00
|
|
|
match foo with
|
|
|
|
| Case1 x -> x
|
|
|
|
| Case2 -> wildcard_payload
|
2022-05-12 16:10:55 +03:00
|
|
|
...
|
2021-06-03 17:38:07 +03:00
|
|
|
| CaseN -> wildcard_payload *)
|
2021-06-03 13:09:54 +03:00
|
|
|
(* Creates the wildcard payload *)
|
2022-06-03 17:40:03 +03:00
|
|
|
let ctxt, payload_var = create_var None in
|
2022-02-28 20:34:32 +03:00
|
|
|
let case_body =
|
|
|
|
translate_expr scope inside_definition_of ctxt match_case_expr
|
2021-06-03 11:24:01 +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
|
|
|
let e_binder = Expr.bind [| payload_var |] case_body in
|
2021-06-03 13:09:54 +03:00
|
|
|
|
2021-06-03 17:38:07 +03:00
|
|
|
(* For each missing cases, binds the wildcard payload. *)
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumConstructor.Map.fold
|
2021-06-03 17:38:07 +03:00
|
|
|
(fun c_uid _ (cases_d, e_uid_opt, curr_index) ->
|
|
|
|
let case_expr =
|
2022-06-03 17:40:03 +03:00
|
|
|
bind_case_body c_uid e_uid ctxt case_body e_binder
|
2021-06-03 17:38:07 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
( EnumConstructor.Map.add c_uid case_expr cases_d,
|
2021-06-03 17:38:07 +03:00
|
|
|
e_uid_opt,
|
|
|
|
curr_index + 1 ))
|
2021-11-07 01:16:58 +03:00
|
|
|
missing_constructors
|
|
|
|
(cases_d, Some e_uid, curr_index))
|
2021-06-03 11:24:01 +03:00
|
|
|
in
|
2022-08-25 17:35:08 +03:00
|
|
|
let naked_expr, e_name, _ =
|
2022-11-21 12:12:45 +03:00
|
|
|
List.fold_left bind_match_cases (EnumConstructor.Map.empty, None, 0) cases
|
2021-01-18 18:21:55 +03:00
|
|
|
in
|
2022-08-25 17:35:08 +03:00
|
|
|
naked_expr, Option.get e_name
|
2021-11-07 01:16:58 +03:00
|
|
|
[@@ocamlformat "wrap-comments=false"]
|
2021-01-18 18:21:55 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** {1 Translating scope definitions} *)
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** A scope use can be annotated with a pervasive precondition, in which case
|
|
|
|
this precondition has to be appended to the justifications of each
|
|
|
|
definition in the subscope use. This is what this function does. *)
|
2022-02-28 20:34:32 +03:00
|
|
|
let merge_conditions
|
2022-11-07 15:50:28 +03:00
|
|
|
(precond : Ast.expr boxed option)
|
|
|
|
(cond : Ast.expr boxed option)
|
|
|
|
(default_pos : Pos.t) : Ast.expr boxed =
|
2020-11-24 17:48:57 +03:00
|
|
|
match precond, cond with
|
|
|
|
| Some precond, Some cond ->
|
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 op_term = Expr.eop (Binop And) (Marked.get_mark cond) in
|
|
|
|
Expr.eapp op_term [precond; cond] (Marked.get_mark cond)
|
|
|
|
| Some precond, None -> Marked.unmark precond, Untyped { pos = default_pos }
|
2022-04-25 16:28:16 +03:00
|
|
|
| None, Some cond -> cond
|
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
|
|
|
| None, None -> Expr.elit (LBool true) (Untyped { pos = default_pos })
|
2020-11-24 17:48:57 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Translates a surface definition into condition into a desugared {!type:
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.rule} *)
|
2020-11-24 17:48:57 +03:00
|
|
|
let process_default
|
|
|
|
(ctxt : Name_resolution.context)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2022-11-07 15:50:28 +03:00
|
|
|
(def_key : Ast.ScopeDef.t Marked.pos)
|
|
|
|
(rule_id : RuleName.t)
|
|
|
|
(param_uid : Ast.expr Var.t Marked.pos option)
|
|
|
|
(precond : Ast.expr boxed option)
|
|
|
|
(exception_situation : Ast.exception_situation)
|
|
|
|
(label_situation : Ast.label_situation)
|
|
|
|
(just : Surface.Ast.expression Marked.pos option)
|
|
|
|
(cons : Surface.Ast.expression Marked.pos) : Ast.rule =
|
2022-02-28 20:34:32 +03:00
|
|
|
let just =
|
|
|
|
match just with
|
|
|
|
| Some just -> Some (translate_expr scope (Some def_key) ctxt just)
|
|
|
|
| None -> None
|
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
let just = merge_conditions precond just (Marked.get_mark def_key) in
|
2022-02-28 20:34:32 +03:00
|
|
|
let cons = translate_expr scope (Some def_key) ctxt cons in
|
2020-11-25 16:35:26 +03:00
|
|
|
{
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_just = just;
|
|
|
|
rule_cons = cons;
|
|
|
|
rule_parameter =
|
2020-12-18 15:13:51 +03:00
|
|
|
(let def_key_typ =
|
2022-05-30 12:20:48 +03:00
|
|
|
Name_resolution.get_def_typ ctxt (Marked.unmark def_key)
|
2020-12-18 15:13:51 +03:00
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark def_key_typ, param_uid with
|
2022-08-25 13:09:51 +03:00
|
|
|
| TArrow (t_in, _), Some param_uid -> Some (Marked.unmark param_uid, t_in)
|
|
|
|
| TArrow _, None ->
|
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
|
|
|
Errors.raise_spanned_error (Expr.pos cons)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This definition has a function type but the parameter is missing"
|
2020-11-25 18:51:19 +03:00
|
|
|
| _, Some _ ->
|
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
|
|
|
Errors.raise_spanned_error (Expr.pos cons)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This definition has a parameter but its type is not a function"
|
2020-11-25 18:51:19 +03:00
|
|
|
| _ -> None);
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_exception = exception_situation;
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id;
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_label = label_situation;
|
2020-11-25 16:35:26 +03:00
|
|
|
}
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Wrapper around {!val: process_default} that performs some name
|
|
|
|
disambiguation *)
|
2022-02-28 20:34:32 +03:00
|
|
|
let process_def
|
2022-11-07 15:50:28 +03:00
|
|
|
(precond : Ast.expr boxed option)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_uid : ScopeName.t)
|
2020-11-25 12:49:53 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(prgm : Ast.program)
|
|
|
|
(def : Surface.Ast.definition) : Ast.program =
|
2022-11-21 12:12:45 +03:00
|
|
|
let scope : Ast.scope = ScopeName.Map.find scope_uid prgm.program_scopes in
|
|
|
|
let scope_ctxt = ScopeName.Map.find scope_uid ctxt.scopes in
|
2021-01-22 07:47:48 +03:00
|
|
|
let def_key =
|
2022-02-28 20:34:32 +03:00
|
|
|
Name_resolution.get_def_key
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.unmark def.definition_name)
|
2022-02-28 20:34:32 +03:00
|
|
|
def.definition_state scope_uid ctxt
|
2022-09-20 15:04:07 +03:00
|
|
|
(Marked.get_mark def.definition_name)
|
2021-01-22 07:47:48 +03:00
|
|
|
in
|
2022-01-03 20:39:59 +03:00
|
|
|
let scope_def_ctxt =
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDefMap.find def_key scope_ctxt.scope_defs_contexts
|
2022-01-03 20:39:59 +03:00
|
|
|
in
|
2020-11-27 18:27:10 +03:00
|
|
|
(* We add to the name resolution context the name of the parameter variable *)
|
|
|
|
let param_uid, new_ctxt =
|
|
|
|
match def.definition_parameter with
|
|
|
|
| None -> None, ctxt
|
|
|
|
| Some param ->
|
2022-06-03 17:40:03 +03:00
|
|
|
let ctxt, param_var =
|
|
|
|
Name_resolution.add_def_local_var ctxt (Marked.unmark param)
|
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
Some (Marked.same_mark_as param_var param), ctxt
|
2020-11-27 18:27:10 +03:00
|
|
|
in
|
2020-11-23 11:22:47 +03:00
|
|
|
let scope_updated =
|
2022-11-07 15:50:28 +03:00
|
|
|
let scope_def = Ast.ScopeDefMap.find def_key scope.scope_defs in
|
2022-01-04 20:19:15 +03:00
|
|
|
let rule_name = def.definition_id in
|
2022-07-13 16:00:57 +03:00
|
|
|
let label_situation =
|
|
|
|
match def.definition_label with
|
|
|
|
| Some (label_str, label_pos) ->
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ExplicitlyLabeled
|
2022-11-22 22:57:59 +03:00
|
|
|
(IdentName.Map.find label_str scope_def_ctxt.label_idmap, label_pos)
|
2022-11-07 15:50:28 +03:00
|
|
|
| None -> Ast.Unlabeled
|
2022-07-13 16:00:57 +03:00
|
|
|
in
|
|
|
|
let exception_situation =
|
2022-11-07 15:50:28 +03:00
|
|
|
match def.Surface.Ast.definition_exception_to with
|
|
|
|
| NotAnException -> Ast.BaseCase
|
2022-01-04 20:19:15 +03:00
|
|
|
| UnlabeledException -> (
|
|
|
|
match scope_def_ctxt.default_exception_rulename with
|
2022-01-28 19:31:31 +03:00
|
|
|
| None | Some (Name_resolution.Ambiguous _) ->
|
2022-07-13 16:00:57 +03:00
|
|
|
(* This should have been caught previously by
|
|
|
|
check_unlabeled_exception *)
|
2022-01-28 19:31:31 +03:00
|
|
|
assert false (* should not happen *)
|
2022-01-05 17:57:18 +03:00
|
|
|
| Some (Name_resolution.Unique (name, pos)) ->
|
2022-08-25 17:08:08 +03:00
|
|
|
ExceptionToRule (name, pos))
|
2022-07-13 16:00:57 +03:00
|
|
|
| ExceptionToLabel label_str -> (
|
2022-01-04 20:19:15 +03:00
|
|
|
try
|
2022-01-05 17:37:34 +03:00
|
|
|
let label_id =
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.find (Marked.unmark label_str)
|
2022-01-05 17:37:34 +03:00
|
|
|
scope_def_ctxt.label_idmap
|
|
|
|
in
|
2022-08-25 17:08:08 +03:00
|
|
|
ExceptionToLabel (label_id, Marked.get_mark label_str)
|
2022-01-04 20:19:15 +03:00
|
|
|
with Not_found ->
|
2022-07-13 16:00:57 +03:00
|
|
|
Errors.raise_spanned_error
|
|
|
|
(Marked.get_mark label_str)
|
2022-03-08 15:04:27 +03:00
|
|
|
"Unknown label for the scope variable %a: \"%s\""
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDef.format_t def_key (Marked.unmark label_str))
|
2020-11-23 11:22:47 +03:00
|
|
|
in
|
2022-01-04 20:19:15 +03:00
|
|
|
let scope_def =
|
|
|
|
{
|
|
|
|
scope_def with
|
|
|
|
scope_def_rules =
|
2022-11-21 12:12:45 +03:00
|
|
|
RuleName.Map.add rule_name
|
2022-01-04 20:19:15 +03:00
|
|
|
(process_default new_ctxt scope_uid
|
2022-05-30 12:20:48 +03:00
|
|
|
(def_key, Marked.get_mark def.definition_name)
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_name param_uid precond exception_situation label_situation
|
|
|
|
def.definition_condition def.definition_expr)
|
2022-01-04 20:19:15 +03:00
|
|
|
scope_def.scope_def_rules;
|
|
|
|
}
|
2020-11-23 11:22:47 +03:00
|
|
|
in
|
2022-01-04 20:19:15 +03:00
|
|
|
{
|
|
|
|
scope with
|
2022-11-07 15:50:28 +03:00
|
|
|
scope_defs = Ast.ScopeDefMap.add def_key scope_def scope.scope_defs;
|
2022-01-04 20:19:15 +03:00
|
|
|
}
|
2020-11-23 11:22:47 +03:00
|
|
|
in
|
2020-12-04 18:40:17 +03:00
|
|
|
{
|
|
|
|
prgm with
|
2022-11-21 12:12:45 +03:00
|
|
|
program_scopes =
|
|
|
|
ScopeName.Map.add scope_uid scope_updated prgm.program_scopes;
|
2020-12-04 18:40:17 +03:00
|
|
|
}
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Translates a {!type: Surface.Ast.rule} from the surface language *)
|
2022-02-28 20:34:32 +03:00
|
|
|
let process_rule
|
2022-11-07 15:50:28 +03:00
|
|
|
(precond : Ast.expr boxed option)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2020-11-25 12:49:53 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(prgm : Ast.program)
|
|
|
|
(rule : Surface.Ast.rule) : Ast.program =
|
|
|
|
let def = Surface.Ast.rule_to_def rule in
|
2020-11-25 12:49:53 +03:00
|
|
|
process_def precond scope ctxt prgm def
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Translates assertions *)
|
2022-02-28 20:34:32 +03:00
|
|
|
let process_assert
|
2022-11-07 15:50:28 +03:00
|
|
|
(precond : Ast.expr boxed option)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_uid : ScopeName.t)
|
2020-12-10 20:11:43 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(prgm : Ast.program)
|
|
|
|
(ass : Surface.Ast.assertion) : Ast.program =
|
2022-11-21 12:12:45 +03:00
|
|
|
let scope : Ast.scope = ScopeName.Map.find scope_uid prgm.program_scopes in
|
2020-12-10 20:11:43 +03:00
|
|
|
let ass =
|
2022-02-28 20:34:32 +03:00
|
|
|
translate_expr scope_uid None ctxt
|
2022-11-07 15:50:28 +03:00
|
|
|
(match ass.Surface.Ast.assertion_condition with
|
|
|
|
| None -> ass.Surface.Ast.assertion_content
|
2020-12-10 20:11:43 +03:00
|
|
|
| Some cond ->
|
2022-11-07 15:50:28 +03:00
|
|
|
( Surface.Ast.IfThenElse
|
2021-01-20 18:06:04 +03:00
|
|
|
( cond,
|
2022-11-07 15:50:28 +03:00
|
|
|
ass.Surface.Ast.assertion_content,
|
|
|
|
Marked.same_mark_as (Surface.Ast.Literal (Surface.Ast.LBool true))
|
|
|
|
cond ),
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.get_mark cond ))
|
2020-12-10 20:11:43 +03:00
|
|
|
in
|
|
|
|
let ass =
|
|
|
|
match precond with
|
|
|
|
| Some precond ->
|
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.eifthenelse precond ass
|
|
|
|
(Expr.elit (LBool true) (Marked.get_mark precond))
|
|
|
|
(Marked.get_mark precond)
|
2020-12-10 20:11:43 +03:00
|
|
|
| None -> ass
|
|
|
|
in
|
|
|
|
let new_scope =
|
|
|
|
{ scope with scope_assertions = ass :: scope.scope_assertions }
|
|
|
|
in
|
|
|
|
{
|
|
|
|
prgm with
|
2022-11-21 12:12:45 +03:00
|
|
|
program_scopes = ScopeName.Map.add scope_uid new_scope prgm.program_scopes;
|
2020-12-10 20:11:43 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Translates a surface definition, rule or assertion *)
|
2020-11-25 12:49:53 +03:00
|
|
|
let process_scope_use_item
|
2022-11-07 15:50:28 +03:00
|
|
|
(precond : Surface.Ast.expression Marked.pos option)
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2020-11-25 12:49:53 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(prgm : Ast.program)
|
|
|
|
(item : Surface.Ast.scope_use_item Marked.pos) : Ast.program =
|
2022-02-28 20:34:32 +03:00
|
|
|
let precond = Option.map (translate_expr scope None ctxt) precond in
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark item with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Rule rule -> process_rule precond scope ctxt prgm rule
|
|
|
|
| Surface.Ast.Definition def -> process_def precond scope ctxt prgm def
|
|
|
|
| Surface.Ast.Assertion ass -> process_assert precond scope ctxt prgm ass
|
2020-11-23 11:22:47 +03:00
|
|
|
| _ -> prgm
|
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** {1 Translating top-level items} *)
|
|
|
|
|
2021-01-22 07:47:48 +03:00
|
|
|
(* If this is an unlabeled exception, ensures that it has a unique default
|
|
|
|
definition *)
|
|
|
|
let check_unlabeled_exception
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope : ScopeName.t)
|
2021-01-22 07:47:48 +03:00
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(item : Surface.Ast.scope_use_item Marked.pos) : unit =
|
2022-11-21 12:12:45 +03:00
|
|
|
let scope_ctxt = ScopeName.Map.find scope ctxt.scopes in
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark item with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Rule _ | Surface.Ast.Definition _ -> (
|
2022-01-05 17:57:18 +03:00
|
|
|
let def_key, exception_to =
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark item with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Rule rule ->
|
2022-02-28 20:34:32 +03:00
|
|
|
( Name_resolution.get_def_key
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.unmark rule.rule_name)
|
2022-02-28 20:34:32 +03:00
|
|
|
rule.rule_state scope ctxt
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.get_mark rule.rule_name),
|
2022-01-05 17:57:18 +03:00
|
|
|
rule.rule_exception_to )
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Definition def ->
|
2022-02-28 20:34:32 +03:00
|
|
|
( Name_resolution.get_def_key
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.unmark def.definition_name)
|
2022-02-28 20:34:32 +03:00
|
|
|
def.definition_state scope ctxt
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.get_mark def.definition_name),
|
2022-01-05 17:57:18 +03:00
|
|
|
def.definition_exception_to )
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *)
|
2022-01-03 20:39:59 +03:00
|
|
|
in
|
|
|
|
let scope_def_ctxt =
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDefMap.find def_key scope_ctxt.scope_defs_contexts
|
2022-01-03 20:39:59 +03:00
|
|
|
in
|
2022-01-05 17:57:18 +03:00
|
|
|
match exception_to with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.NotAnException | Surface.Ast.ExceptionToLabel _ -> ()
|
2021-01-22 07:47:48 +03:00
|
|
|
(* If this is an unlabeled exception, we check that it has a unique default
|
|
|
|
definition *)
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.UnlabeledException -> (
|
2022-01-03 20:39:59 +03:00
|
|
|
match scope_def_ctxt.default_exception_rulename with
|
2021-01-22 07:47:48 +03:00
|
|
|
| None ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark item)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This exception does not have a corresponding definition"
|
2021-04-03 20:31:33 +03:00
|
|
|
| Some (Ambiguous pos) ->
|
|
|
|
Errors.raise_multispanned_error
|
2022-05-30 12:20:48 +03:00
|
|
|
([Some "Ambiguous exception", Marked.get_mark item]
|
2021-04-03 20:31:33 +03:00
|
|
|
@ List.map (fun p -> Some "Candidate definition", p) pos)
|
2022-03-08 15:04:27 +03:00
|
|
|
"This exception can refer to several definitions. Try using labels \
|
|
|
|
to disambiguate"
|
2021-03-16 22:34:03 +03:00
|
|
|
| Some (Unique _) -> ()))
|
2021-01-22 07:47:48 +03:00
|
|
|
| _ -> ()
|
2021-01-21 08:07:09 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Translates a surface scope use, which is a bunch of definitions *)
|
2020-11-25 12:49:53 +03:00
|
|
|
let process_scope_use
|
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-07 15:50:28 +03:00
|
|
|
(prgm : Ast.program)
|
|
|
|
(use : Surface.Ast.scope_use) : Ast.program =
|
2022-10-21 16:47:17 +03:00
|
|
|
let scope_uid = Name_resolution.get_scope ctxt use.scope_use_name in
|
2020-11-23 11:22:47 +03:00
|
|
|
(* Make sure the scope exists *)
|
|
|
|
let prgm =
|
2022-11-21 12:12:45 +03:00
|
|
|
match ScopeName.Map.find_opt scope_uid prgm.program_scopes with
|
2020-11-23 11:22:47 +03:00
|
|
|
| Some _ -> prgm
|
2020-12-09 12:36:09 +03:00
|
|
|
| None -> assert false
|
|
|
|
(* should not happen *)
|
2020-11-23 11:22:47 +03:00
|
|
|
in
|
2020-11-25 12:49:53 +03:00
|
|
|
let precond = use.scope_use_condition in
|
2021-01-22 07:47:48 +03:00
|
|
|
List.iter (check_unlabeled_exception scope_uid ctxt) use.scope_use_items;
|
2020-11-25 12:49:53 +03:00
|
|
|
List.fold_left
|
|
|
|
(process_scope_use_item precond scope_uid ctxt)
|
|
|
|
prgm use.scope_use_items
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2022-11-07 15:50:28 +03:00
|
|
|
let attribute_to_io (attr : Surface.Ast.scope_decl_context_io) : Ast.io =
|
2022-02-07 12:30:36 +03:00
|
|
|
{
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.io_output = attr.scope_decl_context_io_output;
|
|
|
|
Ast.io_input =
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.map_under_mark
|
2022-02-07 12:30:36 +03:00
|
|
|
(fun io ->
|
|
|
|
match io with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.Input -> Ast.OnlyInput
|
|
|
|
| Surface.Ast.Internal -> Ast.NoInput
|
|
|
|
| Surface.Ast.Context -> Ast.Reentrant)
|
2022-02-07 12:30:36 +03:00
|
|
|
attr.scope_decl_context_io_input;
|
|
|
|
}
|
2022-02-05 02:04:19 +03:00
|
|
|
|
2022-11-03 17:18:51 +03:00
|
|
|
let init_scope_defs
|
|
|
|
(ctxt : Name_resolution.context)
|
2022-11-22 22:57:59 +03:00
|
|
|
(scope_idmap : Name_resolution.scope_var_or_subscope IdentName.Map.t) :
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_def Ast.ScopeDefMap.t =
|
2022-10-21 16:47:17 +03:00
|
|
|
(* Initializing the definitions of all scopes and subscope vars, with no rules
|
|
|
|
yet inside *)
|
|
|
|
let add_def _ v scope_def_map =
|
|
|
|
match v with
|
|
|
|
| Name_resolution.ScopeVar v -> (
|
2022-11-21 12:12:45 +03:00
|
|
|
let v_sig = ScopeVar.Map.find v ctxt.Name_resolution.var_typs in
|
2022-10-21 16:47:17 +03:00
|
|
|
match v_sig.var_sig_states_list with
|
|
|
|
| [] ->
|
2022-11-07 15:50:28 +03:00
|
|
|
let def_key = Ast.ScopeDef.Var (v, None) in
|
|
|
|
Ast.ScopeDefMap.add def_key
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
2022-11-21 12:12:45 +03:00
|
|
|
Ast.scope_def_rules = RuleName.Map.empty;
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_def_typ = v_sig.var_sig_typ;
|
|
|
|
Ast.scope_def_is_condition = v_sig.var_sig_is_condition;
|
|
|
|
Ast.scope_def_io = attribute_to_io v_sig.var_sig_io;
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
|
|
|
scope_def_map
|
|
|
|
| states ->
|
|
|
|
let scope_def, _ =
|
|
|
|
List.fold_left
|
|
|
|
(fun (acc, i) state ->
|
2022-11-07 15:50:28 +03:00
|
|
|
let def_key = Ast.ScopeDef.Var (v, Some state) in
|
2022-10-21 16:47:17 +03:00
|
|
|
let def =
|
|
|
|
{
|
2022-11-21 12:12:45 +03:00
|
|
|
Ast.scope_def_rules = RuleName.Map.empty;
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_def_typ = v_sig.var_sig_typ;
|
|
|
|
Ast.scope_def_is_condition = v_sig.var_sig_is_condition;
|
|
|
|
Ast.scope_def_io =
|
2022-10-21 16:47:17 +03:00
|
|
|
(* The first state should have the input I/O of the original
|
|
|
|
variable, and the last state should have the output I/O
|
|
|
|
of the original variable. All intermediate states shall
|
|
|
|
have "internal" I/O.*)
|
|
|
|
(let original_io = attribute_to_io v_sig.var_sig_io in
|
|
|
|
let io_input =
|
|
|
|
if i = 0 then original_io.io_input
|
|
|
|
else
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.NoInput, Marked.get_mark (StateName.get_info state)
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
|
|
|
let io_output =
|
|
|
|
if i = List.length states - 1 then original_io.io_output
|
|
|
|
else false, Marked.get_mark (StateName.get_info state)
|
|
|
|
in
|
|
|
|
{ io_input; io_output });
|
|
|
|
}
|
|
|
|
in
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDefMap.add def_key def acc, i + 1)
|
2022-10-21 16:47:17 +03:00
|
|
|
(scope_def_map, 0) states
|
|
|
|
in
|
|
|
|
scope_def)
|
|
|
|
| Name_resolution.SubScope (v0, subscope_uid) ->
|
|
|
|
let sub_scope_def =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeName.Map.find subscope_uid ctxt.Name_resolution.scopes
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.fold
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun _ v scope_def_map ->
|
|
|
|
match v with
|
|
|
|
| Name_resolution.SubScope _ -> scope_def_map
|
|
|
|
| Name_resolution.ScopeVar v ->
|
|
|
|
(* TODO: shouldn't we ignore internal variables too at this point
|
|
|
|
? *)
|
2022-11-21 12:12:45 +03:00
|
|
|
let v_sig = ScopeVar.Map.find v ctxt.Name_resolution.var_typs in
|
2022-10-21 16:47:17 +03:00
|
|
|
let def_key =
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDef.SubScopeVar
|
2022-10-21 16:47:17 +03:00
|
|
|
(v0, v, Marked.get_mark (ScopeVar.get_info v))
|
|
|
|
in
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.ScopeDefMap.add def_key
|
2022-10-21 16:47:17 +03:00
|
|
|
{
|
2022-11-21 12:12:45 +03:00
|
|
|
Ast.scope_def_rules = RuleName.Map.empty;
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_def_typ = v_sig.var_sig_typ;
|
|
|
|
Ast.scope_def_is_condition = v_sig.var_sig_is_condition;
|
|
|
|
Ast.scope_def_io = attribute_to_io v_sig.var_sig_io;
|
2022-10-21 16:47:17 +03:00
|
|
|
}
|
|
|
|
scope_def_map)
|
|
|
|
sub_scope_def.Name_resolution.var_idmap scope_def_map
|
|
|
|
in
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.fold add_def scope_idmap Ast.ScopeDefMap.empty
|
2022-10-21 16:47:17 +03:00
|
|
|
|
2020-12-14 17:23:04 +03:00
|
|
|
(** Main function of this module *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let translate_program
|
|
|
|
(ctxt : Name_resolution.context)
|
|
|
|
(prgm : Surface.Ast.program) : Ast.program =
|
2020-12-04 18:40:17 +03:00
|
|
|
let empty_prgm =
|
2022-10-21 16:47:17 +03:00
|
|
|
let program_scopes =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeName.Map.mapi
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun s_uid s_context ->
|
|
|
|
let scope_vars =
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.fold
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun _ v acc ->
|
|
|
|
match v with
|
|
|
|
| Name_resolution.SubScope _ -> acc
|
|
|
|
| Name_resolution.ScopeVar v -> (
|
2022-11-21 12:12:45 +03:00
|
|
|
let v_sig = ScopeVar.Map.find v ctxt.var_typs in
|
2022-10-21 16:47:17 +03:00
|
|
|
match v_sig.var_sig_states_list with
|
2022-11-21 12:12:45 +03:00
|
|
|
| [] -> ScopeVar.Map.add v Ast.WholeVar acc
|
|
|
|
| states -> ScopeVar.Map.add v (Ast.States states) acc))
|
|
|
|
s_context.Name_resolution.var_idmap ScopeVar.Map.empty
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
|
|
|
let scope_sub_scopes =
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.fold
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun _ v acc ->
|
|
|
|
match v with
|
|
|
|
| Name_resolution.ScopeVar _ -> acc
|
|
|
|
| Name_resolution.SubScope (sub_var, sub_scope) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
SubScopeName.Map.add sub_var sub_scope acc)
|
|
|
|
s_context.Name_resolution.var_idmap SubScopeName.Map.empty
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
|
|
|
{
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_vars;
|
2022-10-21 16:47:17 +03:00
|
|
|
scope_sub_scopes;
|
|
|
|
scope_defs = init_scope_defs ctxt s_context.var_idmap;
|
|
|
|
scope_assertions = [];
|
|
|
|
scope_meta_assertions = [];
|
|
|
|
scope_uid = s_uid;
|
|
|
|
})
|
|
|
|
ctxt.Name_resolution.scopes
|
|
|
|
in
|
2020-12-04 18:40:17 +03:00
|
|
|
{
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.program_ctx =
|
2022-08-25 17:08:08 +03:00
|
|
|
{
|
2022-11-17 19:13:35 +03:00
|
|
|
ctx_structs = ctxt.Name_resolution.structs;
|
|
|
|
ctx_enums = ctxt.Name_resolution.enums;
|
2022-10-21 16:47:17 +03:00
|
|
|
ctx_scopes =
|
2022-11-22 22:57:59 +03:00
|
|
|
IdentName.Map.fold
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun _ def acc ->
|
|
|
|
match def with
|
2022-11-17 19:13:35 +03:00
|
|
|
| Name_resolution.TScope (scope, scope_out_struct) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeName.Map.add scope scope_out_struct acc
|
2022-10-21 16:47:17 +03:00
|
|
|
| _ -> acc)
|
2022-11-21 12:12:45 +03:00
|
|
|
ctxt.Name_resolution.typedefs ScopeName.Map.empty;
|
2022-08-25 17:08:08 +03:00
|
|
|
};
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.program_scopes;
|
2020-12-04 18:40:17 +03:00
|
|
|
}
|
|
|
|
in
|
2021-05-15 02:16:08 +03:00
|
|
|
let rec processer_structure
|
2022-11-07 15:50:28 +03:00
|
|
|
(prgm : Ast.program)
|
|
|
|
(item : Surface.Ast.law_structure) : Ast.program =
|
2020-11-23 11:22:47 +03:00
|
|
|
match item with
|
2021-05-15 02:16:08 +03:00
|
|
|
| LawHeading (_, children) ->
|
|
|
|
List.fold_left
|
|
|
|
(fun prgm child -> processer_structure prgm child)
|
|
|
|
prgm children
|
|
|
|
| CodeBlock (block, _, _) ->
|
2020-11-25 12:10:27 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun prgm item ->
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark item with
|
2022-11-07 15:50:28 +03:00
|
|
|
| Surface.Ast.ScopeUse use -> process_scope_use ctxt prgm use
|
2020-11-25 12:10:27 +03:00
|
|
|
| _ -> prgm)
|
|
|
|
prgm block
|
2021-05-15 02:16:08 +03:00
|
|
|
| LawInclude _ | LawText _ -> prgm
|
2020-11-23 11:22:47 +03:00
|
|
|
in
|
2021-05-15 02:16:08 +03:00
|
|
|
List.fold_left processer_structure empty_prgm prgm.program_items
|