123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2019,2020 DaiLambda, Inc. <contact@dailambda.jp> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)includeMonad.Make1(structtype'at=Random.State.t->'aletbind(gen:'at)(f:'a->'bt)st=f(genst)stletreturnx=fun_st->xend)moduleRS=Random.Stateletintsz:intt=funst->RS.intstszletstringintchar:stringt=funst->letl=intstinString.initl(fun_->charst)letlistintn:'alistt=funst->letl=intstinList.initl(fun_->nst)letchar:chart=int256>>|Char.chrletbool:boolt=RS.boolletsegmentint:Segment.tt=listint(bool>>|funb->ifbthenSegment.LeftelseSegment.Right)>>|Segment.of_sidesletchoice:'atlist->'at=funxs->assert(xs<>[]);int(List.lengthxs)>>=funi->List.nthxsiletshufflexs:'alistt=funst->leta=Array.of_listxsinletsize=Array.lengthainfori=0tosize-2doletpos=Random.State.intst(size-i-1)+iinletx=Array.unsafe_getaposinArray.unsafe_setapos@@Array.unsafe_getai;Array.unsafe_setaixdone;Array.to_listaletrecleaf=string(int16)char>>|funs->Node.new_leaf@@Value.of_stringsandbud_none=return(Node.new_budNone)andbud_somedepth=ifdepth=0thenreturn(Node.new_budNone)elseintdepth>>=funn->ifn=0thenreturn(Node.new_budNone)elsebeginletdepth=depth-1inchoice[extenderdepth;internaldepth]>>|funn->beginmatchnwith|Node.View(Node.Leaf_)->assertfalse|Node.View(Node.Bud_)->assertfalse|_->Node.new_bud(Somen)endendandinternaldepth=letf=letdepth=depth-1inifdepth=0thenchoice[bud_none;leaf]elsebeginintdepth>>=funn->ifn=0thenchoice[bud_none;leaf]elsechoice[internaldepth;extenderdepth;bud_somedepth]endinf>>=funn1->f>>|funn2->letn=Node.new_internaln1n2inmatchnwith|View(Internal_)->n|_->assertfalseandextenderdepth=letdepth'=max1depthinintdepth'>>=funl->letl=max1linsegment(returnl)>>=funseg->letdepth=max0(depth-l)in(ifdepth=0thenchoice[bud_none;leaf]elseintdepth>>=funn->ifn=0thenchoice[bud_none;leaf]elsechoice[internaldepth;bud_somedepth])>>|funn->matchNode.new_extendersegnwith|View(Extender_)asn->n|_->assertfalseandrootdepth=bud_somedepth