123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openMonadmoduleLegacy=Stdlib.ListincludeLegacyletnil=[]letnil_e=Ok[]letnil_s=Lwt.return_nilletnil_es=Lwt.returnnil_elethd=functionx::_->Somex|[]->Nonelettl=function_::xs->Somexs|[]->Noneletnthxsn=ifn<0thenNoneelseletrecauxxsn=match(xs,n)with|([],_)->None|(x::_,0)->Somex|(_::xs,n)->(aux[@ocaml.tailcall])xs(n-1)inauxxsnletreclasthd=function|[]->hd|[last]->last|hd::(_::_astl)->(last[@ocaml.tailcall])hdtlletlast_opt=function[]->None|hd::tl->Some(lasthdtl)letfind=find_optletreciter2~when_different_lengthsfxsys=(* NOTE: We could do the following but we would need to assume [f] does not
raise [Invalid_argument]
[try
Ok (iter2 f xs ys)
with Invalid_argument _ ->
Error when_different_lengths]
The same remark applies to the other 2-list iterators.
*)match(xs,ys)with|([],[])->Monad.unit_e|([],_::_)|(_::_,[])->Errorwhen_different_lengths|(x::xs,y::ys)->fxy;(iter2[@ocaml.tailcall])~when_different_lengthsfxsysletrev_map2~when_different_lengthsfxsys=letrecauxzsxsys=match(xs,ys)with|([],[])->Okzs|([],_::_)|(_::_,[])->Errorwhen_different_lengths|(x::xs,y::ys)->letz=fxyin(aux[@ocaml.tailcall])(z::zs)xsysinaux[]xsysletmap2~when_different_lengthsfxsys=rev_map2~when_different_lengthsfxsys>|?revletfold_left2~when_different_lengthsfaxsys=letrecauxaccxsys=match(xs,ys)with|([],[])->Okacc|([],_::_)|(_::_,[])->Errorwhen_different_lengths|(x::xs,y::ys)->letacc=faccxyin(aux[@ocaml.tailcall])accxsysinauxaxsysletfold_right2~when_different_lengthsfxsysa=letrecauxxsys=match(xs,ys)with|([],[])->Oka|([],_::_)|(_::_,[])->Errorwhen_different_lengths|(x::xs,y::ys)->auxxsys>|?funacc->fxyaccinauxxsysletfor_all2~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],[])->Oktrue|([],_::_)|(_::_,[])->Errorwhen_different_lengths|(x::xs,y::ys)->(matchfxywith|true->(aux[@ocaml.tailcall])xsys|false->Okfalse)inauxxsysletexists2~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],[])->Okfalse|([],_::_)|(_::_,[])->Errorwhen_different_lengths|(x::xs,y::ys)->(matchfxywith|true->Oktrue|false->(aux[@ocaml.tailcall])xsys)inauxxsysletrecmem~equalx=function|[]->false|y::ys->equalxy||mem~equalxysletrecassoc~equalk=function|[]->None|(kk,v)::kvs->ifequalkkkthenSomevelseassoc~equalkkvsletassoc_opt=assocletassq=assq_optletrecmem_assoc~equalk=function|[]->false|(kk,_)::kvs->equalkkk||mem_assoc~equalkkvsletrecremove_assoc~equalk=function|[]->[]|((kk,_)askv)::kvs->ifequalkkkthenkvselsekv::remove_assoc~equalkkvsletinit~when_negative_lengthlf=ifl<0thenErrorwhen_negative_lengthelseifl=0thennil_eelseOk(Legacy.initlf)letinit_e~when_negative_lengthlf=letrecauxacci=ifi>=lthenOk(revacc)elsefi>>?funv->(aux[@ocaml.tailcall])(v::acc)(i+1)inifl<0thenErrorwhen_negative_lengthelseifl=0thennil_eelseaux[]0letinit_s~when_negative_lengthlf=letrecauxacci=ifi>=lthenLwt.return(Ok(revacc))elsefi>>=funv->(aux[@ocaml.tailcall])(v::acc)(i+1)inifl<0thenLwt.return(Errorwhen_negative_length)elseifl=0thennil_eselseLwt.applyf0>>=funv->aux[v]1letinit_es~when_negative_lengthlf=letrecauxacci=ifi>=lthenLwt.return(Ok(revacc))elsefi>>=?funv->(aux[@ocaml.tailcall])(v::acc)(i+1)inifl<0thenLwt.return(Errorwhen_negative_length)elseifl=0thennil_eselseLwt.applyf0>>=?funv->aux[v]1letinit_ep~when_negative_lengthlf=letrecauxacci=ifi>=lthenall_ep(revacc)else(aux[@ocaml.tailcall])(Lwt.applyfi::acc)(i+1)inifl<0thenLwt.return(Error[when_negative_length])elseifl=0thennil_eselseaux[]0letinit_p~when_negative_lengthlf=letrecauxacci=ifi>=lthenall_p(revacc)>>=funxs->Lwt.return(Okxs)else(aux[@ocaml.tailcall])(Lwt.applyfi::acc)(i+1)inifl<0thenLwt.return(Errorwhen_negative_length)elseifl=0thennil_eselseaux[]0letrecfind_ef=function|[]->none_e|x::xs->(fx>>?function|true->Ok(Somex)|false->(find_e[@ocaml.tailcall])fxs)letrecfind_sf=function|[]->none_s|x::xs->(fx>>=function|true->Lwt.return(Somex)|false->(find_s[@ocaml.tailcall])fxs)letfind_sf=function|[]->none_s|x::xs->(Lwt.applyfx>>=function|true->Lwt.return(Somex)|false->(find_s[@ocaml.tailcall])fxs)letrecfind_esf=function|[]->none_es|x::xs->(fx>>=?function|true->Lwt.return(Ok(Somex))|false->(find_es[@ocaml.tailcall])fxs)letfind_esf=function|[]->none_es|x::xs->(Lwt.applyfx>>=?function|true->Lwt.return(Ok(Somex))|false->(find_es[@ocaml.tailcall])fxs)letrev_filterfxs=fold_left(funrev_xsx->iffxthenx::rev_xselserev_xs)[]xsletrev_filter_efxs=letrecauxacc=function|[]->Okacc|x::xs->(fx>>?function|true->(aux[@ocaml.tailcall])(x::acc)xs|false->(aux[@ocaml.tailcall])accxs)inaux[]xsletrev_filter_someoxs=letrecauxxs=function|[]->xs|Somex::oxs->(aux[@ocaml.tailcall])(x::xs)oxs|None::oxs->(aux[@ocaml.tailcall])xsoxsinaux[]oxsletfilter_someoxs=rev_filter_someoxs|>revletrev_filter_okrxs=letrecauxxs=function|[]->xs|Okx::rxs->(aux[@ocaml.tailcall])(x::xs)rxs|Error_::rxs->(aux[@ocaml.tailcall])xsrxsinaux[]rxsletfilter_okrxs=rev_filter_okrxs|>revletrev_filter_errorrxs=letrecauxxs=function|[]->xs|Errorx::rxs->(aux[@ocaml.tailcall])(x::xs)rxs|Ok_::rxs->(aux[@ocaml.tailcall])xsrxsinaux[]rxsletfilter_errorrxs=rev_filter_errorrxs|>revletfilter_efxs=rev_filter_efxs>|?revletrev_filter_sfxs=letrecauxacc=function|[]->Lwt.returnacc|x::xs->(fx>>=function|true->(aux[@ocaml.tailcall])(x::acc)xs|false->(aux[@ocaml.tailcall])accxs)inmatchxswith|[]->Lwt.return[]|x::xs->(Lwt.applyfx>>=function|true->(aux[@ocaml.tailcall])[x]xs|false->(aux[@ocaml.tailcall])[]xs)letfilter_sfxs=rev_filter_sfxs>|=revletrev_filter_esfxs=letrecauxacc=function|[]->Lwt.return(Okacc)|x::xs->(fx>>=?function|true->(aux[@ocaml.tailcall])(x::acc)xs|false->(aux[@ocaml.tailcall])accxs)inmatchxswith|[]->Lwt.return(Ok[])|x::xs->(Lwt.applyfx>>=?functiontrue->aux[x]xs|false->aux[]xs)letfilter_esfxs=rev_filter_esfxs>|=?revletreciter_ef=function|[]->unit_e|h::t->fh>>?fun()->(iter_e[@ocaml.tailcall])ftletreciter_sf=function|[]->unit_s|h::t->fh>>=fun()->(iter_s[@ocaml.tailcall])ftletiter_sf=function|[]->unit_s|h::t->Lwt.applyfh>>=fun()->(iter_s[@ocaml.tailcall])ftletreciter_esf=function|[]->unit_es|h::t->fh>>=?fun()->(iter_es[@ocaml.tailcall])ftletiter_esf=function|[]->unit_es|h::t->Lwt.applyfh>>=?fun()->(iter_es[@ocaml.tailcall])ftletiter_epfl=join_ep(rev_map(Lwt.applyf)l)letiter_pfl=join_p(rev_map(Lwt.applyf)l)letiteri_efl=letrecauxi=function|[]->unit_e|x::xs->fix>>?fun()->(aux[@ocaml.tailcall])(i+1)xsinaux0lletiteri_sfl=letrecauxi=function|[]->unit_s|x::xs->fix>>=fun()->(aux[@ocaml.tailcall])(i+1)xsinmatchlwith|[]->unit_s|x::xs->lwt_apply2f0x>>=fun()->aux1xsletiteri_esfl=letrecauxi=function|[]->unit_es|x::xs->fix>>=?fun()->(aux[@ocaml.tailcall])(i+1)xsinmatchlwith|[]->unit_es|x::xs->lwt_apply2f0x>>=?fun()->aux1xsletiteri_epfl=join_ep(mapi(lwt_apply2f)l)letiteri_pfl=join_p(mapi(lwt_apply2f)l)letrev_map_efl=letrecauxys=function|[]->Okys|x::xs->fx>>?funy->(aux[@ocaml.tailcall])(y::ys)xsinaux[]lletmap_efl=rev_map_efl>|?revletrev_map_sfl=letrecauxys=function|[]->Lwt.returnys|x::xs->fx>>=funy->(aux[@ocaml.tailcall])(y::ys)xsinmatchlwith|[]->Lwt.return[]|x::xs->Lwt.applyfx>>=funy->aux[y]xsletmap_sfl=rev_map_sfl>|=revletrev_map_esfl=letrecauxys=function|[]->returnys|x::xs->fx>>=?funy->(aux[@ocaml.tailcall])(y::ys)xsinmatchlwith|[]->return[]|x::xs->Lwt.applyfx>>=?funy->aux[y]xsletrev_map_epfl=all_ep@@rev_map(Lwt.applyf)lletmap_esfl=rev_map_esfl>|=?revletmap_epfl=rev_map_epfl>|=?revletrev_map_pfl=all_p@@rev_map(Lwt.applyf)lletmap_pfl=rev_map_pfl>|=revletrev_mapi_efl=letrecauxiys=function|[]->Okys|x::xs->fix>>?funy->(aux[@ocaml.tailcall])(i+1)(y::ys)xsinaux0[]lletmapi_efl=rev_mapi_efl>|?revletrev_mapi_sfl=letrecauxiys=function|[]->Lwt.returnys|x::xs->fix>>=funy->(aux[@ocaml.tailcall])(i+1)(y::ys)xsinmatchlwith|[]->Lwt.return[]|x::xs->lwt_apply2f0x>>=funy->aux1[y]xsletmapi_sfl=rev_mapi_sfl>|=revletrev_mapi_esfl=letrecauxiys=function|[]->returnys|x::xs->fix>>=?funy->(aux[@ocaml.tailcall])(i+1)(y::ys)xsinmatchlwith|[]->return[]|x::xs->lwt_apply2f0x>>=?funy->aux1[y]xsletmapi_esfl=rev_mapi_esfl>|=?revletrev_mapifl=letrecauxiys=function|[]->ys|x::xs->(aux[@ocaml.tailcall])(i+1)(fix::ys)xsinaux0[]lletrev_mapi_pfl=all_p@@rev_mapiflletrev_mapi_epfl=all_ep@@rev_mapiflletmapi_pfl=rev_mapi_pfl>|=revletmapi_epfl=rev_mapi_epfl>|=?revletrecfold_left_efacc=function|[]->Okacc|x::xs->faccx>>?funacc->(fold_left_e[@ocaml.tailcall])faccxsletrecfold_left_sfacc=function|[]->Lwt.returnacc|x::xs->faccx>>=funacc->(fold_left_s[@ocaml.tailcall])faccxsletfold_left_sfacc=function|[]->Lwt.returnacc|x::xs->lwt_apply2faccx>>=funacc->fold_left_sfaccxsletrecfold_left_esfacc=function|[]->returnacc|x::xs->faccx>>=?funacc->(fold_left_es[@ocaml.tailcall])faccxsletfold_left_esfacc=function|[]->returnacc|x::xs->lwt_apply2faccx>>=?funacc->fold_left_esfaccxsletfilter_pfl=rev_map_p(funx->fx>|=funb->ifbthenSomexelseNone)l>|=rev_filter_someletfilter_epfl=rev_map_ep(funx->fx>|=?funb->ifbthenSomexelseNone)l>|=?rev_filter_someletrev_filter_mapfl=fold_left(funaccx->matchfxwithNone->acc|Somey->y::acc)[]lletfilter_mapfl=rev_filter_mapfl|>revletrev_filter_map_efl=fold_left_e(funaccx->fx>|?functionNone->acc|Somey->y::acc)[]lletfilter_map_efl=rev_filter_map_efl>|?revletrev_filter_map_sfl=fold_left_s(funaccx->fx>|=functionNone->acc|Somey->y::acc)[]lletfilter_map_sfl=rev_filter_map_sfl>|=revletrev_filter_map_esfl=fold_left_es(funaccx->fx>|=?functionNone->acc|Somey->y::acc)[]lletfilter_map_esfl=rev_filter_map_esfl>|=?revletfilter_map_epfl=rev_map_epfl>|=?rev_filter_someletfilter_map_pfl=rev_map_pfl>|=rev_filter_someletrecfold_right_eflacc=matchlwith|[]->Okacc|x::xs->fold_right_efxsacc>>?funacc->fxaccletrecfold_right_sflacc=matchlwith|[]->Lwt.returnacc|x::xs->fold_right_sfxsacc>>=funacc->fxaccletrecfold_right_esflacc=matchlwith|[]->returnacc|x::xs->fold_right_esfxsacc>>=?funacc->fxaccletrev_map2_e~when_different_lengthsfxsys=letrecauxzsxsys=match(xs,ys)with|([],[])->Okzs|(x::xs,y::ys)->fxy>>?funz->(aux[@ocaml.tailcall])(z::zs)xsys|([],_::_)|(_::_,[])->Errorwhen_different_lengthsinaux[]xsysletrev_map2_s~when_different_lengthsfxsys=letrecauxzsxsys=match(xs,ys)with|([],[])->Lwt.return(Okzs)|(x::xs,y::ys)->fxy>>=funz->(aux[@ocaml.tailcall])(z::zs)xsys|([],_::_)|(_::_,[])->failwhen_different_lengthsinmatch(xs,ys)with|([],[])->Lwt.return(Ok[])|(x::xs,y::ys)->lwt_apply2fxy>>=funz->aux[z]xsys|([],_::_)|(_::_,[])->failwhen_different_lengthsletrev_map2_es~when_different_lengthsfxsys=letrecauxzsxsys=match(xs,ys)with|([],[])->Lwt.return(Okzs)|(x::xs,y::ys)->fxy>>=?funz->(aux[@ocaml.tailcall])(z::zs)xsys|([],_::_)|(_::_,[])->failwhen_different_lengthsinmatch(xs,ys)with|([],[])->Lwt.return(Ok[])|(x::xs,y::ys)->lwt_apply2fxy>>=?funz->aux[z]xsys|([],_::_)|(_::_,[])->failwhen_different_lengthsletmap2_e~when_different_lengthsfxsys=rev_map2_e~when_different_lengthsfxsys>|?revletmap2_s~when_different_lengthsfxsys=rev_map2_s~when_different_lengthsfxsys>|=?revletmap2_es~when_different_lengthsfxsys=rev_map2_es~when_different_lengthsfxsys>|=?revletiter2_e~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],[])->unit_e|(x::xs,y::ys)->fxy>>?fun()->(aux[@ocaml.tailcall])xsys|([],_::_)|(_::_,[])->Errorwhen_different_lengthsinauxxsysletiter2_s~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],[])->Lwt.return(Ok())|(x::xs,y::ys)->fxy>>=fun()->(aux[@ocaml.tailcall])xsys|([],_::_)|(_::_,[])->failwhen_different_lengthsinmatch(xs,ys)with|([],[])->Lwt.return(Ok())|(x::xs,y::ys)->lwt_apply2fxy>>=fun()->auxxsys|([],_::_)|(_::_,[])->failwhen_different_lengthsletiter2_es~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],[])->Monad.unit_es|(x::xs,y::ys)->fxy>>=?fun()->(aux[@ocaml.tailcall])xsys|([],_::_)|(_::_,[])->failwhen_different_lengthsinmatch(xs,ys)with|([],[])->Monad.unit_es|(x::xs,y::ys)->lwt_apply2fxy>>=?fun()->auxxsys|([],_::_)|(_::_,[])->failwhen_different_lengthsletfold_left2_e~when_different_lengthsfinitxsys=letrecauxaccxsys=match(xs,ys)with|([],[])->Okacc|(x::xs,y::ys)->faccxy>>?funacc->(aux[@ocaml.tailcall])accxsys|([],_::_)|(_::_,[])->Errorwhen_different_lengthsinauxinitxsysletfold_left2_s~when_different_lengthsfinitxsys=letrecauxaccxsys=match(xs,ys)with|([],[])->Lwt.return(Okacc)|(x::xs,y::ys)->faccxy>>=funacc->(aux[@ocaml.tailcall])accxsys|([],_::_)|(_::_,[])->failwhen_different_lengthsinmatch(xs,ys)with|([],[])->Lwt.return(Okinit)|(x::xs,y::ys)->lwt_apply3finitxy>>=funacc->auxaccxsys|([],_::_)|(_::_,[])->failwhen_different_lengthsletfold_left2_es~when_different_lengthsfinitxsys=letrecauxaccxsys=match(xs,ys)with|([],[])->Lwt.return(Okacc)|(x::xs,y::ys)->faccxy>>=?funacc->(aux[@ocaml.tailcall])accxsys|([],_::_)|(_::_,[])->failwhen_different_lengthsinmatch(xs,ys)with|([],[])->Lwt.return(Okinit)|(x::xs,y::ys)->lwt_apply3finitxy>>=?funacc->(aux[@ocaml.tailcall])accxsys|([],_::_)|(_::_,[])->failwhen_different_lengthsletfold_right2_e~when_different_lengthsfxsysinit=letrecauxxsys=match(xs,ys)with|([],[])->Okinit|(x::xs,y::ys)->auxxsys>>?funacc->fxyacc|([],_::_)|(_::_,[])->Errorwhen_different_lengthsinauxxsysletfold_right2_s~when_different_lengthsfxsysinit=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->Lwt.return(Okinit)|(x::xs,y::ys)->(* We could use a specific operator for that. It'd need the following type
('a, 'err) result Lwt.t -> ('a -> 'b Lwt.t) -> ('b, 'err) result Lwt.t
*)auxxsys>>=?funacc->fxyacc>|=okinauxxsysletfold_right2_es~when_different_lengthsfxsysinit=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->Lwt.return(Okinit)|(x::xs,y::ys)->auxxsys>>=?funacc->fxyaccinauxxsysletrecfor_all_ef=function|[]->true_e|x::xs->(fx>>?function|true->(for_all_e[@ocaml.tailcall])fxs|false->false_e)letrecfor_all_sf=function|[]->true_s|x::xs->(fx>>=function|true->(for_all_s[@ocaml.tailcall])fxs|false->false_s)letfor_all_sf=function|[]->true_s|x::xs->(Lwt.applyfx>>=function|true->(for_all_s[@ocaml.tailcall])fxs|false->false_s)letrecfor_all_esf=function|[]->true_es|x::xs->(fx>>=?function|true->(for_all_es[@ocaml.tailcall])fxs|false->false_es)letfor_all_esf=function|[]->true_es|x::xs->(Lwt.applyfx>>=?function|true->(for_all_es[@ocaml.tailcall])fxs|false->false_es)letfor_all_epfl=rev_map_epfl>|=?for_allFun.idletfor_all_pfl=rev_map_pfl>|=for_allFun.idletrecexists_ef=function|[]->false_e|x::xs->(fx>>?function|false->(exists_e[@ocaml.tailcall])fxs|true->true_e)letrecexists_sf=function|[]->false_s|x::xs->(fx>>=function|false->(exists_s[@ocaml.tailcall])fxs|true->true_s)letexists_sf=function|[]->false_s|x::xs->(Lwt.applyfx>>=functionfalse->exists_sfxs|true->true_s)letrecexists_esf=function|[]->false_es|x::xs->(fx>>=?function|false->(exists_es[@ocaml.tailcall])fxs|true->true_es)letexists_esf=function|[]->false_es|x::xs->(Lwt.applyfx>>=?functionfalse->exists_esfxs|true->true_es)letexists_epfl=rev_map_epfl>|=?existsFun.idletexists_pfl=rev_map_pfl>|=existsFun.idletfor_all2_e~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->Errorwhen_different_lengths|([],[])->true_e|(x::xs,y::ys)->(fxy>>?function|true->(aux[@ocaml.tailcall])xsys|false->false_e)inauxxsysletfor_all2_s~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->true_es|(x::xs,y::ys)->(fxy>>=function|true->(aux[@ocaml.tailcall])xsys|false->false_es)inmatch(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->true_es|(x::xs,y::ys)->(lwt_apply2fxy>>=functiontrue->auxxsys|false->false_es)letfor_all2_es~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->true_es|(x::xs,y::ys)->(fxy>>=?function|true->(aux[@ocaml.tailcall])xsys|false->false_es)inmatch(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->true_es|(x::xs,y::ys)->(lwt_apply2fxy>>=?functiontrue->auxxsys|false->false_es)letexists2_e~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->Errorwhen_different_lengths|([],[])->false_e|(x::xs,y::ys)->(fxy>>?function|false->(aux[@ocaml.tailcall])xsys|true->true_e)inauxxsysletexists2_s~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->false_es|(x::xs,y::ys)->(fxy>>=function|false->(aux[@ocaml.tailcall])xsys|true->true_es)inmatch(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->false_es|(x::xs,y::ys)->(lwt_apply2fxy>>=functionfalse->auxxsys|true->true_es)letexists2_es~when_different_lengthsfxsys=letrecauxxsys=match(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->false_es|(x::xs,y::ys)->(fxy>>=?function|false->(aux[@ocaml.tailcall])xsys|true->true_es)inmatch(xs,ys)with|([],_::_)|(_::_,[])->failwhen_different_lengths|([],[])->false_es|(x::xs,y::ys)->(lwt_apply2fxy>>=?functionfalse->auxxsys|true->true_es)letrev_partitionfxs=letrecauxtruesfalses=function|[]->(trues,falses)|x::xs->iffxthen(aux[@ocaml.tailcall])(x::trues)falsesxselse(aux[@ocaml.tailcall])trues(x::falses)xsinaux[][]xsletpartitionfxs=rev_partitionfxs|>fun(trues,falses)->(revtrues,revfalses)letrev_partition_resultxs=letrecauxokserrors=function|[]->(oks,errors)|Okok::xs->(aux[@ocaml.tailcall])(ok::oks)errorsxs|Errorerror::xs->(aux[@ocaml.tailcall])oks(error::errors)xsinaux[][]xsletpartition_resultxs=let(rev_oks,rev_errors)=rev_partition_resultxsin(revrev_oks,revrev_errors)letrev_partition_efl=letrecauxtruesfalses=function|[]->Ok(trues,falses)|x::xs->fx>>?funb->ifbthen(aux[@ocaml.tailcall])(x::trues)falsesxselse(aux[@ocaml.tailcall])trues(x::falses)xsinaux[][]lletpartition_efl=rev_partition_efl>|?fun(trues,falses)->(revtrues,revfalses)letrev_partition_sfl=letrecauxtruesfalses=function|[]->Lwt.return(trues,falses)|x::xs->fx>>=funb->ifbthen(aux[@ocaml.tailcall])(x::trues)falsesxselse(aux[@ocaml.tailcall])trues(x::falses)xsinmatchlwith|[]->Lwt.return([],[])|x::xs->Lwt.applyfx>>=funb->ifbthenaux[x][]xselseaux[][x]xsletpartition_sfl=rev_partition_sfl>|=fun(trues,falses)->(revtrues,revfalses)letrev_partition_esfl=letrecauxtruesfalses=function|[]->return(trues,falses)|x::xs->fx>>=?funb->ifbthen(aux[@ocaml.tailcall])(x::trues)falsesxselse(aux[@ocaml.tailcall])trues(x::falses)xsinmatchlwith|[]->return([],[])|x::xs->Lwt.applyfx>>=?funb->ifbthenaux[x][]xselseaux[][x]xsletpartition_esfl=rev_partition_esfl>|=?fun(trues,falses)->(revtrues,revfalses)letpartition_epfl=rev_map_ep(funx->fx>|=?funb->(b,x))l>|=?funbxs->fold_left(fun(trues,falses)(b,x)->ifbthen(x::trues,falses)else(trues,x::falses))([],[])bxsletpartition_pfl=rev_map_p(funx->fx>|=funb->(b,x))l>|=funbxs->fold_left(fun(trues,falses)(b,x)->ifbthen(x::trues,falses)else(trues,x::falses))([],[])bxsletcombine~when_different_lengthsxsys=map2~when_different_lengths(funxy->(x,y))xsysletrev_combine~when_different_lengthsxsys=rev_map2~when_different_lengths(funxy->(x,y))xsysletcombine_with_leftoversxsys=letrecauxrev_combinedxsys=match(xs,ys)with|([],[])->(revrev_combined,None)|((_::_asleft),[])->(revrev_combined,Some(`Leftleft))|([],(_::_asright))->(revrev_combined,Some(`Rightright))|(x::xs,y::ys)->(aux[@ocaml.tailcall])((x,y)::rev_combined)xsysinaux[]xsysletcombine_dropxsys=letrecauxrev_combinedxsys=match(xs,ys)with|(x::xs,y::ys)->(aux[@ocaml.tailcall])((x,y)::rev_combined)xsys|([],[])|(_::_,[])|([],_::_)->revrev_combinedinaux[]xsysletreccompareecompxsys=match(xs,ys)with|([],[])->0|([],_::_)->-1|(_::_,[])->1|(x::xs,y::ys)->letec=ecompxyinifec=0thencompareecompxsyselseecletrecequaleeqxsys=match(xs,ys)with|([],[])->true|([],_::_)|(_::_,[])->false|(x::xs,y::ys)->eeqxy&&equaleeqxsys