123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132(* This file is part of the Catala compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2020-2022 Inria, contributor: Alain Delaët-Tixeuil
<alain.delaet--tixeuil@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. *)openUtilsmoduleD=Asttypescope_lets=|ResultofD.exprPos.marked|ScopeLetof{scope_let_kind:D.scope_let_kind;scope_let_typ:D.typPos.marked;scope_let_expr:D.exprPos.marked;scope_let_next:(D.expr,scope_lets)Bindlib.binder;scope_let_pos:Pos.t;}typescope_body={scope_body_input_struct:D.StructName.t;scope_body_output_struct:D.StructName.t;scope_body_result:(D.expr,scope_lets)Bindlib.binder;}typescopes=|Nil|ScopeDefof{scope_name:D.ScopeName.t;scope_body:scope_body;scope_next:(D.expr,scopes)Bindlib.binder;}letunion:unitD.VarMap.t->unitD.VarMap.t->unitD.VarMap.t=D.VarMap.union(fun___->Some())letrecfree_vars_set_scope_lets(scope_lets:scope_lets):unitD.VarMap.t=matchscope_letswith|Resulte->D.free_vars_sete|ScopeLet{scope_let_expr=e;scope_let_next=next;_}->letv,body=Bindlib.unbindnextinunion(D.free_vars_sete)(D.VarMap.removev(free_vars_set_scope_letsbody))letfree_vars_set_scope_body(scope_body:scope_body):unitD.VarMap.t=let{scope_body_result=binder;_}=scope_bodyinletv,body=Bindlib.unbindbinderinD.VarMap.removev(free_vars_set_scope_letsbody)letrecfree_vars_set_scopes(scopes:scopes):unitD.VarMap.t=matchscopeswith|Nil->D.VarMap.empty|ScopeDef{scope_body=body;scope_next=next;_}->letv,next=Bindlib.unbindnextinunion(D.VarMap.removev(free_vars_set_scopesnext))(free_vars_set_scope_bodybody)letfree_vars_list_scope_lets(scope_lets:scope_lets):D.Var.tlist=free_vars_set_scope_letsscope_lets|>D.VarMap.bindings|>List.mapfstletfree_vars_list_scope_body(scope_body:scope_body):D.Var.tlist=free_vars_set_scope_bodyscope_body|>D.VarMap.bindings|>List.mapfstletfree_vars_list_scopes(scopes:scopes):D.Var.tlist=free_vars_set_scopesscopes|>D.VarMap.bindings|>List.mapfst(** Actual transformation for scopes. *)letbind_scope_lets(acc:scope_letsBindlib.box)(scope_let:D.scope_let):scope_letsBindlib.box=letpos=sndscope_let.D.scope_let_varin(* Cli.debug_print @@ Format.asprintf "binding let %a. Variable occurs = %b" Print.format_var (fst
scope_let.D.scope_let_var) (Bindlib.occur (fst scope_let.D.scope_let_var) acc); *)letbinder=Bindlib.bind_var(fstscope_let.D.scope_let_var)accinBindlib.box_apply2(funexprbinder->(* Cli.debug_print @@ Format.asprintf "free variables in expression: %a" (Format.pp_print_list
Print.format_var) (D.free_vars_list expr); *)ScopeLet{scope_let_kind=scope_let.D.scope_let_kind;scope_let_typ=scope_let.D.scope_let_typ;scope_let_expr=expr;scope_let_next=binder;scope_let_pos=pos;})scope_let.D.scope_let_exprbinderletbind_scope_body(body:D.scope_body):scope_bodyBindlib.box=(* it is a fold_right and not a fold_left. *)letbody_result=ListLabels.fold_rightbody.D.scope_body_lets~init:(Bindlib.box_apply(fune->Resulte)body.D.scope_body_result)~f:(Fun.flipbind_scope_lets)in(* Cli.debug_print @@ Format.asprintf "binding arg %a" Print.format_var body.D.scope_body_arg; *)letscope_body_result=Bindlib.bind_varbody.D.scope_body_argbody_resultin(* Cli.debug_print @@ Format.asprintf "isfinal term is closed: %b" (Bindlib.is_closed
scope_body_result); *)Bindlib.box_apply(funscope_body_result->(* Cli.debug_print @@ Format.asprintf "rank of the final term: %i" (Bindlib.binder_rank
scope_body_result); *){scope_body_output_struct=body.D.scope_body_output_struct;scope_body_input_struct=body.D.scope_body_input_struct;scope_body_result;})scope_body_resultletbind_scope((scope_name,scope_var,scope_body):D.ScopeName.t*D.exprBindlib.var*D.scope_body)(acc:scopesBindlib.box):scopesBindlib.box=Bindlib.box_apply2(funscope_bodyscope_next->ScopeDef{scope_name;scope_body;scope_next})(bind_scope_bodyscope_body)(Bindlib.bind_varscope_varacc)letbind_scopes(scopes:(D.ScopeName.t*D.exprBindlib.var*D.scope_body)list):scopesBindlib.box=letresult=ListLabels.fold_rightscopes~init:(Bindlib.boxNil)~f:bind_scopein(* Cli.debug_print @@ Format.asprintf "free variable in the program : [%a]" (Format.pp_print_list
Print.format_var) (free_vars_list_scopes (Bindlib.unbox result)); *)result