123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476open!ImportmoduleDefault=structmoduleLocated=structtype'at='aLoc.tletloc(x:_t)=x.locletmk~locx={loc;txt=x}letmapft={twithtxt=ft.txt}letmap_lidentx=map(funx->Longident.Lidentx)xletlident~locx=mk~loc(Longident.parsex)endincludeAst_builder_generated.MmoduleLatest=structletppat_construct=ppat_constructletpexp_function=pexp_functionletvalue_binding?constraint_~loc~pat~expr()=value_binding~constraint_~loc~pat~exprletconstructor_declaration~loc~name~vars~args~res()=constructor_declaration~loc~name~vars~args~resend(*------ stable layer above Ast_builder_generated.M -----*)letppat_construct~loclidp={ppat_loc_stack=[];ppat_attributes=[];ppat_loc=loc;ppat_desc=Ppat_construct(lid,Option.mapp~f:(funp->([],p)));}letpexp_function_cases~loccases={pexp_loc_stack=[];pexp_attributes=[];pexp_loc=loc;pexp_desc=Pexp_function([],None,Pfunction_cases(cases,loc,[]));}letadd_fun_paramsreturn_constraint~locparamsbody=matchparamswith|[]->body|_->(matchbody.pexp_descwith|Pexp_function(more_params,constraint_,func_body)->pexp_function~loc(params@more_params)constraint_func_body|_->assert(matchparamswith[]->false|_->true);pexp_function~locparamsreturn_constraint(Pfunction_bodybody))letpexp_fun~loc(label:arg_label)exprpe=letparam:function_param={pparam_desc=Pparam_val(label,expr,p);pparam_loc=loc}inadd_fun_params~locNone[param]eletvalue_binding~loc~pat~expr=value_binding~loc~pat~expr~constraint_:Noneletconstructor_declaration~loc~name~args~res={pcd_name=name;pcd_vars=[];pcd_args=args;pcd_res=res;pcd_loc=loc;pcd_attributes=[];}(*-------------------------------------------------------*)letcoalesce_aritye=matche.pexp_descwith(* We stop coalescing parameters if there is a constraint on the result of a function
(i.e [fun x y : T -> ...] or the body is a function_case. *)|Pexp_function(_,Some_,_)|Pexp_function(_,_,Pfunction_cases_)->e|Pexp_function(params1,None,Pfunction_body({pexp_attributes=[];_}asbody1))->(matchbody1.pexp_descwith|Pexp_function(params2,constraint_,body2)->Latest.pexp_function~loc:e.pexp_loc(params1@params2)constraint_body2|_->e)|_->eletpstr_value_list~locrec_flag=function|[]->[]|vbs->[pstr_value~locrec_flagvbs]letnonrec_type_declaration~loc:_~name:_~params:_~cstrs:_~kind:_~private_:_~manifest:_=failwith"Ppxlib.Ast_builder.nonrec_type_declaration: don't use this function"leteint~loct=pexp_constant~loc(Pconst_integer(Int.to_stringt,None))letechar~loct=pexp_constant~loc(Pconst_chart)letestring~loct=pexp_constant~loc(Pconst_string(t,loc,None))letefloat~loct=pexp_constant~loc(Pconst_float(t,None))leteint32~loct=pexp_constant~loc(Pconst_integer(Int32.to_stringt,Some'l'))leteint64~loct=pexp_constant~loc(Pconst_integer(Int64.to_stringt,Some'L'))letenativeint~loct=pexp_constant~loc(Pconst_integer(Nativeint.to_stringt,Some'n'))letpint~loct=ppat_constant~loc(Pconst_integer(Int.to_stringt,None))letpchar~loct=ppat_constant~loc(Pconst_chart)letpstring~loct=ppat_constant~loc(Pconst_string(t,loc,None))letpfloat~loct=ppat_constant~loc(Pconst_float(t,None))letpint32~loct=ppat_constant~loc(Pconst_integer(Int32.to_stringt,Some'l'))letpint64~loct=ppat_constant~loc(Pconst_integer(Int64.to_stringt,Some'L'))letpnativeint~loct=ppat_constant~loc(Pconst_integer(Nativeint.to_stringt,Some'n'))letebool~loct=pexp_construct~loc(Located.lident~loc(Bool.to_stringt))Noneletpbool~loct=ppat_construct~loc(Located.lident~loc(Bool.to_stringt))Noneletevar~locv=pexp_ident~loc(Located.mk~loc(Longident.parsev))letpvar~locv=ppat_var~loc(Located.mk~locv)leteunit~loc=pexp_construct~loc(Located.lident~loc"()")Noneletpunit~loc=ppat_construct~loc(Located.lident~loc"()")Noneletpexp_tuple~locl=matchlwith[x]->x|_->pexp_tuple~loclletppat_tuple~locl=matchlwith[x]->x|_->ppat_tuple~loclletptyp_tuple~locl=matchlwith[x]->x|_->ptyp_tuple~loclletpexp_tuple_opt~locl=matchlwith[]->None|_::_->Some(pexp_tuple~locl)letppat_tuple_opt~locl=matchlwith[]->None|_::_->Some(ppat_tuple~locl)letptyp_poly~locvarsty=matchvarswith[]->ty|_->ptyp_poly~locvarstyletpexp_apply~loceel=match(e,el)with|_,[]->e|{pexp_desc=Pexp_apply(e,args);pexp_attributes=[];_},_->{ewithpexp_desc=Pexp_apply(e,args@el)}|_->pexp_apply~loceelleteapply~loceel=pexp_apply~loce(List.mapel~f:(fune->(Asttypes.Nolabel,e)))leteabstract~locpse=List.fold_rightps~init:e~f:(funpe->pexp_fun~locAsttypes.NolabelNonepe)letesequence~locel=matchList.revelwith|[]->eunit~loc|hd::tl->List.fold_lefttl~init:hd~f:(funacce->pexp_sequence~loceacc)letpconstructcdarg=ppat_construct~loc:cd.pcd_loc(Located.map_lidentcd.pcd_name)argleteconstructcdarg=pexp_construct~loc:cd.pcd_loc(Located.map_lidentcd.pcd_name)argletrecelist_tail~locltail=matchlwith|[]->tail|x::l->pexp_construct~loc(Located.mk~loc(Longident.Lident"::"))(Some(pexp_tuple~loc[x;elist_tail~locltail]))letelist~locl=lettail=pexp_construct~loc(Located.mk~loc(Longident.Lident"[]"))Noneinelist_tail~locltailletrecplist_tail~locltail=matchlwith|[]->tail|x::l->ppat_construct~loc(Located.mk~loc(Longident.Lident"::"))(Some(ppat_tuple~loc[x;plist_tail~locltail]))letplist~locl=lettail=ppat_construct~loc(Located.mk~loc(Longident.Lident"[]"))Noneinplist_tail~locltailletunapplied_type_constr_conv_without_apply~loc(ident:Longident.t)~f=matchidentwith|Lidentn->pexp_ident~loc{txt=Lident(fn);loc}|Ldot(path,n)->pexp_ident~loc{txt=Ldot(path,fn);loc}|Lapply_->Location.raise_errorf~loc"unexpected applicative functor type"lettype_constr_conv~loc:apply_loc{Loc.loc;txt=longident}~fargs=letloc={locwithloc_ghost=true}inmatch(longident:Longident.t)with|Lident_|Ldot((Lident_|Ldot_),_)|Lapply_->(letident=unapplied_type_constr_conv_without_applylongident~loc~finmatchargswith|[]->ident|_::_->eapply~loc:apply_locidentargs)|Ldot((Lapply_asmodule_path),n)->letsuffix_nfunctor_=String.uncapitalize_asciifunctor_^"__"^ninletrecgather_lapplyfunctor_args:Longident.t->Longident.t*_=function|Lapply(rest,arg)->gather_lapply(arg::functor_args)rest|Lidentfunctor_->(Lident(suffix_nfunctor_),functor_args)|Ldot(functor_path,functor_)->(Ldot(functor_path,suffix_nfunctor_),functor_args)inletident,functor_args=gather_lapply[]module_pathineapply~loc:apply_loc(unapplied_type_constr_conv_without_applyident~loc~f)(List.mapfunctor_args~f:(funpath->pexp_pack~loc(pmod_ident~loc{txt=path;loc}))@args)letunapplied_type_constr_conv~loclongident~f=type_constr_convlongident~loc~f[]leteta_reduce=letrecgather_paramsaccexpr=matchexprwith|{pexp_desc=Pexp_function([{pparam_loc=_;pparam_desc=Pparam_val(label,_,subpat)}],_constraint,Pfunction_bodybody);pexp_attributes=[];pexp_loc=_;pexp_loc_stack=_;}->(matchsubpatwith|{ppat_desc=Ppat_varname;ppat_attributes=[];ppat_loc=_;ppat_loc_stack=_;}->gather_params((label,name,None)::acc)body|{ppat_desc=Ppat_constraint({ppat_desc=Ppat_varname;ppat_attributes=[];ppat_loc=_;ppat_loc_stack=_;},ty);ppat_attributes=[];ppat_loc=_;ppat_loc_stack=_;}->(* We reduce [fun (x : ty) -> f x] by rewriting it [(f : ty -> _)]. *)gather_params((label,name,Somety)::acc)body|_->(List.revacc,expr))|_->(List.revacc,expr)inletannotate~locexprparams=ifList.existsparams~f:(fun(_,_,ty)->Option.is_somety)thenletty=List.fold_rightparams~init:(ptyp_any~loc)~f:(fun(param_label,param,ty_opt)acc->letloc=param.locinletty=matchty_optwithNone->ptyp_any~loc|Somety->tyinptyp_arrow~locparam_labeltyacc)inpexp_constraint~locexprtyelseexprinletrecgather_argsnx=ifn=0thenSome(x,[])elsematchxwith|{pexp_desc=Pexp_apply(body,args);pexp_attributes=[];pexp_loc=_;pexp_loc_stack=_;}->ifList.lengthargs<=nthenmatchgather_args(n-List.lengthargs)bodywith|None->None|Some(body,args')->Some(body,args'@args)elseNone|_->Noneinfunexpr->letparams,body=gather_params[]exprinmatchgather_args(List.lengthparams)bodywith|None->None|Some(({pexp_desc=Pexp_ident_;_}asf_ident),args)->(matchList.for_all2argsparams~f:(fun(arg_label,arg)(param_label,param,_)->Poly.(=)(arg_label:arg_label)param_label&&matchargwith|{pexp_desc=Pexp_ident{txt=Lidentname';_};pexp_attributes=[];pexp_loc=_;pexp_loc_stack=_;}->String.(=)name'param.txt|_->false)with|false->None|true->Some(annotate~loc:expr.pexp_locf_identparams))|_->Noneleteta_reduce_if_possibleexpr=Option.value(eta_reduceexpr)~default:exprleteta_reduce_if_possible_and_nonrecexpr~rec_flag=matchrec_flagwith|Recursive->expr|Nonrecursive->eta_reduce_if_possibleexprendmoduletypeLoc=Ast_builder_intf.LocmoduletypeS=sigincludeAst_builder_intf.SmoduleLatest:sigvalppat_construct:longidentloc->(labelloclist*pattern)option->patternvalconstructor_declaration:name:labelloc->vars:labelloclist->args:constructor_arguments->res:core_typeoption->unit->constructor_declarationendvalppat_construct:longidentloc->patternoption->patternvalconstructor_declaration:name:labelloc->args:constructor_arguments->res:core_typeoption->constructor_declarationendmoduleMake(Loc:sigvalloc:Location.tend):S=structincludeAst_builder_generated.Make(Loc)moduleLatest=structletppat_construct=ppat_constructletconstructor_declaration~name~vars~args~res()=constructor_declaration~name~vars~args~resend(*----- stable layer above Ast_builder_generated.Make (Loc) -----*)letppat_constructlidp={ppat_loc_stack=[];ppat_attributes=[];ppat_loc=loc;ppat_desc=Ppat_construct(lid,Option.mapp~f:(funp->([],p)));}letconstructor_declaration~name~args~res={pcd_name=name;pcd_vars=[];pcd_args=args;pcd_res=res;pcd_loc=loc;pcd_attributes=[];}(*---------------------------------------------------------------*)letpstr_value_list=Default.pstr_value_listletnonrec_type_declaration~name~params~cstrs~kind~private_~manifest=Default.nonrec_type_declaration~loc~name~params~cstrs~kind~private_~manifestmoduleLocated=structincludeDefault.Locatedletloc_=Loc.locletmkx=mk~loc:Loc.locxletlidentx=lident~loc:Loc.locxendletpexp_tuplel=Default.pexp_tuple~loclletppat_tuplel=Default.ppat_tuple~loclletptyp_tuplel=Default.ptyp_tuple~loclletpexp_tuple_optl=Default.pexp_tuple_opt~loclletppat_tuple_optl=Default.ppat_tuple_opt~loclletptyp_polyvarsty=Default.ptyp_poly~locvarstyletpexp_applyeel=Default.pexp_apply~loceelletpexp_funlble1pe2=Default.pexp_fun~loclble1pe2letpexp_function_casescases=Default.pexp_function_cases~loccasesleteintt=Default.eint~loctletechart=Default.echar~loctletestringt=Default.estring~loctletefloatt=Default.efloat~loctleteint32t=Default.eint32~loctleteint64t=Default.eint64~loctletenativeintt=Default.enativeint~loctleteboolt=Default.ebool~loctletevart=Default.evar~loctletpintt=Default.pint~loctletpchart=Default.pchar~loctletpstringt=Default.pstring~loctletpfloatt=Default.pfloat~loctletpint32t=Default.pint32~loctletpint64t=Default.pint64~loctletpnativeintt=Default.pnativeint~loctletpboolt=Default.pbool~loctletpvart=Default.pvar~loctleteunit=Default.eunit~locletpunit=Default.punit~locleteconstruct=Default.econstructletpconstruct=Default.pconstructleteapplyeel=Default.eapply~loceelleteabstractpse=Default.eabstract~locpseletesequenceel=Default.esequence~locelletelist_tailltail=Default.elist_tail~locltailletplist_tailltail=Default.plist_tail~locltailletelistl=Default.elist~loclletplistl=Default.plist~loclletvalue_binding=Default.value_binding~loclettype_constr_convident~fargs=Default.type_constr_conv~locident~fargsletunapplied_type_constr_convident~f=Default.unapplied_type_constr_conv~locident~fleteta_reduce=Default.eta_reduceleteta_reduce_if_possible=Default.eta_reduce_if_possibleleteta_reduce_if_possible_and_nonrec=Default.eta_reduce_if_possible_and_nonrecendletmakeloc=(moduleMake(structletloc=locend):S)