2020-11-23 13:42:29 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2022-03-01 22:41:01 +03:00
|
|
|
|
2022-08-25 13:09:51 +03:00
|
|
|
type location = scopelang glocation
|
2020-11-23 18:12:45 +03:00
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
module LocationSet : Set.S with type elt = location Mark.pos = Set.Make (struct
|
|
|
|
type t = location Mark.pos
|
2020-12-03 23:02:28 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
let compare = Expr.compare_location
|
2020-12-03 23:02:28 +03:00
|
|
|
end)
|
|
|
|
|
2023-05-17 17:15:00 +03:00
|
|
|
type 'm expr = (scopelang, 'm) gexpr
|
2022-06-03 17:40:03 +03:00
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
let rec locations_used (e : 'm expr) : LocationSet.t =
|
2022-10-10 16:15:36 +03:00
|
|
|
match e with
|
|
|
|
| ELocation l, pos -> LocationSet.singleton (l, Expr.mark_pos pos)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; _ }, _ ->
|
2020-11-25 13:53:56 +03:00
|
|
|
let _, body = Bindlib.unmbind binder in
|
|
|
|
locations_used body
|
2022-10-10 16:15:36 +03:00
|
|
|
| e ->
|
|
|
|
Expr.shallow_fold
|
|
|
|
(fun e -> LocationSet.union (locations_used e))
|
|
|
|
e LocationSet.empty
|
2020-11-25 13:53:56 +03:00
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
type 'm rule =
|
2023-05-17 16:44:57 +03:00
|
|
|
| Definition of location Mark.pos * typ * Desugared.Ast.io * 'm expr
|
2022-09-23 18:43:48 +03:00
|
|
|
| Assertion of 'm expr
|
2023-08-30 18:49:29 +03:00
|
|
|
| Call of ScopeName.t * SubScopeName.t * 'm mark
|
2020-12-14 19:00:42 +03:00
|
|
|
|
2023-11-03 19:15:55 +03:00
|
|
|
type scope_var_ty = {
|
2023-11-07 20:25:57 +03:00
|
|
|
svar_in_ty : typ;
|
|
|
|
svar_out_ty : typ;
|
|
|
|
svar_io : Desugared.Ast.io;
|
2023-11-03 19:15:55 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
type 'm scope_decl = {
|
2020-12-14 19:00:42 +03:00
|
|
|
scope_decl_name : ScopeName.t;
|
2023-11-03 19:15:55 +03:00
|
|
|
scope_sig : scope_var_ty ScopeVar.Map.t;
|
2022-09-23 18:43:48 +03:00
|
|
|
scope_decl_rules : 'm rule list;
|
2023-05-17 16:44:57 +03:00
|
|
|
scope_options : Desugared.Ast.catala_option Mark.pos list;
|
2020-12-14 19:00:42 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
type 'm program = {
|
2023-09-19 12:44:18 +03:00
|
|
|
program_module_name : ModuleName.t option;
|
2023-08-10 17:52:39 +03:00
|
|
|
program_scopes : 'm scope_decl Mark.pos ScopeName.Map.t;
|
2023-02-13 17:00:23 +03:00
|
|
|
program_topdefs : ('m expr * typ) TopdefName.Map.t;
|
2023-08-10 17:52:39 +03:00
|
|
|
program_modules : nil program ModuleName.Map.t;
|
2022-08-25 13:09:51 +03:00
|
|
|
program_ctx : decl_ctx;
|
2023-09-22 18:50:19 +03:00
|
|
|
program_lang : Cli.backend_lang;
|
2020-12-14 19:00:42 +03:00
|
|
|
}
|
2022-09-23 18:43:48 +03:00
|
|
|
|
2022-09-26 17:32:02 +03:00
|
|
|
let type_rule decl_ctx env = function
|
|
|
|
| Definition (loc, typ, io, expr) ->
|
2022-12-13 18:06:36 +03:00
|
|
|
let expr' = Typing.expr ~leave_unresolved:false decl_ctx ~env ~typ expr 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
|
|
|
Definition (loc, typ, io, Expr.unbox expr')
|
2022-09-26 17:32:02 +03:00
|
|
|
| Assertion expr ->
|
2023-05-17 16:44:57 +03:00
|
|
|
let typ = Mark.add (Expr.pos expr) (TLit TBool) in
|
2022-12-13 18:06:36 +03:00
|
|
|
let expr' = Typing.expr ~leave_unresolved:false decl_ctx ~env ~typ expr 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
|
|
|
Assertion (Expr.unbox expr')
|
2022-09-30 17:52:35 +03:00
|
|
|
| Call (sc_name, ssc_name, m) ->
|
|
|
|
let pos = Expr.mark_pos m in
|
2023-05-17 16:44:57 +03:00
|
|
|
Call (sc_name, ssc_name, Typed { pos; ty = Mark.add pos TAny })
|
2022-09-26 17:32:02 +03:00
|
|
|
|
|
|
|
let type_program (prg : 'm program) : typed program =
|
2023-08-31 17:54:45 +03:00
|
|
|
(* Caution: this environment building code is very similar to that in
|
|
|
|
desugared/disambiguate.ml. Any edits should probably be reflected. *)
|
2023-08-10 17:52:39 +03:00
|
|
|
let base_typing_env prg =
|
2023-08-31 17:54:45 +03:00
|
|
|
let env = Typing.Env.empty prg.program_ctx in
|
|
|
|
let env =
|
2023-08-10 17:52:39 +03:00
|
|
|
TopdefName.Map.fold
|
2023-08-31 17:54:45 +03:00
|
|
|
(fun name ty env -> Typing.Env.add_toplevel_var name ty env)
|
|
|
|
prg.program_ctx.ctx_topdefs env
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
2023-08-31 17:54:45 +03:00
|
|
|
let env =
|
2023-08-10 17:52:39 +03:00
|
|
|
ScopeName.Map.fold
|
2023-08-31 17:54:45 +03:00
|
|
|
(fun scope_name scope_decl env ->
|
2023-11-07 20:25:57 +03:00
|
|
|
let sg = (Mark.remove scope_decl).scope_sig in
|
|
|
|
let vars =
|
|
|
|
ScopeVar.Map.map (fun { svar_out_ty; _ } -> svar_out_ty) sg
|
|
|
|
in
|
|
|
|
let in_vars =
|
|
|
|
ScopeVar.Map.map (fun { svar_in_ty; _ } -> svar_in_ty) sg
|
|
|
|
in
|
|
|
|
Typing.Env.add_scope scope_name ~vars ~in_vars env)
|
2023-08-31 17:54:45 +03:00
|
|
|
prg.program_scopes env
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
2023-08-31 17:54:45 +03:00
|
|
|
env
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
|
|
|
let rec build_typing_env prg =
|
|
|
|
ModuleName.Map.fold
|
|
|
|
(fun modname prg ->
|
|
|
|
Typing.Env.add_module modname ~module_env:(build_typing_env prg))
|
|
|
|
prg.program_modules (base_typing_env prg)
|
|
|
|
in
|
2023-08-31 17:54:45 +03:00
|
|
|
let env =
|
2023-08-10 17:52:39 +03:00
|
|
|
ModuleName.Map.fold
|
|
|
|
(fun modname prg ->
|
|
|
|
Typing.Env.add_module modname ~module_env:(build_typing_env prg))
|
|
|
|
prg.program_modules (base_typing_env prg)
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
2023-02-13 17:00:23 +03:00
|
|
|
let program_topdefs =
|
2023-01-23 14:19:36 +03:00
|
|
|
TopdefName.Map.map
|
|
|
|
(fun (expr, typ) ->
|
2023-03-21 14:14:10 +03:00
|
|
|
( Expr.unbox
|
2023-08-31 17:54:45 +03:00
|
|
|
(Typing.expr prg.program_ctx ~leave_unresolved:false ~env ~typ expr),
|
2023-03-21 14:14:10 +03:00
|
|
|
typ ))
|
2023-02-13 17:00:23 +03:00
|
|
|
prg.program_topdefs
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
2022-09-26 17:32:02 +03:00
|
|
|
let program_scopes =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeName.Map.map
|
2023-08-10 17:52:39 +03:00
|
|
|
(Mark.map (fun scope_decl ->
|
2023-08-31 17:54:45 +03:00
|
|
|
let env =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.fold
|
2023-11-07 20:25:57 +03:00
|
|
|
(fun svar { svar_out_ty; _ } env ->
|
|
|
|
Typing.Env.add_scope_var svar svar_out_ty env)
|
2023-08-31 17:54:45 +03:00
|
|
|
scope_decl.scope_sig env
|
2022-09-26 17:32:02 +03:00
|
|
|
in
|
|
|
|
let scope_decl_rules =
|
|
|
|
List.map
|
2023-08-31 17:54:45 +03:00
|
|
|
(type_rule prg.program_ctx env)
|
2022-09-26 17:32:02 +03:00
|
|
|
scope_decl.scope_decl_rules
|
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
{ scope_decl with scope_decl_rules }))
|
2022-09-26 17:32:02 +03:00
|
|
|
prg.program_scopes
|
|
|
|
in
|
2023-02-13 17:00:23 +03:00
|
|
|
{ prg with program_topdefs; program_scopes }
|