123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106(** Re-export for compatibility with 4.02. *)type('a,'b)result=('a,'b)Result.result=Okof'a|Errorof'b(** The [result] type and a bind operator. This module is meant to be opened. *)moduleResultMonad=struct(** Re-export for compat *)type('a,'b)result=('a,'b)Result.result=Okof'a|Errorof'bletmap_errorf=functionOk_asok->ok|Errore->Error(fe)letof_option~error=functionSomex->Okx|None->Errorerrorletbindmf=matchmwithOkx->fx|Error_ase->elet(>>=)=bindend(** A bind operator for the [option] type. This module is meant to be opened. *)moduleOptionMonad=struct(* The error case become [None], the error value is ignored. *)letof_result=functionResult.Okx->Somex|Error_->Noneletreturnx=Somexletbindmf=matchmwithSomex->fx|None->Nonelet(>>=)=bindendmoduleEitherMonad=structtype('a,'b)t=Leftof'a|Rightof'bletreturnx=Rightxletreturn_leftx=Leftxletbindmf=matchmwithRightx->fx|Lefty->Leftyletbind_leftmf=matchmwithLeftx->fx|Righty->Rightylet(>>=)=bindletof_option~left=functionSomex->Rightx|None->Leftleftletof_result=functionResult.Okx->Right x|Errory->LeftyendmoduleList=structincludeListletrecconcat_map?sep~f=function|[]->[]|[x]->fx|x::xs->(lethd=fxinlettl=concat_map?sep~fxsinmatchsepwithNone->hd@tl|Somesep->hd@(sep::tl))letrecfilter_mapaccf=function|hd::tl->letacc=matchfhdwithSomex->x::acc|None->accinfilter_mapaccftl|[]->List.revaccletfilter_mapfx=filter_map[]fx(** @raise [Failure] if the list is empty. *)letreclast=function|[]->failwith"Odoc_utils.List.last"|[x]->x|_:: tl->lasttl(* From ocaml/ocaml *)letrecfind_mapf=function|[]->None|x::l->(matchfxwithSome_asresult->result|None->find_mapfl)endmoduleOption=structletmapf=functionNone->None|Somex->Some(fx)letis_some=functionNone->false|Some_->trueendmoduleResult=structincludeResultletjoin=functionOkr->r|Error_ase->eendmoduleFun=structexceptionFinally_raisedofexnletprotect~(finally:unit->unit)work=letfinally_no_exn()=tryfinally()withe->raise(Finally_raisede)inmatchwork()with|result->finally_no_exn();result|exceptionwork_exn->finally_no_exn();raisework_exnend