123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 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. *)(* *)(*****************************************************************************)(** Lwt monad *)let(>>=)=Lwt.(>>=)let(>|=)=Lwt.(>|=)(** result monad *)letokx=Okxleterrore=Errorelet(>>?)vf=matchvwithOkv->fv|Error_aserror->errorlet(>|?)vf=matchvwithOkv->Ok(fv)|Error_aserror->error(** lwt-result combined monad *)letok_sv=Lwt.return(Okv)letreturn=ok_sleterror_sv=Lwt.return(Errorv)letfail=error_slet(>>=?)vf=v>>=functionError_aserr->Lwt.returnerr|Okv->fvlet(>|=?)vf=v>>=?funv->Lwt.return(Ok(fv))(** Mixing operators *)(** All operators follow this naming convention:
- the first character is [>]
- the second character is [>] for [bind] and [|] for [map]
- the next character is [=] for Lwt or [?] for Error
- the next character (if present) is [=] for Lwt or [?] for Error, it is
only used for operator that are within both monads. *)let(>>?=)vf=matchvwithError_ase->Lwt.returne|Okv->fvlet(>|?=)vf=matchvwith|Error_ase->Lwt.returne|Okv->fv>>=funv->Lwt.return(Okv)letunit_s=Lwt.return_unitletunit_e=Ok()letok_unit=unit_eletunit_es=Lwt.returnunit_eletreturn_unit=unit_esletnone_s=Lwt.return_noneletnone_e=OkNoneletnone_es=Lwt.returnnone_eletsome_sx=Lwt.return(Somex)letsome_ex=Ok(Somex)letsome_esx=Lwt.return(Ok(Somex))letnil_s=Lwt.return_nilletnil_e=Ok[]letnil_es=Lwt.returnnil_elettrue_s=Lwt.return_truelettrue_e=Oktruelettrue_es=Lwt.returntrue_eletfalse_s=Lwt.return_falseletfalse_e=Okfalseletfalse_es=Lwt.returnfalse_e(* joins *)letjoin_p=Lwt.joinletall_p=Lwt.allletboth_p=Lwt.bothletrecjoin_e_errorserrors=function|Ok_::ts->join_e_errorserrorsts|Errorerror::ts->join_e_errors(error::errors)ts|[]->Errorerrorsletrecjoin_e=function|[]->unit_e|Ok()::ts->join_ets|Errorerror::ts->join_e_errors[error]tsletall_ets=letrecauxacc=function|[]->Ok(Stdlib.List.revacc)|Okv::ts->aux(v::acc)ts|Errorerror::ts->join_e_errors[error]tsinaux[]tsletboth_eab=match(a,b)with|(Oka,Okb)->Ok(a,b)|(Errorerr,Ok_)|(Ok_,Errorerr)->Error[err]|(Errorerra,Errorerrb)->Error[erra;errb]letjoin_epts=all_pts>|=join_eletall_epts=all_pts>|=all_eletboth_epab=both_pab>|=fun(a,b)->both_eab(**/**)(* For internal use only, not advertised *)(* Like Lwt.apply but specialised for two-parameters functions *)letlwt_apply2fxy=tryfxywithexn->Lwt.failexnletlwt_apply3faxy=tryfaxywithexn->Lwt.failexn