12345678910111213141516171819202122232425262728293031323334353637383940414243type'at={value:'a;tl:'at;length:int}letrecempty={value=Obj.magic();tl=empty;length=0}letsingletonvalue={value;tl=empty;length=1}[@@inline]lettl_safe{tl;_}=tl[@@inline]lettl_or_retryt=ift!=emptythent.tlelseKcas.Retry.later()[@@inline]letlength{length;_}=length[@@inline]letconsvaluetl={value;tl;length=1+tl.length}[@@inline]lethd_optt=ift!=emptythenSomet.valueelseNone[@@inline]lethd_or_retryt=ift!=emptythent.valueelseKcas.Retry.later()[@@inline]lethd_unsafet=t.value[@@inline]letrecfoldfat=ift==emptythenaelsefoldf(fat.value)t.tlletiterft=fold(fun()x->fx)()t[@@inline]letrecrev_appendttl=ift==emptythentlelserev_appendt.tl@@const.valuetlletrevt=ift.length<=1thentelserev_appendt.tl(singletont.value)letrecprepend_to_seqttl=ift==emptythentlelsefun()->Seq.Cons(t.value,prepend_to_seqt.tltl)letto_seqt=prepend_to_seqtSeq.emptyletof_seq_revxs=Seq.fold_left(funtx->consxt)emptyxsletrev_prepend_to_seqttl=ift.length<=1thenprepend_to_seqttlelselett=ref(`Originalt)infun()->lett=match!twith|`Originalt'->(* This is domain safe as the result is always equivalent. *)lett'=revt'int:=`Reversedt';t'|`Reversedt'->t'inprepend_to_seqttl()