123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openMonad(* This module is about sequences mixed in with Lwt+result, the most common monad
here is the combined Lwt-Result monad, we open its syntax module for the whole file (and
shadow it when needed. *)openLwt_result_syntaxincludeSeqes.Monadic.Make2(Lwt_result)letreturn_er()=let*?x=rinLwt_result_syntax.return(Cons(x,empty))letreturn_sp()=let*!x=pinLwt_result_syntax.return(Cons(x,empty))letreturn_esp()=let*x=pinLwt_result_syntax.return(Cons(x,empty))letinterruptede()=Lwt.return(Errore)letinterrupted_sp()=Lwt_syntax.(let*)pLwt.return_errorletrecmap_errorfs()=let*!r=s()inmatchrwith|OkNil->Lwt_result_syntax.returnNil|Ok(Cons(x,s))->Lwt_result_syntax.return(Cons(x,map_errorfs))|Errore->Lwt_result_syntax.fail(fe)letrecmap_error_sfs()=let*!r=s()inmatchrwith|OkNil->Lwt_result_syntax.returnNil|Ok(Cons(x,s))->Lwt_result_syntax.return(Cons(x,map_error_sfs))|Errore->let*!e=feinLwt_result_syntax.failelettake~when_negative_lengthns=ifn<0thenErrorwhen_negative_lengthelseOk(takens)letdrop~when_negative_lengthns=ifn<0thenErrorwhen_negative_lengthelseOk(dropns)moduleS=Make(structtype('a,'e)t='aLwt.tletbind=Lwt.bindletreturn=Lwt.returnend)(structletbind=Lwt.bindend)moduleE=Make(Result)(structletbindxf=matchxwithError_aserr->Lwt.returnerr|Okx->fxend)moduleES=Mletcons_eitemt()=matchitemwith|Error_ase->Lwt.returne|Okitem->Lwt_result_syntax.return(Cons(item,t))letcons_sitemt()=letopenLwt_syntaxinlet*iteminreturn_ok(Cons(item,t))letcons_esitemt()=let*iteminLwt_result_syntax.return(Cons(item,t))letrecof_seq_eseq()=matchseq()with|Stdlib.Seq.Nil->empty()|Stdlib.Seq.Cons(Oke,seq)->Lwt_result_syntax.return(Cons(e,of_seq_eseq))|Stdlib.Seq.Cons((Error_ase),_)->Lwt.returneletrecof_seqeseq()=matchseq()with|OkSeq_e.Nil->empty()|Ok(Seq_e.Cons(item,seq))->Lwt_result_syntax.return(Cons(item,of_seqeseq))|Error_ase->Lwt.returneletrecof_seq_sseq()=matchseq()with|Stdlib.Seq.Nil->empty()|Stdlib.Seq.Cons(p,seq)->letopenLwt_syntaxinlet*e=pinreturn_ok(Cons(e,of_seq_sseq))letrecof_seqsseq()=letopenLwt_syntaxinlet*n=seq()inmatchnwith|Seq_s.Nil->empty()|Seq_s.Cons(e,seq)->return_ok(Cons(e,of_seqsseq))letrecof_seq_esseq()=matchseq()with|Stdlib.Seq.Nil->empty()|Stdlib.Seq.Cons(p,seq)->let*e=pinLwt_result_syntax.return(Cons(e,of_seq_esseq))