catala/compiler/scopelang/scope_to_dcalc.ml

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

882 lines
32 KiB
OCaml
Raw Normal View History

2020-11-23 18:12:45 +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. *)
2021-01-21 23:33:04 +03:00
open Utils
open Shared_ast
2020-11-23 18:12:45 +03:00
2022-01-31 20:09:14 +03:00
type scope_var_ctx = {
scope_var_name : ScopeVar.t;
scope_var_typ : naked_typ;
scope_var_io : Ast.io;
2022-01-31 20:09:14 +03:00
}
type scope_sig_ctx = {
scope_sig_local_vars : scope_var_ctx list; (** List of scope variables *)
scope_sig_scope_var : untyped Dcalc.Ast.expr Var.t;
(** Var representing the scope *)
scope_sig_input_var : untyped Dcalc.Ast.expr Var.t;
2022-01-31 20:09:14 +03:00
(** Var representing the scope input inside the scope func *)
scope_sig_input_struct : StructName.t; (** Scope input *)
scope_sig_output_struct : StructName.t; (** Scope output *)
2022-01-31 20:09:14 +03:00
}
type scope_sigs_ctx = scope_sig_ctx Ast.ScopeMap.t
2020-11-26 19:06:32 +03:00
type ctx = {
structs : struct_ctx;
enums : enum_ctx;
scope_name : ScopeName.t;
2020-11-27 13:37:21 +03:00
scopes_parameters : scope_sigs_ctx;
scope_vars :
(untyped Dcalc.Ast.expr Var.t * naked_typ * Ast.io) ScopeVarMap.t;
subscope_vars :
(untyped Dcalc.Ast.expr Var.t * naked_typ * Ast.io) ScopeVarMap.t
Ast.SubScopeMap.t;
local_vars : (Ast.expr, untyped Dcalc.Ast.expr Var.t) Var.Map.t;
}
let empty_ctx
(struct_ctx : struct_ctx)
(enum_ctx : enum_ctx)
(scopes_ctx : scope_sigs_ctx)
(scope_name : ScopeName.t) =
{
structs = struct_ctx;
enums = enum_ctx;
2020-12-10 18:58:32 +03:00
scope_name;
2020-11-26 19:06:32 +03:00
scopes_parameters = scopes_ctx;
2022-08-25 17:08:08 +03:00
scope_vars = ScopeVarMap.empty;
subscope_vars = Ast.SubScopeMap.empty;
local_vars = Var.Map.empty;
}
2020-11-23 18:12:45 +03:00
2022-08-16 11:04:01 +03:00
let pos_mark (pos : Pos.t) : untyped mark = Untyped { pos }
let pos_mark_as e = pos_mark (Marked.get_mark e)
let merge_defaults
(caller : untyped Dcalc.Ast.expr Bindlib.box)
(callee : untyped Dcalc.Ast.expr Bindlib.box) :
untyped Dcalc.Ast.expr Bindlib.box =
let caller =
2022-07-11 12:32:23 +03:00
let m = Marked.get_mark (Bindlib.unbox caller) in
Expr.make_app caller [Bindlib.box (ELit LUnit, m)] m
in
let body =
Bindlib.box_apply2
(fun caller callee ->
2022-07-11 12:32:23 +03:00
let m = Marked.get_mark callee in
2022-08-16 11:04:01 +03:00
EDefault ([caller], (ELit (LBool true), m), callee), m)
caller callee
in
2020-11-27 13:54:22 +03:00
body
2020-11-23 18:12:45 +03:00
2021-01-21 23:33:04 +03:00
let tag_with_log_entry
(e : untyped Dcalc.Ast.expr Bindlib.box)
(l : log_entry)
2021-01-21 23:33:04 +03:00
(markings : Utils.Uid.MarkedString.info list) :
untyped Dcalc.Ast.expr Bindlib.box =
2021-01-21 23:33:04 +03:00
Bindlib.box_apply
(fun e ->
Marked.same_mark_as
2022-08-16 11:04:01 +03:00
(EApp (Marked.same_mark_as (EOp (Unop (Log (l, markings)))) e, [e]))
e)
2021-01-21 23:33:04 +03:00
e
(* In a list of exceptions, it is normally an error if more than a single one
apply at the same time. This relaxes this constraint slightly, allowing a
conflict if all the triggered conflicting exception yield syntactically equal
results (and as long as none of these exceptions have exceptions themselves)
NOTE: the choice of the exception that will be triggered and show in the
trace is arbitrary (but deterministic). *)
let collapse_similar_outcomes (excepts : Ast.expr list) : Ast.expr list =
let cons_map =
List.fold_left
(fun map -> function
| (EDefault ([], _, cons), _) as e ->
Ast.ExprMap.update cons
(fun prev -> Some (e :: Option.value ~default:[] prev))
map
| _ -> map)
Ast.ExprMap.empty excepts
in
let _, excepts =
List.fold_right
(fun e (cons_map, excepts) ->
match e with
| EDefault ([], _, cons), _ ->
let collapsed_exc =
List.fold_left
(fun acc -> function
| EDefault ([], just, cons), pos ->
[EDefault (acc, just, cons), pos]
| _ -> assert false)
[]
(Ast.ExprMap.find cons cons_map)
in
Ast.ExprMap.add cons [] cons_map, collapsed_exc @ excepts
| e -> cons_map, e :: excepts)
excepts (cons_map, [])
in
excepts
let rec translate_expr (ctx : ctx) (e : Ast.expr) :
untyped Dcalc.Ast.expr Bindlib.box =
Bindlib.box_apply (fun x -> Marked.mark (pos_mark_as e) x)
@@
match Marked.unmark e with
| EVar v -> Bindlib.box_var (Var.Map.find v ctx.local_vars)
| ELit
(( LBool _ | LEmptyError | LInt _ | LRat _ | LMoney _ | LUnit | LDate _
| LDuration _ ) as l) ->
Bindlib.box (ELit l)
| EStruct (struct_name, e_fields) ->
let struct_sig = StructMap.find struct_name ctx.structs in
let d_fields, remaining_e_fields =
List.fold_right
2021-01-14 02:17:24 +03:00
(fun (field_name, _) (d_fields, e_fields) ->
let field_e = StructFieldMap.find field_name e_fields in
let field_d = translate_expr ctx field_e in
field_d :: d_fields, StructFieldMap.remove field_name e_fields)
2020-12-05 19:27:08 +03:00
struct_sig ([], e_fields)
in
if StructFieldMap.cardinal remaining_e_fields > 0 then
Errors.raise_spanned_error (Marked.get_mark e)
"The fields \"%a\" do not belong to the structure %a"
StructName.format_t struct_name
2021-01-05 17:33:30 +03:00
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
(fun fmt (field_name, _) ->
Format.fprintf fmt "%a" StructFieldName.format_t field_name))
(StructFieldMap.bindings remaining_e_fields)
else
Bindlib.box_apply
(fun d_fields -> ETuple (d_fields, Some struct_name))
(Bindlib.box_list d_fields)
| EStructAccess (e1, field_name, struct_name) ->
let struct_sig = StructMap.find struct_name ctx.structs in
let _, field_index =
2022-07-11 12:34:01 +03:00
try
List.assoc field_name (List.mapi (fun i (x, y) -> x, (y, i)) struct_sig)
2020-12-05 19:27:08 +03:00
with Not_found ->
Errors.raise_spanned_error (Marked.get_mark e)
"The field \"%a\" does not belong to the structure %a"
2022-08-16 11:04:01 +03:00
StructFieldName.format_t field_name StructName.format_t struct_name
2022-07-11 12:34:01 +03:00
in
let e1 = translate_expr ctx e1 in
Bindlib.box_apply
(fun e1 ->
ETupleAccess (e1, field_index, Some struct_name, List.map snd struct_sig))
2022-07-11 12:34:01 +03:00
e1
| EEnumInj (e1, constructor, enum_name) ->
let enum_sig = EnumMap.find enum_name ctx.enums in
let _, constructor_index =
2022-07-11 12:34:01 +03:00
try
List.assoc constructor (List.mapi (fun i (x, y) -> x, (y, i)) enum_sig)
with Not_found ->
Errors.raise_spanned_error (Marked.get_mark e)
"The constructor \"%a\" does not belong to the enum %a"
2022-08-16 11:04:01 +03:00
EnumConstructor.format_t constructor EnumName.format_t enum_name
in
let e1 = translate_expr ctx e1 in
Bindlib.box_apply
(fun e1 -> EInj (e1, constructor_index, enum_name, List.map snd enum_sig))
2022-07-11 12:34:01 +03:00
e1
| EMatchS (e1, enum_name, cases) ->
let enum_sig = EnumMap.find enum_name ctx.enums in
2021-01-14 02:17:24 +03:00
let d_cases, remaining_e_cases =
List.fold_right
(fun (constructor, _) (d_cases, e_cases) ->
2021-01-21 01:29:50 +03:00
let case_e =
try EnumConstructorMap.find constructor e_cases
2021-01-21 01:29:50 +03:00
with Not_found ->
Errors.raise_spanned_error (Marked.get_mark e)
"The constructor %a of enum %a is missing from this pattern \
matching"
2022-08-16 11:04:01 +03:00
EnumConstructor.format_t constructor EnumName.format_t enum_name
2022-07-11 12:34:01 +03:00
in
2021-01-21 01:29:50 +03:00
let case_d = translate_expr ctx case_e in
case_d :: d_cases, EnumConstructorMap.remove constructor e_cases)
2021-01-21 01:29:50 +03:00
enum_sig ([], cases)
2022-07-11 12:34:01 +03:00
in
if EnumConstructorMap.cardinal remaining_e_cases > 0 then
2021-01-21 01:29:50 +03:00
Errors.raise_spanned_error (Marked.get_mark e)
"Patter matching is incomplete for enum %a: missing cases %a"
EnumName.format_t enum_name
2021-01-21 01:29:50 +03:00
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
(fun fmt (case_name, _) ->
Format.fprintf fmt "%a" EnumConstructor.format_t case_name))
(EnumConstructorMap.bindings remaining_e_cases)
2021-01-21 01:29:50 +03:00
else
let e1 = translate_expr ctx e1 in
Bindlib.box_apply2
(fun d_fields e1 -> EMatch (e1, d_fields, enum_name))
2021-01-21 01:29:50 +03:00
(Bindlib.box_list d_cases) e1
| EApp (e1, args) ->
(* We insert various log calls to record arguments and outputs of
user-defined functions belonging to scopes *)
let e1_func = translate_expr ctx e1 in
let markings l =
2021-01-21 01:29:50 +03:00
match l with
| ScopelangScopeVar (v, _) ->
[ScopeName.get_info ctx.scope_name; ScopeVar.get_info v]
| SubScopeVar (s, _, (v, _)) ->
[ScopeName.get_info s; ScopeVar.get_info v]
in
2021-01-21 01:29:50 +03:00
let e1_func =
match Marked.unmark e1 with
2022-08-16 11:04:01 +03:00
| ELocation l -> tag_with_log_entry e1_func BeginCall (markings l)
| _ -> e1_func
2022-07-11 12:34:01 +03:00
in
let new_args = List.map (translate_expr ctx) args in
let input_typ, output_typ =
(* NOTE: this is a temporary solution, it works because it's assume that
all function calls are from scope variable. However, this will change
-- for more information see
https://github.com/CatalaLang/catala/pull/280#discussion_r898851693. *)
let retrieve_in_and_out_typ_or_any var vars =
2022-08-25 17:08:08 +03:00
let _, typ, _ = ScopeVarMap.find (Marked.unmark var) vars in
match typ with
| TArrow (marked_input_typ, marked_output_typ) ->
Marked.unmark marked_input_typ, Marked.unmark marked_output_typ
| _ -> TAny, TAny
in
match Marked.unmark e1 with
| ELocation (ScopelangScopeVar var) ->
retrieve_in_and_out_typ_or_any var ctx.scope_vars
| ELocation (SubScopeVar (_, sname, var)) ->
ctx.subscope_vars
|> Ast.SubScopeMap.find (Marked.unmark sname)
|> retrieve_in_and_out_typ_or_any var
| _ -> TAny, TAny
2022-07-11 12:34:01 +03:00
in
2021-01-21 01:29:50 +03:00
let new_args =
match Marked.unmark e1, new_args with
2021-01-21 01:29:50 +03:00
| ELocation l, [new_arg] ->
[
tag_with_log_entry new_arg (VarDef input_typ)
(markings l @ [Marked.same_mark_as "input" e]);
2021-01-21 01:29:50 +03:00
]
| _ -> new_args
in
let new_e =
2021-01-21 23:33:04 +03:00
Bindlib.box_apply2
(fun e' u -> EApp (e', u), pos_mark_as e)
2021-01-21 23:33:04 +03:00
e1_func
(Bindlib.box_list new_args)
2021-01-21 01:29:50 +03:00
in
let new_e =
match Marked.unmark e1 with
2021-01-21 01:29:50 +03:00
| ELocation l ->
2021-01-21 23:33:04 +03:00
tag_with_log_entry
(tag_with_log_entry new_e (VarDef output_typ)
(markings l @ [Marked.same_mark_as "output" e]))
EndCall (markings l)
2021-01-21 01:29:50 +03:00
| _ -> new_e
in
Bindlib.box_apply Marked.unmark new_e
| EAbs (binder, typ) ->
let xs, body = Bindlib.unmbind binder in
let new_xs = Array.map (fun x -> Var.make (Bindlib.name_of x)) xs in
let both_xs = Array.map2 (fun x new_x -> x, new_x) xs new_xs in
let body =
translate_expr
{
ctx with
local_vars =
Array.fold_left
(fun local_vars (x, new_x) -> Var.Map.add x new_x local_vars)
ctx.local_vars both_xs;
2022-07-11 12:34:01 +03:00
}
body
in
let binder = Bindlib.bind_mvar new_xs body in
Bindlib.box_apply (fun b -> EAbs (b, typ)) binder
| EDefault (excepts, just, cons) ->
let excepts = collapse_similar_outcomes excepts in
Bindlib.box_apply3
(fun e j c -> EDefault (e, j, c))
(Bindlib.box_list (List.map (translate_expr ctx) excepts))
(translate_expr ctx just) (translate_expr ctx cons)
| ELocation (ScopelangScopeVar a) ->
2022-08-25 17:08:08 +03:00
let v, _, _ = ScopeVarMap.find (Marked.unmark a) ctx.scope_vars in
Bindlib.box_var v
2020-11-27 13:37:21 +03:00
| ELocation (SubScopeVar (_, s, a)) -> (
2022-07-11 12:34:01 +03:00
try
let v, _, _ =
2022-08-25 17:08:08 +03:00
ScopeVarMap.find (Marked.unmark a)
(Ast.SubScopeMap.find (Marked.unmark s) ctx.subscope_vars)
in
Bindlib.box_var v
2020-11-27 13:37:21 +03:00
with Not_found ->
Errors.raise_multispanned_error
[
Some "Incriminated variable usage:", Marked.get_mark e;
( Some "Incriminated subscope variable declaration:",
Marked.get_mark (ScopeVar.get_info (Marked.unmark a)) );
( Some "Incriminated subscope declaration:",
Marked.get_mark (SubScopeName.get_info (Marked.unmark s)) );
]
"The variable %a.%a cannot be used here, as it is not part subscope \
%a's results. Maybe you forgot to qualify it as an output?"
SubScopeName.format_t (Marked.unmark s) ScopeVar.format_t
(Marked.unmark a) SubScopeName.format_t (Marked.unmark s))
2020-11-24 00:26:26 +03:00
| EIfThenElse (cond, et, ef) ->
Bindlib.box_apply3
(fun c t f -> EIfThenElse (c, t, f))
(translate_expr ctx cond) (translate_expr ctx et) (translate_expr ctx ef)
| EOp op -> Bindlib.box (EOp op)
| ErrorOnEmpty e' ->
2022-08-16 11:04:01 +03:00
Bindlib.box_apply (fun e' -> ErrorOnEmpty e') (translate_expr ctx e')
2020-12-28 01:53:02 +03:00
| EArray es ->
Bindlib.box_apply
(fun es -> EArray es)
(Bindlib.box_list (List.map (translate_expr ctx) es))
2020-11-23 18:12:45 +03:00
(** The result of a rule translation is a list of assignment, with variables and
expressions. We also return the new translation context available after the
assignment to use in later rule translations. The list is actually a
continuation yielding a [Dcalc.scope_body_expr] by giving it what should
come later in the chain of let-bindings. *)
let translate_rule
(ctx : ctx)
(rule : Ast.rule)
2021-12-09 13:58:42 +03:00
((sigma_name, pos_sigma) : Utils.Uid.MarkedString.info) :
(untyped Dcalc.Ast.expr scope_body_expr Bindlib.box ->
untyped Dcalc.Ast.expr scope_body_expr Bindlib.box)
* ctx =
match rule with
| Definition ((ScopelangScopeVar a, var_def_pos), tau, a_io, e) ->
let a_name = ScopeVar.get_info (Marked.unmark a) in
let a_var = Var.make (Marked.unmark a_name) in
let new_e = translate_expr ctx e in
let a_expr = Expr.make_var (a_var, pos_mark var_def_pos) in
2020-12-10 18:58:32 +03:00
let merged_expr =
Bindlib.box_apply
2022-08-16 11:04:01 +03:00
(fun merged_expr -> ErrorOnEmpty merged_expr, pos_mark_as a_name)
(match Marked.unmark a_io.io_input with
2022-02-09 13:37:52 +03:00
| OnlyInput ->
2022-02-09 17:34:13 +03:00
failwith "should not happen"
(* scopelang should not contain any definitions of input only
variables *)
2022-02-09 13:37:52 +03:00
| Reentrant -> merge_defaults a_expr new_e
| NoInput -> new_e)
in
2020-12-11 12:51:46 +03:00
let merged_expr =
2021-04-05 20:06:32 +03:00
tag_with_log_entry merged_expr
(VarDef (Marked.unmark tau))
2021-04-05 20:06:32 +03:00
[sigma_name, pos_sigma; a_name]
2020-12-11 12:51:46 +03:00
in
( (fun next ->
Bindlib.box_apply2
(fun next merged_expr ->
ScopeLet
{
scope_let_next = next;
scope_let_typ = tau;
scope_let_expr = merged_expr;
scope_let_kind = ScopeVarDefinition;
scope_let_pos = Marked.get_mark a;
})
(Bindlib.bind_var a_var next)
merged_expr),
{
ctx with
scope_vars =
2022-08-25 17:08:08 +03:00
ScopeVarMap.add (Marked.unmark a)
(a_var, Marked.unmark tau, a_io)
ctx.scope_vars;
} )
2022-02-10 12:09:58 +03:00
| Definition
( (SubScopeVar (_subs_name, subs_index, subs_var), var_def_pos),
tau,
a_io,
e ) ->
2020-11-26 19:06:32 +03:00
let a_name =
Marked.map_under_mark
2022-02-09 17:56:48 +03:00
(fun str ->
str ^ "." ^ Marked.unmark (ScopeVar.get_info (Marked.unmark subs_var)))
(SubScopeName.get_info (Marked.unmark subs_index))
in
let a_var = Var.make (Marked.unmark a_name) in
let new_e =
tag_with_log_entry (translate_expr ctx e)
(VarDef (Marked.unmark tau))
2022-01-31 20:09:14 +03:00
[sigma_name, pos_sigma; a_name]
in
let silent_var = Var.make "_" in
2022-01-31 20:09:14 +03:00
let thunked_or_nonempty_new_e =
match Marked.unmark a_io.io_input with
| NoInput -> failwith "should not happen"
| OnlyInput ->
Bindlib.box_apply
(fun new_e -> ErrorOnEmpty new_e, pos_mark_as subs_var)
2020-11-27 13:37:21 +03:00
new_e
2021-01-21 23:33:04 +03:00
| Reentrant ->
Expr.make_abs [| silent_var |] new_e
[TLit TUnit, var_def_pos]
(pos_mark var_def_pos)
in
( (fun next ->
Bindlib.box_apply2
(fun next thunked_or_nonempty_new_e ->
ScopeLet
{
scope_let_next = next;
scope_let_pos = Marked.get_mark a_name;
scope_let_typ =
(match Marked.unmark a_io.io_input with
| NoInput -> failwith "should not happen"
| OnlyInput -> tau
| Reentrant ->
2022-08-16 11:04:01 +03:00
TArrow ((TLit TUnit, var_def_pos), tau), var_def_pos);
scope_let_expr = thunked_or_nonempty_new_e;
scope_let_kind = SubScopeVarDefinition;
})
(Bindlib.bind_var a_var next)
thunked_or_nonempty_new_e),
{
ctx with
subscope_vars =
Ast.SubScopeMap.update (Marked.unmark subs_index)
(fun map ->
match map with
| Some map ->
Some
2022-08-25 17:08:08 +03:00
(ScopeVarMap.add (Marked.unmark subs_var)
(a_var, Marked.unmark tau, a_io)
map)
| None ->
Some
2022-08-25 17:08:08 +03:00
(ScopeVarMap.singleton (Marked.unmark subs_var)
(a_var, Marked.unmark tau, a_io)))
ctx.subscope_vars;
} )
| Call (subname, subindex) ->
2022-04-12 11:53:07 +03:00
let subscope_sig = Ast.ScopeMap.find subname ctx.scopes_parameters in
let all_subscope_vars = subscope_sig.scope_sig_local_vars in
let all_subscope_input_vars =
List.filter
(fun var_ctx ->
match Marked.unmark var_ctx.scope_var_io.Ast.io_input with
| NoInput -> false
| _ -> true)
all_subscope_vars
in
let all_subscope_output_vars =
List.filter
(fun var_ctx -> Marked.unmark var_ctx.scope_var_io.Ast.io_output)
all_subscope_vars
in
2022-04-12 11:53:07 +03:00
let scope_dcalc_var = subscope_sig.scope_sig_scope_var in
2022-01-31 20:09:14 +03:00
let called_scope_input_struct = subscope_sig.scope_sig_input_struct in
let called_scope_return_struct = subscope_sig.scope_sig_output_struct in
2022-04-12 11:53:07 +03:00
let subscope_vars_defined =
try Ast.SubScopeMap.find subindex ctx.subscope_vars
2022-08-25 17:08:08 +03:00
with Not_found -> ScopeVarMap.empty
in
2020-11-27 13:37:21 +03:00
let subscope_var_not_yet_defined subvar =
2022-08-25 17:08:08 +03:00
not (ScopeVarMap.mem subvar subscope_vars_defined)
in
let pos_call = Marked.get_mark (SubScopeName.get_info subindex) in
2020-11-27 13:37:21 +03:00
let subscope_args =
List.map
2022-04-12 11:53:07 +03:00
(fun (subvar : scope_var_ctx) ->
2022-01-31 20:09:14 +03:00
if subscope_var_not_yet_defined subvar.scope_var_name then
(* This is a redundant check. Normally, all subscope variables
should have been defined (even an empty definition, if they're
2022-04-12 11:53:07 +03:00
not defined by any rule in the source code) by the translation
from desugared to the scope language. *)
Bindlib.box (Expr.empty_thunked_term (Untyped { pos = pos_call }))
else
let a_var, _, _ =
2022-08-25 17:08:08 +03:00
ScopeVarMap.find subvar.scope_var_name subscope_vars_defined
in
Expr.make_var (a_var, pos_mark pos_call))
all_subscope_input_vars
in
let subscope_struct_arg =
Bindlib.box_apply
(fun subscope_args ->
( ETuple (subscope_args, Some called_scope_input_struct),
pos_mark pos_call ))
(Bindlib.box_list subscope_args)
in
let all_subscope_output_vars_dcalc =
List.map
2022-01-31 20:09:14 +03:00
(fun (subvar : scope_var_ctx) ->
2020-11-27 13:37:21 +03:00
let sub_dcalc_var =
Var.make
(Marked.unmark (SubScopeName.get_info subindex)
^ "."
^ Marked.unmark (ScopeVar.get_info subvar.scope_var_name))
in
2022-01-31 20:09:14 +03:00
subvar, sub_dcalc_var)
all_subscope_output_vars
in
2020-12-11 12:51:46 +03:00
let subscope_func =
2021-01-21 23:33:04 +03:00
tag_with_log_entry
(Expr.make_var
(scope_dcalc_var, pos_mark_as (SubScopeName.get_info subindex)))
BeginCall
[
2021-01-21 23:33:04 +03:00
sigma_name, pos_sigma;
SubScopeName.get_info subindex;
ScopeName.get_info subname;
]
in
2020-11-27 13:37:21 +03:00
let call_expr =
2021-01-21 23:33:04 +03:00
tag_with_log_entry
(Bindlib.box_apply2
(fun e u -> EApp (e, [u]), pos_mark Pos.no_pos)
subscope_func subscope_struct_arg)
EndCall
[
sigma_name, pos_sigma;
SubScopeName.get_info subindex;
ScopeName.get_info subname;
]
in
let result_tuple_var = Var.make "result" in
let result_tuple_typ = TStruct called_scope_return_struct, pos_sigma in
let call_scope_let next =
Bindlib.box_apply2
(fun next call_expr ->
ScopeLet
{
scope_let_next = next;
scope_let_pos = pos_sigma;
scope_let_kind = CallingSubScope;
scope_let_typ = result_tuple_typ;
scope_let_expr = call_expr;
})
(Bindlib.bind_var result_tuple_var next)
call_expr
in
let result_bindings_lets next =
2021-01-28 15:58:59 +03:00
List.fold_right
(fun (var_ctx, v) (next, i) ->
( Bindlib.box_apply2
(fun next r ->
ScopeLet
{
scope_let_next = next;
scope_let_pos = pos_sigma;
scope_let_typ = var_ctx.scope_var_typ, pos_sigma;
2022-08-16 11:04:01 +03:00
scope_let_kind = DestructuringSubScopeResults;
scope_let_expr =
( ETupleAccess
( r,
i,
Some called_scope_return_struct,
List.map
(fun (var_ctx, _) ->
var_ctx.scope_var_typ, pos_sigma)
all_subscope_output_vars_dcalc ),
pos_mark pos_sigma );
})
(Bindlib.bind_var v next)
(Expr.make_var (result_tuple_var, pos_mark pos_sigma)),
i - 1 ))
all_subscope_output_vars_dcalc
(next, List.length all_subscope_output_vars_dcalc - 1)
2021-12-09 20:42:36 +03:00
in
( (fun next -> call_scope_let (fst (result_bindings_lets next))),
{
ctx with
subscope_vars =
Ast.SubScopeMap.add subindex
(List.fold_left
2022-01-31 20:09:14 +03:00
(fun acc (var_ctx, dvar) ->
2022-08-25 17:08:08 +03:00
ScopeVarMap.add var_ctx.scope_var_name
(dvar, var_ctx.scope_var_typ, var_ctx.scope_var_io)
acc)
2022-08-25 17:08:08 +03:00
ScopeVarMap.empty all_subscope_output_vars_dcalc)
ctx.subscope_vars;
} )
| Assertion e ->
let new_e = translate_expr ctx e in
( (fun next ->
Bindlib.box_apply2
(fun next new_e ->
ScopeLet
{
scope_let_next = next;
scope_let_pos = Marked.get_mark e;
2022-08-16 11:04:01 +03:00
scope_let_typ = TLit TUnit, Marked.get_mark e;
scope_let_expr =
(* To ensure that we throw an error if the value is not
defined, we add an check "ErrorOnEmpty" here. *)
Marked.same_mark_as
2022-08-16 11:04:01 +03:00
(EAssert (ErrorOnEmpty new_e, pos_mark_as e))
new_e;
scope_let_kind = Assertion;
})
(Bindlib.bind_var (Var.make "_") next)
new_e),
ctx )
let translate_rules
(ctx : ctx)
(rules : Ast.rule list)
((sigma_name, pos_sigma) : Utils.Uid.MarkedString.info)
(sigma_return_struct_name : StructName.t) :
untyped Dcalc.Ast.expr scope_body_expr Bindlib.box * ctx =
2021-12-09 13:58:42 +03:00
let scope_lets, new_ctx =
List.fold_left
2021-12-09 13:58:42 +03:00
(fun (scope_lets, ctx) rule ->
let new_scope_lets, new_ctx =
translate_rule ctx rule (sigma_name, pos_sigma)
in
(fun next -> scope_lets (new_scope_lets next)), new_ctx)
((fun next -> next), ctx)
rules
in
2022-08-25 17:08:08 +03:00
let scope_variables = ScopeVarMap.bindings new_ctx.scope_vars in
let scope_output_variables =
List.filter
(fun (_, (_, _, io)) -> Marked.unmark io.Ast.io_output)
scope_variables
in
let return_exp =
Bindlib.box_apply
(fun args ->
2022-08-16 11:04:01 +03:00
ETuple (args, Some sigma_return_struct_name), pos_mark pos_sigma)
(Bindlib.box_list
(List.map
(fun (_, (dcalc_var, _, _)) ->
Expr.make_var (dcalc_var, pos_mark pos_sigma))
scope_output_variables))
in
( scope_lets
2022-08-16 11:04:01 +03:00
(Bindlib.box_apply (fun return_exp -> Result return_exp) return_exp),
new_ctx )
let translate_scope_decl
(struct_ctx : struct_ctx)
(enum_ctx : enum_ctx)
2020-12-10 18:58:32 +03:00
(sctx : scope_sigs_ctx)
(scope_name : ScopeName.t)
(sigma : Ast.scope_decl) :
untyped Dcalc.Ast.expr scope_body Bindlib.box * struct_ctx =
let sigma_info = ScopeName.get_info sigma.scope_decl_name in
2022-01-31 20:09:14 +03:00
let scope_sig = Ast.ScopeMap.find sigma.scope_decl_name sctx in
let scope_variables = scope_sig.scope_sig_local_vars in
2022-02-09 17:34:13 +03:00
let ctx =
(* the context must be initialized for fresh variables for all only-input
scope variables *)
List.fold_left
(fun ctx scope_var ->
match Marked.unmark scope_var.scope_var_io.io_input with
2022-02-09 17:34:13 +03:00
| OnlyInput ->
let scope_var_name = ScopeVar.get_info scope_var.scope_var_name in
let scope_var_dcalc = Var.make (Marked.unmark scope_var_name) in
2022-02-09 17:34:13 +03:00
{
ctx with
scope_vars =
2022-08-25 17:08:08 +03:00
ScopeVarMap.add scope_var.scope_var_name
2022-02-09 17:34:13 +03:00
( scope_var_dcalc,
scope_var.scope_var_typ,
scope_var.scope_var_io )
ctx.scope_vars;
}
| _ -> ctx)
(empty_ctx struct_ctx enum_ctx sctx scope_name)
scope_variables
in
2022-01-31 20:09:14 +03:00
let scope_input_var = scope_sig.scope_sig_input_var in
let scope_input_struct_name = scope_sig.scope_sig_input_struct in
let scope_return_struct_name = scope_sig.scope_sig_output_struct in
let pos_sigma = Marked.get_mark sigma_info in
let rules_with_return_expr, ctx =
2021-12-09 13:58:42 +03:00
translate_rules ctx sigma.scope_decl_rules sigma_info
scope_return_struct_name
in
2020-11-27 13:37:21 +03:00
let scope_variables =
List.map
2022-01-31 20:09:14 +03:00
(fun var_ctx ->
let dcalc_x, _, _ =
2022-08-25 17:08:08 +03:00
ScopeVarMap.find var_ctx.scope_var_name ctx.scope_vars
in
2022-01-31 20:09:14 +03:00
var_ctx, dcalc_x)
2020-11-27 13:37:21 +03:00
scope_variables
in
(* first we create variables from the fields of the input struct *)
let scope_input_variables =
List.filter
(fun (var_ctx, _) ->
match Marked.unmark var_ctx.scope_var_io.io_input with
| NoInput -> false
| _ -> true)
scope_variables
in
let scope_output_variables =
List.filter
(fun (var_ctx, _) -> Marked.unmark var_ctx.scope_var_io.io_output)
scope_variables
in
2022-02-09 17:34:13 +03:00
let input_var_typ (var_ctx : scope_var_ctx) =
match Marked.unmark var_ctx.scope_var_io.io_input with
2022-02-09 17:34:13 +03:00
| OnlyInput -> var_ctx.scope_var_typ, pos_sigma
| Reentrant ->
2022-08-16 11:04:01 +03:00
( TArrow ((TLit TUnit, pos_sigma), (var_ctx.scope_var_typ, pos_sigma)),
2022-02-09 17:34:13 +03:00
pos_sigma )
| NoInput -> failwith "should not happen"
in
let input_destructurings next =
fst
(List.fold_right
(fun (var_ctx, v) (next, i) ->
( Bindlib.box_apply2
(fun next r ->
ScopeLet
{
2022-08-16 11:04:01 +03:00
scope_let_kind = DestructuringInputStruct;
scope_let_next = next;
scope_let_pos = pos_sigma;
scope_let_typ = input_var_typ var_ctx;
scope_let_expr =
( ETupleAccess
( r,
i,
Some scope_input_struct_name,
List.map
(fun (var_ctx, _) -> input_var_typ var_ctx)
scope_input_variables ),
pos_mark pos_sigma );
})
(Bindlib.bind_var v next)
(Expr.make_var (scope_input_var, pos_mark pos_sigma)),
i - 1 ))
scope_input_variables
(next, List.length scope_input_variables - 1))
in
let scope_return_struct_fields =
List.map
2022-01-31 20:09:14 +03:00
(fun (var_ctx, dvar) ->
let struct_field_name =
StructFieldName.fresh (Bindlib.name_of dvar ^ "_out", pos_sigma)
in
2022-01-31 20:09:14 +03:00
struct_field_name, (var_ctx.scope_var_typ, pos_sigma))
scope_output_variables
in
let scope_input_struct_fields =
List.map
2022-01-31 20:09:14 +03:00
(fun (var_ctx, dvar) ->
let struct_field_name =
StructFieldName.fresh (Bindlib.name_of dvar ^ "_in", pos_sigma)
in
2022-02-09 17:34:13 +03:00
struct_field_name, input_var_typ var_ctx)
scope_input_variables
in
let new_struct_ctx =
StructMap.add scope_input_struct_name scope_input_struct_fields
2022-08-16 11:04:01 +03:00
(StructMap.singleton scope_return_struct_name scope_return_struct_fields)
in
( Bindlib.box_apply
(fun scope_body_expr ->
{
scope_body_expr;
scope_body_input_struct = scope_input_struct_name;
scope_body_output_struct = scope_return_struct_name;
})
(Bindlib.bind_var scope_input_var
(input_destructurings rules_with_return_expr)),
new_struct_ctx )
2020-11-27 13:37:21 +03:00
let translate_program (prgm : Ast.program) :
untyped Dcalc.Ast.program * Dependency.TVertex.t list =
2020-11-25 20:00:34 +03:00
let scope_dependencies = Dependency.build_program_dep_graph prgm in
2020-12-06 14:32:36 +03:00
Dependency.check_for_cycle_in_scope scope_dependencies;
let types_ordering =
Dependency.check_type_cycles prgm.program_ctx.ctx_structs
prgm.program_ctx.ctx_enums
in
2020-11-25 20:00:34 +03:00
let scope_ordering = Dependency.get_scope_ordering scope_dependencies in
let decl_ctx = prgm.program_ctx in
2020-11-27 13:37:21 +03:00
let sctx : scope_sigs_ctx =
2020-12-10 18:58:32 +03:00
Ast.ScopeMap.mapi
(fun scope_name scope ->
2020-11-27 13:37:21 +03:00
let scope_dvar =
Var.make
(Marked.unmark (ScopeName.get_info scope.Ast.scope_decl_name))
2020-11-27 13:37:21 +03:00
in
let scope_return_struct_name =
StructName.fresh
(Marked.map_under_mark
(fun s -> s ^ "_out")
(ScopeName.get_info scope_name))
in
let scope_input_var =
Var.make (Marked.unmark (ScopeName.get_info scope_name) ^ "_in")
in
let scope_input_struct_name =
StructName.fresh
(Marked.map_under_mark
(fun s -> s ^ "_in")
(ScopeName.get_info scope_name))
in
2022-01-31 20:09:14 +03:00
{
scope_sig_local_vars =
List.map
(fun (scope_var, (tau, vis)) ->
{
scope_var_name = scope_var;
scope_var_typ = Marked.unmark tau;
scope_var_io = vis;
})
2022-08-25 17:08:08 +03:00
(ScopeVarMap.bindings scope.scope_sig);
2022-01-31 20:09:14 +03:00
scope_sig_scope_var = scope_dvar;
scope_sig_input_var = scope_input_var;
scope_sig_input_struct = scope_input_struct_name;
scope_sig_output_struct = scope_return_struct_name;
})
prgm.program_scopes
2020-11-27 13:37:21 +03:00
in
2020-11-25 20:00:34 +03:00
(* the resulting expression is the list of definitions of all the scopes,
ending with the top-level scope. *)
let (scopes, decl_ctx) : untyped Dcalc.Ast.expr scopes Bindlib.box * _ =
2021-01-28 15:58:59 +03:00
List.fold_right
2022-04-04 09:56:48 +03:00
(fun scope_name (scopes, decl_ctx) ->
2021-01-28 15:58:59 +03:00
let scope = Ast.ScopeMap.find scope_name prgm.program_scopes in
let scope_body, scope_out_struct =
translate_scope_decl decl_ctx.ctx_structs decl_ctx.ctx_enums sctx
scope_name scope
in
2022-01-31 20:09:14 +03:00
let dvar = (Ast.ScopeMap.find scope_name sctx).scope_sig_scope_var in
let decl_ctx =
{
decl_ctx with
ctx_structs =
StructMap.union
(fun _ _ -> assert false (* should not happen *))
decl_ctx.ctx_structs scope_out_struct;
}
in
2022-04-04 09:56:48 +03:00
let scope_next = Bindlib.bind_var dvar scopes in
let new_scopes =
Bindlib.box_apply2
(fun scope_body scope_next ->
ScopeDef { scope_name; scope_body; scope_next })
2022-04-04 09:56:48 +03:00
scope_body scope_next
in
new_scopes, decl_ctx)
scope_ordering
(Bindlib.box Nil, decl_ctx)
2021-01-28 15:58:59 +03:00
in
2022-04-04 09:56:48 +03:00
{ scopes = Bindlib.unbox scopes; decl_ctx }, types_ordering