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 =
|
2024-03-28 19:05:12 +03:00
|
|
|
| ScopeVarDefinition of {
|
2024-07-17 16:17:57 +03:00
|
|
|
var : (ScopeVar.t, Pos.t list) Mark.ed;
|
2024-03-28 19:05:12 +03:00
|
|
|
typ : typ;
|
|
|
|
io : Desugared.Ast.io;
|
|
|
|
e : 'm expr;
|
|
|
|
}
|
|
|
|
| SubScopeVarDefinition of {
|
2024-07-17 16:17:57 +03:00
|
|
|
var : (ScopeVar.t, Pos.t list) Mark.ed;
|
2024-03-29 17:50:23 +03:00
|
|
|
var_within_origin_scope : ScopeVar.t;
|
2024-03-28 19:05:12 +03:00
|
|
|
typ : typ;
|
|
|
|
e : 'm expr;
|
|
|
|
}
|
2022-09-23 18:43:48 +03:00
|
|
|
| Assertion of 'm expr
|
2020-12-14 19:00:42 +03:00
|
|
|
|
2023-11-03 19:15:55 +03:00
|
|
|
type scope_var_ty = {
|
|
|
|
svar_in_ty : typ;
|
|
|
|
svar_out_ty : typ;
|
|
|
|
svar_io : Desugared.Ast.io;
|
|
|
|
}
|
|
|
|
|
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;
|
2024-08-28 17:47:49 +03:00
|
|
|
scope_visibility : visibility;
|
2020-12-14 19:00:42 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:43:48 +03:00
|
|
|
type 'm program = {
|
2024-05-27 12:26:14 +03:00
|
|
|
program_module_name : (ModuleName.t * module_intf_id) option;
|
2023-11-20 18:01:06 +03:00
|
|
|
program_ctx : decl_ctx;
|
|
|
|
program_modules : nil scope_decl Mark.pos ScopeName.Map.t ModuleName.Map.t;
|
2023-08-10 17:52:39 +03:00
|
|
|
program_scopes : 'm scope_decl Mark.pos ScopeName.Map.t;
|
2024-08-28 17:47:49 +03:00
|
|
|
program_topdefs : ('m expr * typ * visibility) TopdefName.Map.t;
|
2024-03-15 16:23:30 +03:00
|
|
|
program_lang : Global.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
|
2024-03-28 19:05:12 +03:00
|
|
|
| ScopeVarDefinition ({ typ; e; _ } as def) ->
|
|
|
|
let e = Typing.expr decl_ctx ~env ~typ e in
|
|
|
|
ScopeVarDefinition { def with e = Expr.unbox e }
|
|
|
|
| SubScopeVarDefinition ({ typ; e; _ } as def) ->
|
|
|
|
let e = Typing.expr decl_ctx ~env ~typ e in
|
|
|
|
SubScopeVarDefinition { def with e = Expr.unbox e }
|
|
|
|
| Assertion e ->
|
|
|
|
let typ = Mark.add (Expr.pos e) (TLit TBool) in
|
|
|
|
let e = Typing.expr decl_ctx ~env ~typ e in
|
|
|
|
Assertion (Expr.unbox e)
|
2022-09-26 17:32:02 +03:00
|
|
|
|
2023-11-20 18:01:06 +03:00
|
|
|
let type_program (type m) (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-11-20 18:01:06 +03:00
|
|
|
let env = Typing.Env.empty prg.program_ctx in
|
|
|
|
let env =
|
|
|
|
TopdefName.Map.fold
|
2024-08-28 17:47:49 +03:00
|
|
|
(fun name (ty, _vis) env -> Typing.Env.add_toplevel_var name ty env)
|
2023-11-20 18:01:06 +03:00
|
|
|
prg.program_ctx.ctx_topdefs env
|
|
|
|
in
|
|
|
|
let env =
|
|
|
|
ScopeName.Map.fold
|
|
|
|
(fun scope_name _info env ->
|
|
|
|
let scope_sig =
|
|
|
|
match ScopeName.path scope_name with
|
|
|
|
| [] ->
|
|
|
|
(Mark.remove (ScopeName.Map.find scope_name prg.program_scopes))
|
|
|
|
.scope_sig
|
|
|
|
| p ->
|
|
|
|
let m = List.hd (List.rev p) in
|
|
|
|
let scope =
|
|
|
|
ScopeName.Map.find scope_name
|
|
|
|
(ModuleName.Map.find m prg.program_modules)
|
|
|
|
in
|
|
|
|
(Mark.remove scope).scope_sig
|
|
|
|
in
|
2023-11-03 19:15:55 +03:00
|
|
|
let vars =
|
2023-11-20 18:01:06 +03:00
|
|
|
ScopeVar.Map.map (fun { svar_out_ty; _ } -> svar_out_ty) scope_sig
|
2023-11-03 19:15:55 +03:00
|
|
|
in
|
|
|
|
let in_vars =
|
2023-11-20 18:01:06 +03:00
|
|
|
ScopeVar.Map.map (fun { svar_in_ty; _ } -> svar_in_ty) scope_sig
|
2023-11-03 19:15:55 +03:00
|
|
|
in
|
|
|
|
Typing.Env.add_scope scope_name ~vars ~in_vars env)
|
2023-11-20 18:01:06 +03:00
|
|
|
prg.program_ctx.ctx_scopes env
|
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
|
2024-08-28 17:47:49 +03:00
|
|
|
(fun (expr, typ, vis) ->
|
|
|
|
Expr.unbox (Typing.expr prg.program_ctx ~env ~typ expr), typ, vis)
|
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-03 19:15:55 +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 }
|
2024-06-17 16:57:47 +03:00
|
|
|
|
|
|
|
let type_program prg = Message.with_delayed_errors (fun () -> type_program prg)
|