123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566moduleError=structtypepayload=Loc.toption*Pp.ttypet=|Userofpayload|Anomalyofpayloadletmap~f=function|Usere->User(fe)|Anomalye->Anomaly(fe)endmoduleR=structtype'at=|Completedof('a,Error.t)result|Interrupted(* signal sent, eval didn't complete *)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))|res->resletmap_loc~f=letf(loc,msg)=(Option.mapfloc,msg)inmap_error~fend(* Eval and reify exceptions *)leteval_exn~fx=tryletres=fxinR.Completed(Okres)with|Sys.Break->R.Interrupted|exn->lete,info=Exninfo.captureexninletloc=Loc.(get_locinfo)inletmsg=CErrors.iprint(e,info)inifCErrors.is_anomalyethenR.Completed(Error(Anomaly(loc,msg)))elseR.Completed(Error(User(loc,msg)))moduleE=structtype'at={r:'aR.t;feedback:Message.tlist}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}endletfb_queue:Message.tlistref=ref[](* Eval with reified exceptions and feedback *)leteval~fx=letr=eval_exn~fxinletfeedback=List.rev!fb_queueinlet()=fb_queue:=[]in{E.r;feedback}