12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667moduleError=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 *)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~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('a,'l)t={r:('a,'l)R.t;feedback:'lMessage.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:Loc.tMessage.tlistref=ref[](* Eval with reified exceptions and feedback *)leteval~fx=letr=eval_exn~fxinletfeedback=List.rev!fb_queueinlet()=fb_queue:=[]in{E.r;feedback}