12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394moduleError=structtype'lpayload='loption*Pp.ttype'lt=|Userof'lpayload|Anomalyof'lpayloadletmap~f=function|Usere->User(fe)|Anomalye->Anomaly(fe)endmoduleR=structtype('a,'l)t=|Completedof('a,'lError.t)result|Interrupted(* signal sent, eval didn't complete *)leterrore=Completed(Error(Error.User(None,e)))letmap~f=function|Completed(Result.Okr)->Completed(Result.Ok(fr))|Completed(Result.Errorr)->Completed(Result.Errorr)|Interrupted->Interruptedletmap_error~f=function|Completed(Errore)->Completed(Error(Error.map~fe))|Completed(Okr)->Completed(Okr)|Interrupted->Interruptedletmap_loc~f=letf(loc,msg)=(Option.mapfloc,msg)inmap_error~fend(* Eval and reify exceptions *)leteval_exn~token~fx=matchLimits.limit~token~fxwith|Okres->R.Completed(Okres)|Error_->Vernacstate.invalidate_cache();R.Interrupted|exceptionexn->lete,info=Exninfo.captureexninletloc=Loc.(get_locinfo)inletmsg=CErrors.iprint(e,info)inVernacstate.invalidate_cache();ifCErrors.is_anomalyethenR.Completed(Error(Anomaly(loc,msg)))elseR.Completed(Error(User(loc,msg)))let_bind_exn~fx=matchxwith|R.Interrupted->R.Interrupted|R.Completed(Errore)->R.Completed(Errore)|R.Completed(Okr)->frletfb_queue:Loc.tMessage.tlistref=ref[]moduleE=structtype('a,'l)t={r:('a,'l)R.t;feedback:'lMessage.tlist}leteval~token~fx=letr=eval_exn~token~fxinletfeedback=List.rev!fb_queueinlet()=fb_queue:=[]in{r;feedback}letmap~f{r;feedback}={r=R.map~fr;feedback}letmap_message~f(loc,lvl,msg)=(Option.mapfloc,lvl,msg)letmap_loc~f{r;feedback}={r=R.map_loc~fr;feedback=List.map(map_message~f)feedback}letbind~f{r;feedback}=matchrwith|R.Interrupted->{r=R.Interrupted;feedback}|R.Completed(Errore)->{r=R.Completed(Errore);feedback}|R.Completed(Okr)->let{r;feedback=fb2}=frin{r;feedback=feedback@fb2}letokv={r=Completed(Okv);feedback=[]}leterrorerr={r=R.errorerr;feedback=[]}moduleO=structlet(let+)xf=map~fxlet(let*)xf=bind~fxendend(* Eval with reified exceptions and feedback *)leteval~token~fx=E.eval~token~fx