2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2021 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
2021-06-22 17:01:57 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2021-06-22 17:01:57 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2021-06-22 17:01:57 +03:00
|
|
|
the License. *)
|
|
|
|
|
|
|
|
open Utils
|
|
|
|
module A = Ast
|
|
|
|
module L = Lcalc.Ast
|
|
|
|
module D = Dcalc.Ast
|
|
|
|
|
|
|
|
type ctxt = {
|
2021-06-24 15:52:51 +03:00
|
|
|
func_dict : A.TopLevelName.t L.VarMap.t;
|
2021-06-23 18:47:34 +03:00
|
|
|
decl_ctx : D.decl_ctx;
|
2021-06-24 15:52:51 +03:00
|
|
|
var_dict : A.LocalName.t L.VarMap.t;
|
|
|
|
inside_definition_of : A.LocalName.t option;
|
2022-03-21 16:58:54 +03:00
|
|
|
context_name : string;
|
2021-06-22 17:01:57 +03:00
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(* Expressions can spill out side effect, hence this function also returns a
|
|
|
|
list of statements to be prepended before the expression is evaluated *)
|
|
|
|
let rec translate_expr (ctxt : ctxt) (expr : L.expr Pos.marked) :
|
|
|
|
A.block * A.expr Pos.marked =
|
2021-06-23 18:47:34 +03:00
|
|
|
match Pos.unmark expr with
|
|
|
|
| L.EVar v ->
|
|
|
|
let local_var =
|
|
|
|
try A.EVar (L.VarMap.find (Pos.unmark v) ctxt.var_dict)
|
2022-03-08 17:03:14 +03:00
|
|
|
with Not_found ->
|
|
|
|
A.EFunc (L.VarMap.find (Pos.unmark v) ctxt.func_dict)
|
2021-06-23 18:47:34 +03:00
|
|
|
in
|
|
|
|
([], (local_var, Pos.get_position v))
|
2021-06-24 15:52:51 +03:00
|
|
|
| L.ETuple (args, Some s_name) ->
|
|
|
|
let args_stmts, new_args =
|
|
|
|
List.fold_left
|
|
|
|
(fun (args_stmts, new_args) arg ->
|
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
|
|
|
(arg_stmts @ args_stmts, new_arg :: new_args))
|
|
|
|
([], []) args
|
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
|
|
|
let args_stmts = List.rev args_stmts in
|
|
|
|
(args_stmts, (A.EStruct (new_args, s_name), Pos.get_position expr))
|
2022-03-08 17:03:14 +03:00
|
|
|
| L.ETuple (_, None) ->
|
|
|
|
failwith "Non-struct tuples cannot be compiled to scalc"
|
2021-06-23 18:47:34 +03:00
|
|
|
| L.ETupleAccess (e1, num_field, Some s_name, _) ->
|
|
|
|
let e1_stmts, new_e1 = translate_expr ctxt e1 in
|
|
|
|
let field_name =
|
2022-03-08 17:03:14 +03:00
|
|
|
fst
|
|
|
|
(List.nth
|
|
|
|
(D.StructMap.find s_name ctxt.decl_ctx.ctx_structs)
|
|
|
|
num_field)
|
2021-06-23 18:47:34 +03:00
|
|
|
in
|
2022-03-08 17:03:14 +03:00
|
|
|
( e1_stmts,
|
|
|
|
( A.EStructFieldAccess (new_e1, field_name, s_name),
|
|
|
|
Pos.get_position expr ) )
|
|
|
|
| L.ETupleAccess (_, _, None, _) ->
|
|
|
|
failwith "Non-struct tuples cannot be compiled to scalc"
|
2021-06-24 15:52:51 +03:00
|
|
|
| L.EInj (e1, num_cons, e_name, _) ->
|
|
|
|
let e1_stmts, new_e1 = translate_expr ctxt e1 in
|
2022-03-08 17:03:14 +03:00
|
|
|
let cons_name =
|
|
|
|
fst (List.nth (D.EnumMap.find e_name ctxt.decl_ctx.ctx_enums) num_cons)
|
|
|
|
in
|
2021-06-24 15:52:51 +03:00
|
|
|
(e1_stmts, (A.EInj (new_e1, cons_name, e_name), Pos.get_position expr))
|
2021-06-23 18:47:34 +03:00
|
|
|
| L.EApp (f, args) ->
|
|
|
|
let f_stmts, new_f = translate_expr ctxt f in
|
|
|
|
let args_stmts, new_args =
|
|
|
|
List.fold_left
|
|
|
|
(fun (args_stmts, new_args) arg ->
|
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
|
|
|
(arg_stmts @ args_stmts, new_arg :: new_args))
|
|
|
|
([], []) args
|
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
|
|
|
(f_stmts @ args_stmts, (A.EApp (new_f, new_args), Pos.get_position expr))
|
|
|
|
| L.EArray args ->
|
|
|
|
let args_stmts, new_args =
|
|
|
|
List.fold_left
|
|
|
|
(fun (args_stmts, new_args) arg ->
|
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
|
|
|
(arg_stmts @ args_stmts, new_arg :: new_args))
|
|
|
|
([], []) args
|
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
|
|
|
(args_stmts, (A.EArray new_args, Pos.get_position expr))
|
|
|
|
| L.EOp op -> ([], (A.EOp op, Pos.get_position expr))
|
|
|
|
| L.ELit l -> ([], (A.ELit l, Pos.get_position expr))
|
|
|
|
| _ ->
|
2022-03-21 16:58:54 +03:00
|
|
|
let tmp_var =
|
|
|
|
A.LocalName.fresh
|
|
|
|
( (*This piece of logic is used to make the code more readable *)
|
|
|
|
(match ctxt.inside_definition_of with
|
|
|
|
| None -> ctxt.context_name
|
|
|
|
| Some v ->
|
|
|
|
let v = Pos.unmark (A.LocalName.get_info v) in
|
|
|
|
let tmp_rex = Re.Pcre.regexp "^temp_" in
|
|
|
|
if Re.Pcre.pmatch ~rex:tmp_rex v then v else "temp_" ^ v),
|
|
|
|
Pos.get_position expr )
|
|
|
|
in
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
inside_definition_of = Some tmp_var;
|
|
|
|
context_name = Pos.unmark (A.LocalName.get_info tmp_var);
|
|
|
|
}
|
|
|
|
in
|
2021-06-23 18:47:34 +03:00
|
|
|
let tmp_stmts = translate_statements ctxt expr in
|
2022-03-08 17:03:14 +03:00
|
|
|
( ( A.SLocalDecl
|
|
|
|
((tmp_var, Pos.get_position expr), (D.TAny, Pos.get_position expr)),
|
2021-06-23 18:47:34 +03:00
|
|
|
Pos.get_position expr )
|
|
|
|
:: tmp_stmts,
|
|
|
|
(A.EVar tmp_var, Pos.get_position expr) )
|
2021-06-22 19:16:47 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
and translate_statements (ctxt : ctxt) (block_expr : L.expr Pos.marked) :
|
|
|
|
A.block =
|
2021-06-22 19:16:47 +03:00
|
|
|
match Pos.unmark block_expr with
|
2022-03-08 17:03:14 +03:00
|
|
|
| L.EApp
|
|
|
|
((L.EAbs ((binder, _), [ (D.TLit D.TUnit, _) ]), _), [ (L.EAssert e, _) ])
|
|
|
|
->
|
2021-06-23 18:47:34 +03:00
|
|
|
(* Assertions are always encapsulated in a unit-typed let binding *)
|
|
|
|
let _, body = Bindlib.unmbind binder in
|
|
|
|
let e_stmts, new_e = translate_expr ctxt e in
|
|
|
|
e_stmts
|
|
|
|
@ (A.SAssert (Pos.unmark new_e), Pos.get_position block_expr)
|
|
|
|
:: translate_statements ctxt body
|
2021-06-22 19:16:47 +03:00
|
|
|
| L.EApp ((L.EAbs ((binder, binder_pos), taus), eabs_pos), args) ->
|
|
|
|
(* This defines multiple local variables at the time *)
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
2022-03-08 17:03:14 +03:00
|
|
|
let vars_tau =
|
|
|
|
List.map2 (fun x tau -> (x, tau)) (Array.to_list vars) taus
|
|
|
|
in
|
2021-06-22 19:16:47 +03:00
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
var_dict =
|
|
|
|
List.fold_left
|
|
|
|
(fun var_dict (x, _) ->
|
2022-03-08 17:03:14 +03:00
|
|
|
L.VarMap.add x
|
|
|
|
(A.LocalName.fresh (Bindlib.name_of x, binder_pos))
|
|
|
|
var_dict)
|
2021-06-22 19:16:47 +03:00
|
|
|
ctxt.var_dict vars_tau;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
let local_decls =
|
|
|
|
List.map
|
|
|
|
(fun (x, tau) ->
|
2022-03-08 17:03:14 +03:00
|
|
|
( A.SLocalDecl ((L.VarMap.find x ctxt.var_dict, binder_pos), tau),
|
|
|
|
eabs_pos ))
|
2021-06-22 19:16:47 +03:00
|
|
|
vars_tau
|
|
|
|
in
|
|
|
|
let vars_args =
|
|
|
|
List.map2
|
2022-03-08 17:03:14 +03:00
|
|
|
(fun (x, tau) arg ->
|
|
|
|
((L.VarMap.find x ctxt.var_dict, binder_pos), tau, arg))
|
2021-06-22 19:16:47 +03:00
|
|
|
vars_tau args
|
|
|
|
in
|
|
|
|
let def_blocks =
|
|
|
|
List.map
|
2021-06-23 18:47:34 +03:00
|
|
|
(fun (x, _tau, arg) ->
|
2022-03-08 17:03:14 +03:00
|
|
|
let ctxt =
|
2022-03-21 16:58:54 +03:00
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
inside_definition_of = Some (Pos.unmark x);
|
|
|
|
context_name = Pos.unmark (A.LocalName.get_info (Pos.unmark x));
|
|
|
|
}
|
2022-03-08 17:03:14 +03:00
|
|
|
in
|
2021-06-23 18:47:34 +03:00
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
|
|
|
arg_stmts @ [ (A.SLocalDef (x, new_arg), binder_pos) ])
|
2021-06-22 19:16:47 +03:00
|
|
|
vars_args
|
|
|
|
in
|
2021-06-23 18:47:34 +03:00
|
|
|
let rest_of_block = translate_statements ctxt body in
|
|
|
|
local_decls @ List.flatten def_blocks @ rest_of_block
|
2021-06-24 15:52:51 +03:00
|
|
|
| L.EAbs ((binder, binder_pos), taus) ->
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
2022-03-08 17:03:14 +03:00
|
|
|
let vars_tau =
|
|
|
|
List.map2 (fun x tau -> (x, tau)) (Array.to_list vars) taus
|
|
|
|
in
|
2021-06-24 18:50:08 +03:00
|
|
|
let closure_name =
|
|
|
|
match ctxt.inside_definition_of with
|
2022-03-21 16:58:54 +03:00
|
|
|
| None ->
|
|
|
|
A.LocalName.fresh (ctxt.context_name, Pos.get_position block_expr)
|
2021-06-24 18:50:08 +03:00
|
|
|
| Some x -> x
|
|
|
|
in
|
2021-06-24 15:52:51 +03:00
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
var_dict =
|
|
|
|
List.fold_left
|
|
|
|
(fun var_dict (x, _) ->
|
2022-03-08 17:03:14 +03:00
|
|
|
L.VarMap.add x
|
|
|
|
(A.LocalName.fresh (Bindlib.name_of x, binder_pos))
|
|
|
|
var_dict)
|
2021-06-24 15:52:51 +03:00
|
|
|
ctxt.var_dict vars_tau;
|
2021-06-24 18:50:08 +03:00
|
|
|
inside_definition_of = None;
|
2021-06-24 15:52:51 +03:00
|
|
|
}
|
|
|
|
in
|
|
|
|
let new_body = translate_statements ctxt body in
|
|
|
|
[
|
|
|
|
( A.SInnerFuncDef
|
|
|
|
( (closure_name, binder_pos),
|
|
|
|
{
|
|
|
|
func_params =
|
|
|
|
List.map
|
2022-03-08 17:03:14 +03:00
|
|
|
(fun (var, tau) ->
|
|
|
|
((L.VarMap.find var ctxt.var_dict, binder_pos), tau))
|
2021-06-24 15:52:51 +03:00
|
|
|
vars_tau;
|
|
|
|
func_body = new_body;
|
|
|
|
} ),
|
|
|
|
binder_pos );
|
|
|
|
]
|
|
|
|
| L.EMatch (e1, args, e_name) ->
|
|
|
|
let e1_stmts, new_e1 = translate_expr ctxt e1 in
|
|
|
|
let new_args =
|
|
|
|
List.fold_left
|
|
|
|
(fun new_args arg ->
|
|
|
|
match Pos.unmark arg with
|
|
|
|
| L.EAbs ((binder, pos_binder), _) ->
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
|
|
|
assert (Array.length vars = 1);
|
|
|
|
let var = vars.(0) in
|
2022-03-08 17:03:14 +03:00
|
|
|
let scalc_var =
|
|
|
|
A.LocalName.fresh (Bindlib.name_of var, pos_binder)
|
|
|
|
in
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
var_dict = L.VarMap.add var scalc_var ctxt.var_dict;
|
|
|
|
}
|
|
|
|
in
|
2021-06-24 15:52:51 +03:00
|
|
|
let new_arg = translate_statements ctxt body in
|
|
|
|
(new_arg, scalc_var) :: new_args
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *))
|
|
|
|
[] args
|
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
2022-03-08 17:03:14 +03:00
|
|
|
e1_stmts
|
|
|
|
@ [ (A.SSwitch (new_e1, e_name, new_args), Pos.get_position block_expr) ]
|
2021-06-23 18:47:34 +03:00
|
|
|
| L.EIfThenElse (cond, e_true, e_false) ->
|
|
|
|
let cond_stmts, s_cond = translate_expr ctxt cond in
|
|
|
|
let s_e_true = translate_statements ctxt e_true in
|
|
|
|
let s_e_false = translate_statements ctxt e_false in
|
2022-03-08 17:03:14 +03:00
|
|
|
cond_stmts
|
|
|
|
@ [
|
|
|
|
( A.SIfThenElse (s_cond, s_e_true, s_e_false),
|
|
|
|
Pos.get_position block_expr );
|
|
|
|
]
|
2021-06-23 18:47:34 +03:00
|
|
|
| L.ECatch (e_try, except, e_catch) ->
|
|
|
|
let s_e_try = translate_statements ctxt e_try in
|
|
|
|
let s_e_catch = translate_statements ctxt e_catch in
|
2022-03-08 17:03:14 +03:00
|
|
|
[
|
|
|
|
(A.STryExcept (s_e_try, except, s_e_catch), Pos.get_position block_expr);
|
|
|
|
]
|
2021-06-23 18:47:34 +03:00
|
|
|
| L.ERaise except -> [ (A.SRaise except, Pos.get_position block_expr) ]
|
2022-02-01 17:41:53 +03:00
|
|
|
| _ -> (
|
2021-06-23 18:47:34 +03:00
|
|
|
let e_stmts, new_e = translate_expr ctxt block_expr in
|
2021-06-24 15:52:51 +03:00
|
|
|
e_stmts
|
2022-02-01 17:41:53 +03:00
|
|
|
@
|
|
|
|
match e_stmts with
|
|
|
|
| (A.SRaise _, _) :: _ ->
|
2022-03-08 17:03:14 +03:00
|
|
|
(* if the last statement raises an exception, then we don't need to
|
|
|
|
return or to define the current variable since this code will be
|
|
|
|
unreachable *)
|
2022-02-01 17:41:53 +03:00
|
|
|
[]
|
|
|
|
| _ ->
|
|
|
|
[
|
|
|
|
( (match ctxt.inside_definition_of with
|
|
|
|
| None -> A.SReturn (Pos.unmark new_e)
|
|
|
|
| Some x -> A.SLocalDef (Pos.same_pos_as x new_e, new_e)),
|
|
|
|
Pos.get_position block_expr );
|
|
|
|
])
|
2021-06-22 17:01:57 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let translate_scope
|
2022-03-21 16:58:54 +03:00
|
|
|
(scope_name : D.ScopeName.t)
|
2022-03-08 17:03:14 +03:00
|
|
|
(decl_ctx : D.decl_ctx)
|
|
|
|
(func_dict : A.TopLevelName.t L.VarMap.t)
|
|
|
|
(scope_expr : L.expr Pos.marked) :
|
|
|
|
(A.LocalName.t Pos.marked * D.typ Pos.marked) list * A.block =
|
2021-06-22 17:01:57 +03:00
|
|
|
match Pos.unmark scope_expr with
|
|
|
|
| L.EAbs ((binder, binder_pos), typs) ->
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
|
|
|
let var_dict =
|
|
|
|
Array.fold_left
|
|
|
|
(fun var_dict var ->
|
2022-03-08 17:03:14 +03:00
|
|
|
L.VarMap.add var
|
|
|
|
(A.LocalName.fresh (Bindlib.name_of var, binder_pos))
|
|
|
|
var_dict)
|
2021-06-22 17:01:57 +03:00
|
|
|
L.VarMap.empty vars
|
|
|
|
in
|
|
|
|
let param_list =
|
|
|
|
List.map2
|
|
|
|
(fun var typ -> ((L.VarMap.find var var_dict, binder_pos), typ))
|
|
|
|
(Array.to_list vars) typs
|
|
|
|
in
|
2021-06-23 18:47:34 +03:00
|
|
|
let new_body =
|
2022-03-08 17:03:14 +03:00
|
|
|
translate_statements
|
2022-03-21 16:58:54 +03:00
|
|
|
{
|
|
|
|
decl_ctx;
|
|
|
|
func_dict;
|
|
|
|
var_dict;
|
|
|
|
inside_definition_of = None;
|
|
|
|
context_name = Pos.unmark (D.ScopeName.get_info scope_name);
|
|
|
|
}
|
2022-03-08 17:03:14 +03:00
|
|
|
body
|
2021-06-23 18:47:34 +03:00
|
|
|
in
|
2021-06-22 17:01:57 +03:00
|
|
|
(param_list, new_body)
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *)
|
|
|
|
|
|
|
|
let translate_program (p : L.program) : A.program =
|
|
|
|
{
|
|
|
|
decl_ctx = p.L.decl_ctx;
|
|
|
|
scopes =
|
|
|
|
(let _, new_scopes =
|
|
|
|
List.fold_left
|
2022-02-14 19:01:34 +03:00
|
|
|
(fun (func_dict, new_scopes) body ->
|
2021-06-23 18:47:34 +03:00
|
|
|
let new_scope_params, new_scope_body =
|
2022-03-21 16:58:54 +03:00
|
|
|
translate_scope body.L.scope_body_name p.decl_ctx func_dict
|
|
|
|
body.L.scope_body_expr
|
2021-06-23 18:47:34 +03:00
|
|
|
in
|
2022-02-14 19:01:34 +03:00
|
|
|
let func_id =
|
2022-03-08 17:03:14 +03:00
|
|
|
A.TopLevelName.fresh
|
|
|
|
(Bindlib.name_of body.Lcalc.Ast.scope_body_var, Pos.no_pos)
|
|
|
|
in
|
|
|
|
let func_dict =
|
|
|
|
L.VarMap.add body.Lcalc.Ast.scope_body_var func_id func_dict
|
2022-02-14 19:01:34 +03:00
|
|
|
in
|
2021-06-24 15:52:51 +03:00
|
|
|
( func_dict,
|
2022-02-14 19:01:34 +03:00
|
|
|
{
|
|
|
|
Ast.scope_body_name = body.Lcalc.Ast.scope_body_name;
|
|
|
|
Ast.scope_body_var = func_id;
|
|
|
|
scope_body_func =
|
2022-03-08 17:03:14 +03:00
|
|
|
{
|
|
|
|
A.func_params = new_scope_params;
|
|
|
|
A.func_body = new_scope_body;
|
|
|
|
};
|
2022-02-14 19:01:34 +03:00
|
|
|
}
|
2021-06-24 15:52:51 +03:00
|
|
|
:: new_scopes ))
|
2022-02-25 14:25:42 +03:00
|
|
|
( (if !Cli.avoid_exceptions_flag then
|
|
|
|
L.VarMap.singleton L.handle_default_opt
|
|
|
|
(A.TopLevelName.fresh ("handle_default_opt", Pos.no_pos))
|
|
|
|
else
|
|
|
|
L.VarMap.singleton L.handle_default
|
|
|
|
(A.TopLevelName.fresh ("handle_default", Pos.no_pos))),
|
2021-06-23 18:47:34 +03:00
|
|
|
[] )
|
|
|
|
p.L.scopes
|
2021-06-22 17:01:57 +03:00
|
|
|
in
|
|
|
|
List.rev new_scopes);
|
|
|
|
}
|