123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113(**************************************************************************)(* *)(* OCaml *)(* *)(* The OCaml programmers *)(* *)(* Copyright 2018 Institut National de Recherche en Informatique et *)(* en Automatique. *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)type('a,'e)t=('a,'e)BatPervasives.result=|Okof'a|Error of'eletokv=Okvleterror e=Erroreletvaluer~default=matchrwithOkv->v|Error_->defaultletget_ok=functionOkv->v|Error_->invalid_arg"result is Error _"letget_error=functionErrore->e|Ok_->invalid_arg"result is Ok _"letbindrf=matchrwithOkv->fv|Error_ase->eletjoin=functionOkr->r|Error_ase->eletmap_errorf=functionErrore->Error(fe)|Ok_asv->vletfold~ok~error=functionOkv->okv|Errore->erroreletiterf=functionOkv->fv|Error_->()letiter_errorf=functionErrore->fe|Ok_->()letis_error=functionError_->true|Ok_->falseletequal~ok~errorr0r1=matchr0,r1with|Okv0,Okv1 ->okv0v1|Error e0,Errore1-> errore0e1|_,_->falseletcompare ~ok~errorr0r1=matchr0,r1with|Okv0,Okv1 ->okv0v1|Error e0,Errore1-> errore0e1|Ok_,Error_->-1|Error_,Ok_->1letto_list=functionOkv->[v]|Error_->[]letto_seq=functionOkv->BatSeq.(consvnil)|Error_->BatSeq.nilletcatchfx=tryOk (fx)withe->Erroreletcatch2fxy=tryOk(fxy)withe->Erroreletcatch3fxyz=tryOk(fxyz)withe->Erroreletof_option=function|Somex->Okx|None->Error()letto_option=function|Okx->Somex|Error_->Noneletdefaultdef=function|Okx-> x|Error_->defletmapf=function|Errore->Errore|Okv->Ok(fv)(*$T map map succ(Error (-1)) = (Error (-1))
map succ (Error 0) = (Error 0)
map succ (Ok 3) = (Ok 4)
*)letmap_bothfg=function|Errore->Error(ge)|Okv->Ok(fv)(*$T map_both
map_both succ pred (Error (-1)) = (Error (-2))
map_both succ pred (Error 0) = (Error (-1))
map_both succ pred (Error 1) = (Error 0)
map_both succ pred (Ok (-1)) = (Ok 0)
map_both succ pred (Ok 0) = (Ok 1)
map_both succ pred (Ok 1) = (Ok 2)
*)letmap_defaultdeff=function|Okx->fx|Error_->defletis_ok=functionOk_->true|Error_->falseletis_bad=functionError_->true|Ok_->falseletis_exne=functionErrorexn->exn=e|Ok_->falseletget=functionOkx->x|Errore->raiseeletprintprint_valoc=function|Okx->BatPrintf.fprintfoc"Ok(%a)"print_valx|Errore->BatPrintf.fprintfoc"Error(%a)"BatPrintexc.printemoduleMonad=structletbindmk=matchmwith|Okx->kx|Error_ase->eletreturnx=Okxlet(>>=)=bindendmoduleInfix=structlet(>>=)=Monad.bindend