12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607moduleComparator=structtype('a,'identity)t=('a,'identity)Base.Comparator.ttype('a,'identity)comparator=('a,'identity)tmoduletypeT=sigtypenonrectvalcompare:t->t->intendmoduletypeS=sigtypenonrecttypeidentityvalcomparator:(t,identity)comparatorendtype('a,'identity)s=(moduleSwithtypeidentity='identityandtypet='a)(* let ofBaseComparator (baseComparator : ('a, 'id) Base.Map.comparator) : ('a, 'id) s = Obj.magic baseComparator *)lettoBaseComparator(comparator:('a,'id)s):('a,'id)Base.Map.comparator=Obj.magiccomparatorletopaque_=Base.Sexp.Atom"<opaque>"letmake~compare=Obj.magic(Base.Comparator.make~compare~sexp_of_t:opaque)moduleMake(M:T)=structmoduleBaseComparator=Base.Comparator.Make(structincludeMletcompare=M.compareletsexp_of_t=opaqueend)includeBaseComparatortypeidentity=BaseComparator.comparator_witnessletcomparator=BaseComparator.comparatorendendmoduleBool=structtypet=boolexternal(&&):bool->bool->bool="%sequand"external(||):bool->bool->bool="%sequor"letxorab=(a&¬b)||((nota)&&b)letnot=notletnegateft=not(ft)letequal=(=)letcompare=compareletofInti=matchiwith0->Somefalse|1->Sometrue|_->NoneletofStringstring=matchstringwith"false"->Somefalse|"true"->Sometrue|_->NonelettoString=functiontrue->"true"|false->"false"lettoIntt=matchtwithtrue->1|false->0endmoduleChar=structtypet=charlettoCode(c:char)=(Base.Char.to_intc:int)letofCode(i:int)=(if0<=i&&i<=255thenSome(Char.chri)elseNone:charoption)lettoString=Base.Char.to_stringletofString(str:string)=(matchString.lengthstrwith1->Somestr.[0]|_->None:charoption)lettoDigitchar=matchcharwith'0'..'9'->Some(toCodechar-toCode'0')|_->NonelettoLowercase=Base.Char.lowercaselettoUppercase=Base.Char.uppercaseletisLowercase=Base.Char.is_lowercaseletisUppercase=Base.Char.is_uppercaseletisLetter=Base.Char.is_alphaletisDigit=Base.Char.is_digitletisAlphanumeric=Base.Char.is_alphanumletisPrintable=Base.Char.is_printletisWhitespace=Base.Char.is_whitespaceletequal:char->char->bool=(=)letcompare=comparetypeidentity=Base.Char.comparator_witnessletcomparator=Base.Char.comparatorendmoduleFun=structexternalidentity:'a->'a="%identity"externalignore:_->unit="%ignore"letconstanta_=aletsequenceab=ignorea;bletflipfab=fbaletapplyfa=falet(<|)fa=faexternalpipe:'a->('a->'b)->'b="%revapply"external(|>):'a->('a->'b)->'b="%revapply"letcomposefga=f(ga)let(<<)=composeletcomposeRightfga=g(fa)let(>>)=composeRightlettapvalue~f=fvalue;valueletrectimesn~f=ifn<=0then()else(f();times(n-1)~f)letforeverf=trywhiletruedof()done;failwith"[while true] managed to return, you are in trouble now."withexn->exnletcurry(f:'a*'b->'c)(a:'a)(b:'b)=(f(a,b):'c)letuncurry(f:'a->'b->'c)((a,b):'a*'b)=(fab:'c)letcurry3(f:'a*'b*'c->'d)(a:'a)(b:'b)(c:'c)=(f(a,b,c):'d)letuncurry3(f:'a->'b->'c->'d)((a,b,c):'a*'b*'c)=(fabc:'d)endmoduleContainer=structmoduletypeSum=sigtypetvalzero:tvaladd:t->t->tendendmoduleTuple=structtype('a,'b)t='a*'bletmakeab=(a,b)letofArray=function[|a;b|]->Some(a,b)|_->NoneletofList=functiona::b::_->Some(a,b)|_->Noneletfirst(a,_)=aletsecond(_,b)=bletmapFirst(a,b)~f=(fa,b)letmapSecond(a,b)~f=(a,fb)letmapEach(a,b)~f~g=(fa,gb)letmapAll(a,b)~f=(fa,fb)letswap(a,b)=(b,a)lettoArray(a,b)=[|a;b|]lettoList(a,b)=[a;b]letequalequalFirstequalSecond(a,b)(a',b')=equalFirstaa'&&equalSecondbb'letcomparecompareFirstcompareSecond(a,b)(a',b')=matchcompareFirstaa'with0->compareSecondbb'|result->resultendmoduleTuple3=structtype('a,'b,'c)t='a*'b*'cletmakeabc=(a,b,c)letofArray=function[|a;b;c|]->Some(a,b,c)|_->NoneletofList=functiona::b::c::_->Some(a,b,c)|_->Noneletfirst((a,_,_):'a*'b*'c)=(a:'a)letsecond((_,b,_):'a*'b*'c)=(b:'b)letthird((_,_,c):'a*'b*'c)=(c:'c)letinitial((a,b,_):'a*'b*'c)=((a,b):'a*'b)lettail((_,b,c):'a*'b*'c)=((b,c):'b*'c)letmapFirst(a,b,c)~f=(fa,b,c)letmapSecond(a,b,c)~f=(a,fb,c)letmapThird(a,b,c)~f=(a,b,fc)letmapEach(a,b,c)~f~g~h=(fa,gb,hc)letmapAll(a1,a2,a3)~f=(fa1,fa2,fa3)letrotateLeft((a,b,c):'a*'b*'c)=((b,c,a):'b*'c*'a)letrotateRight((a,b,c):'a*'b*'c)=((c,a,b):'c*'a*'b)lettoArray(a,b,c)=[|a;b;c|]lettoList((a,b,c):'a*'a*'a)=([a;b;c]:'alist)letequalequalFirstequalSecondequalThird(a,b,c)(a',b',c')=equalFirstaa'&&equalSecondbb'&&equalThirdcc'letcomparecompareFirstcompareSecondcompareThird(a,b,c)(a',b',c')=matchcompareFirstaa'with|0->(matchcompareSecondbb'with0->compareThirdcc'|result->result)|result->resultendmoduleOption=structtype'at='aoptionletsomea=SomealetisSome=Option.is_someletisNone=Option.is_noneletand_tatb=matchisSometawithtrue->tb|false->Noneletor_tatb=matchisSometawithtrue->ta|false->tbletflatMapt~f=matchtwithSomex->fx|None->Noneletflatten=Option.joinletbothab=match(a,b)with(Somea,Someb)->Some(a,b)|_->Noneletmapt~f=Option.mapftletmap2(ta:'at)(tb:'bt)~(f:'a->'b->'c)=(match(ta,tb)with(Somea,Someb)->Some(fab)|_->None:'ct)letgett~default=matchtwithNone->default|Somevalue->valuelet(|?)tdefault=gett~defaultletgetUnsafex=matchxwith|None->raise(Invalid_argument"Option.getUnsafe called with None")|Somex->xletforEacht~f=Option.iterftlettoArrayt=matchtwithNone->[||]|Somevalue->[|value|]lettoListt=matchtwithNone->[]|Somevalue->[value]moduleInfix=structlet(>>=)tf=flatMapt~flet(>>|)tf=mapt~fendletequalequalab=match(a,b)with|(None,None)->true|(Somea',Someb')->equala'b'|_->falseletcomparecompareab=match(a,b)with|(None,None)->0|(Somea',Someb')->comparea'b'|(None,Some_)->-1|(Some_,None)->1endmoduleResult=structtype('ok,'error)t=('ok,'error)Result.tletok=Result.okleterror=Result.errorletofOptionma~error=matchmawithNone->Result.Errorerror|Someright->Result.OkrightletisOk=Result.is_okletisError=Result.is_errorletbothab=match(a,b)with|(Oka',Okb')->Ok(a',b')|(Errora',_)->Errora'|(_,Errorb')->Errorb'letflatten=Result.joinletor_ab=matchawithOk_->a|_->bletand_ab=matchawithOk_->b|_->aletget=Result.valuelet(|?)tdefault=gett~defaultletgetUnsafe=Result.get_okletgetErrort~default=matchtwithOk_->default|Errorerror->errorletmapt~f=Result.mapftletmap2ab~f=match(a,b)with|(Oka,Okb)->Ok(fab)|(Errora,_)->Errora|(_,Errorb)->ErrorbletmapErrort~f=matchtwithErrorerror->Error(ferror)|Okvalue->Okvalueletvaluest=Base.List.fold_rightt~f:(map2~f:(funab->a::b))~init:(Ok[])lettoOptionr=matchrwithOkv->Somev|Error_->NoneletflatMapt~f=Result.bindtfletattemptf=matchf()withvalue->Okvalue|exceptionerror->Errorerrorlettransposet=matchtwith|Errorerror->Some(Errorerror)|OkNone->None|Ok(Somevalue)->Some(Okvalue)letforEacht~f=matchtwithOka->fa|_->()letequalequalOkequalErrorab=match(a,b)with|(Errora',Errorb')->equalErrora'b'|(Oka',Okb')->equalOka'b'|_->falseletcompare(compareOk:'ok->'ok->int)(compareError:'error->'error->int)(a:('ok,'error)t)(b:('ok,'error)t)=(match(a,b)with|(Errora',Errorb')->compareErrora'b'|(Oka',Okb')->compareOka'b'|(Error_,Ok_)->-1|(Ok_,Error_)->1:int)moduleInfix=structlet(>>=)tf=flatMapt~flet(>>|)tf=mapt~fendendmoduleFloat=structtypet=floattyperadians=tletofInt=Base.Float.of_intletofStringstring=trySome(Base.Float.of_stringstring)withInvalid_argument_->Noneletzero=0.0letone=1.0letnan=Base.Float.nanletinfinity=Base.Float.infinityletnegativeInfinity=Base.Float.neg_infinitylete=Base.Float.eulerletpi=Base.Float.piletepsilon=Base.Float.epsilon_floatletmaximumSafeInteger=(2.**52.)-.1.letminimumSafeInteger=(-2.**52.)-.1.letlargestValue=Base.Float.max_finite_valueletsmallestValue=Base.Float.min_positive_normal_valueletadd=(+.)let(+)=(+.)letsubtract=(-.)let(-)=(-.)letmultiply=(*.)let(*)=(*.)letdividen~by=n/.bylet(/)=(/.)letpower~base~exponent=base**exponentlet(**)=(**)letnegate=Base.Float.neglet(~-)=negateletabsolute=Base.Float.absletisIntegert=t=Base.Float.roundtletisSafeIntegert=isIntegert&&t<=maximumSafeIntegerletclampn~lower~upper=ifupper<lowerthenraise(Invalid_argument("~lower:"^Base.Float.to_stringlower^" must be less than or equal to ~upper:"^Base.Float.to_stringupper))elseifBase.Float.is_nanlower||Base.Float.is_nanupper||Base.Float.is_nannthenBase.Float.nanelsemaxlower(minuppern)letinRangen~lower~upper=ifletopenBase.Floatinupper<lowerthenraise(Invalid_argument("~lower:"^Base.Float.to_stringlower^" must be less than or equal to ~upper:"^Base.Float.to_stringupper))elsen>=lower&&n<upperletsquareRoot=sqrtletlogn~base=letopenBase.Floatinlog10n/log10baseletisNaN=Base.Float.is_nanletisInfinite=Base.Float.is_infletisFiniten=(not(isInfiniten))&¬(isNaNn)letmaximumxy=ifisNaNx||isNaNythennanelseify>xthenyelsexletminimumxy=ifisNaNx||isNaNythennanelseify<xthenyelsexlethypotenusexy=squareRoot((x*x)+(y*y))letdegreesn=n*(pi/180.0)letradians=Fun.identityletturnsn=n*2.*piletcos=Base.Float.cosletacos=Base.Float.acosletsin=Base.Float.sinletasin=Base.Float.asinlettan=Base.Float.tanletatan=Base.Float.atanletatan2~y~x=Base.Float.atan2yxtypedirection=[`Zero|`AwayFromZero|`Up|`Down|`Closestof[`Zero|`AwayFromZero|`Up|`Down|`ToEven]]letround?(direction=`Closest`Up)n=matchdirectionwith|(`Up|`Down|`Zero)asdir->Base.Float.roundn~dir|`AwayFromZero->ifn<0.thenBase.Float.roundn~dir:`DownelseBase.Float.roundn~dir:`Up|`Closest`Zero->ifn>0.thenBase.Float.round(n-.0.5)~dir:`UpelseBase.Float.round(n+.0.5)~dir:`Down|`Closest`AwayFromZero->ifn>0.thenBase.Float.round(n+.0.5)~dir:`DownelseBase.Float.round(n-.0.5)~dir:`Up|`Closest`Down->Base.Float.round(n-.0.5)~dir:`Up|`Closest`Up->Base.Float.round_nearestn|`Closest`ToEven->Base.Float.round_nearest_half_to_evennletfloor=Base.Float.round_downletceiling=Base.Float.round_uplettruncate=Base.Float.round_towards_zeroletofPolar(r,theta)=(r*costheta,r*sintheta)lettoPolar(x,y)=(hypotenusexy,atan2~x~y)lettoInt=Base.Float.iround_towards_zerolettoString=Base.Float.to_stringletequal=(=)letcompare=compareendmoduleInt=structtypet=intletofString=int_of_string_optletminimumValue=Base.Int.min_valueletmaximumValue=Base.Int.max_valueletzero=0letone=1letadd=(+)let(+)=(+)letsubtract=(-)let(-)=(-)letmultiply=(*)let(*)=multiplyletdividen~by=n/bylet(/)=(/)let(/.)=Base.Int.(//)letpower~base~exponent=letopenBase.Intinbase**exponentlet(**)=Base.Int.(**)letnegate=(~-)let(~-)=(~-)letremaindern~by=Stdlib.(nmodby)letmodulon~by=((ifn<0then2*(absn)elsen)modby)let(mod)nby=modulon~byletmaximum=Base.Int.maxletminimum=Base.Int.minletabsoluten=Base.Int.absnletisEvenn=remaindern~by:2=0letisOddn=remaindern~by:2<>0letclampn~lower~upper=ifupper<lowerthenraise(Invalid_argument("~lower:"^Base.Int.to_stringlower^" must be less than or equal to ~upper:"^Base.Int.to_stringupper))elsemaxlower(minuppern)letinRangen~lower~upper=ifupper<lowerthenraise(Invalid_argument("~lower:"^Base.Int.to_stringlower^" must be less than or equal to ~upper:"^Base.Int.to_stringupper))elsen>=lower&&n<upperlettoFloat=Base.Int.to_floatlettoString=Base.Int.to_stringletequal=(=)letcompare=comparetypeidentity=Base.Int.comparator_witnessletcomparator=Base.Int.comparatorendmoduleInteger=structtypet=Z.tincludeComparator.Make(structtypenonrect=tletcompare=Z.compareend)letofInt=Z.of_intletofInt64=Z.of_int64letofFloatfloat=matchZ.of_floatfloatwith|integer->Someinteger|exceptionZ.Overflow->NoneletofStringstring=matchZ.of_stringstringwithvalue->Somevalue|exception_->Noneletzero=Z.zeroletone=Z.oneletisEvent=letopenZintmod~$2=zeroletisOddt=letopenZintmod~$2<>zeroletadd=Z.addlet(+)=Z.(+)letsubtract=Z.sublet(-)=subtractletmultiply=Z.mullet(*)=multiplyletdivide=Z.divlet(/)=divideletdividen~by=dividenbyletnegate=Z.negletmodulo(n:t)~(by:t):t=(Z.remnby)let(mod)(n:t)(by:t):t=(Z.remnby)letremainder(n:t)~(by:t)=(Z.remnby)let(**)=Z.(**)letpower?modulo~(base:t)~(exponent:int)=matchmodulowith|Somemodulus->Z.powmbase(ofIntexponent)modulus|None->Z.powbaseexponentletmaximumab=ifa<bthenbelsealetminimumab=ifa>bthenbelsealetabsoluten=ifn<zerothennegatenelsenletclampn~lower~upper=ifupper<lowerthenraise(Invalid_argument"~lower must be less than or equal to ~upper")elsemaxlower(minuppern)letinRangen~lower~upper=ifupper<lowerthenraise(Invalid_argument"~lower must be less than or equal to ~upper")elsen>=lower&&n<upperlettoIntt=ift>ofIntInt.maximumValuethenNoneelseSome(Z.to_intt)lettoInt64t=ift>ofInt64Int64.max_intthenNoneelseSome(Z.to_int64t)lettoFloat=Z.to_floatlettoString=Z.to_stringletequal=Z.equalletcompare=Z.compareendmoduleString=structtypet=stringletinitializelength~f=Base.List.initlength~f|>Base.String.of_char_listletrepeatt~count=Base.List.initcount~f:(fun_->t)|>Base.String.concatletofArraycharacters=letopenBaseinArray.to_listcharacters|>String.of_char_listletofList=Base.String.of_char_listletlength=String.lengthletisEmptyt=lengtht=0letget=Base.String.getletgetAta~index=ifindex>=0&&index<lengthathenSome(Base.String.getaindex)elseNonelet(.?[])(string:string)(index:int):charoption=getAtstring~indexletuncons(s:string)=(matchswith|""->None|s->Some(s.[0],String.subs1(String.lengths-1)):(char*string)option)letdropLeft(s:string)~(count:int)=(Base.String.drop_prefixscount:string)letdropRight(s:string)~(count:int)=(Base.String.drop_suffixscount:string)letsplitt~(on:string)=(Str.split(Str.regexp_stringon)t:stringlist)letstartsWitht~prefix=Base.String.is_prefix~prefixtletendsWitht~suffix=Base.String.is_suffix~suffixtlettoLowercase(s:string)=(String.lowercase_asciis:string)lettoUppercase(s:string)=(String.uppercase_asciis:string)letuncapitalize(s:string)=(String.uncapitalize_asciis:string)letcapitalize(s:string)=(String.capitalize_asciis:string)letisCapitalized(s:string)=(s=String.capitalize_asciis:bool)letincludest~substring=(Base.String.is_substringt~substring:bool)letreverse=Base.String.revletofChar=Base.String.of_charletslice?(to_=0)str~from=String.substrfrom(to_-from)letinsertAtt~(index:int)~(value:string)=(letlength=lengthtinletstartCount=indexinletendCount=length-indexinletstart=dropRight~count:endCounttinletend_=dropLeft~count:startCounttinString.concat""[start;value;end_]:string)lettoArraystring=Base.String.to_liststring|>Array.of_listlettoList=Base.String.to_listlettrimstring=Base.String.stripstringlettrimLeftstring=Base.String.lstripstringlettrimRightstring=Base.String.rstripstringletpadLeftstringtargetLength~with_=if(length(string)>=targetLength)thenstringelse(letpaddingLength=targetLength-length(string)inletcount=paddingLength/length(with_)inletpadding=slice(repeatwith_~count)~from:0~to_:paddingLengthinpadding^string)letpadRightstringtargetLength~with_=if(length(string)>=targetLength)thenstringelse(letpaddingLength=targetLength-length(string)inletcount=paddingLength/length(with_)inletpadding=slice(repeatwith_~count)~from:0~to_:paddingLengthinstring^padding)letforEach=Base.String.iterletfolds~initial~f=Base.String.folds~init:initial~fletequal=Base.String.equalletcompare=Base.String.comparetypeidentity=Base.String.comparator_witnessletcomparator=Base.String.comparatorendmoduleSet=structtype('a,'id)t=('a,'id)Base.Set.tmoduleOf(M:Comparator.S)=structtypenonrec'valuet=(M.t,M.identity)tendletemptycomparator=(Base.Set.empty(Comparator.toBaseComparatorcomparator))letsingleton(comparator:('a,'identity)Comparator.s)(element:'a):('a,'identity)t=(Base.Set.of_list(Comparator.toBaseComparatorcomparator)[element])letofArray(comparator:('a,'identity)Comparator.s)(elements:'aarray):('a,'identity)t=(Base.Set.of_list(Comparator.toBaseComparatorcomparator)(Array.to_listelements))letofList(comparator:('a,'identity)Comparator.s)(elements:'alist):('a,'identity)t=(Base.Set.of_list(Comparator.toBaseComparatorcomparator)elements)letlength=Base.Set.lengthletisEmpty=Base.Set.is_emptyletincludes=Base.Set.memlet(.?{})(set:('element,_)t)(element:'element):bool=includessetelementletadd=Base.Set.addletremove=Base.Set.removeletdifference=Base.Set.diffletintersection=Base.Set.interletunion=Base.Set.unionletfilter=Base.Set.filterletpartition=Base.Set.partition_tfletfind=Base.Set.findletall=Base.Set.for_allletany=Base.Set.existsletforEach=Base.Set.iterletfolds~initial~f=Base.Set.folds~init:initial~flettoArray=Base.Set.to_arraylettoList=Base.Set.to_listmodulePoly=structtypeidentity=Base.Comparator.Poly.comparator_witnesstypenonrec'at=('a,identity)tletempty()=Base.Set.Poly.emptyletsingleton=Base.Set.Poly.singletonletofArray=Base.Set.Poly.of_arrayletofList=Base.Set.Poly.of_listendmoduleInt=structtypenonrect=(Base.Int.t,Base.Int.comparator_witness)tletempty=Base.Set.empty(moduleBase.Int)letsingleton=Base.Set.singleton(moduleBase.Int)letofArray=Base.Set.of_array(moduleBase.Int)letofList=Base.Set.of_list(moduleBase.Int)endmoduleString=structtypenonrect=(String.t,Base.String.comparator_witness)tletempty=Base.Set.empty(moduleBase.String)letsingleton=Base.Set.singleton(moduleBase.String)letofArray=Base.Set.of_array(moduleBase.String)letofList=Base.Set.of_list(moduleBase.String)endendmoduleMap=structtype('key,'value,'id)t=('key,'value,'id)Base.Map.tmoduleOf(M:Comparator.S)=structtypenonrec'valuet=(M.t,'value,M.identity)tendletkeepLatestOnly=fun_latest->latestletempty(comparator:('key,'identity)Comparator.s):('key,'value,'identity)t=(Base.Map.empty(Comparator.toBaseComparatorcomparator))letsingleton(comparator:('key,'identity)Comparator.s)~key~value:('key,'value,'identity)t=Base.Map.of_alist_reduce(Comparator.toBaseComparatorcomparator)[(key,value)]~f:keepLatestOnlyletofArray(comparator:('key,'identity)Comparator.s)(elements:('key*'value)array):('key,'value,'identity)t=Base.Map.of_alist_reduce(Comparator.toBaseComparatorcomparator)(Array.to_listelements)~f:keepLatestOnlyletofList(comparator:('key,'identity)Comparator.s)(elements:('key*'value)list):('key,'value,'identity)t=Base.Map.of_alist_reduce(Comparator.toBaseComparatorcomparator)elements~f:keepLatestOnlyletisEmpty=Base.Map.is_emptyletincludes=Base.Map.memletlength=Base.Map.lengthletminimumt=Base.Map.min_eltt|>Option.map~f:Tuple.firstletmaximumt=Base.Map.max_eltt|>Option.map~f:Tuple.firstletextentt=Option.both(minimumt)(maximumt)letaddm~key~value=Base.Map.setm~key~data:valuelet(.?{}<-)(map:('key,'value,'id)t)(key:'key)(value:'value):('key,'value,'id)t=addmap~key~valueletremove=Base.Map.removeletget=Base.Map.findlet(.?{})(map:('key,'value,_)t)(key:'key):'valueoption=getmapkeyletupdatem~key~f=Base.Map.changemkey~fletmergem1m2~f=Base.Map.mergem1m2~f:(fun~keydesc->matchdescwith|`Leftv1->fkey(Somev1)None|`Rightv2->fkeyNone(Somev2)|`Both(v1,v2)->fkey(Somev1)(Somev2))letmap=Base.Map.mapletmapIt~f=Base.Map.mapit~f:(fun~key~data->fkeydata)letfilter=Base.Map.filterletpartitionm~f=Base.Map.partition_mapim~f:(fun~key~data->iff~key~value:datathen`Fstdataelse`Snddata)letfindm~f=Base.Map.foldm~init:None~f:(fun~key~datamatching->matchmatchingwith|Some_->matching|None->iff~key~value:datathenSome(key,data)elseNone)letany=Base.Map.existsletall=Base.Map.for_allletforEach=Base.Map.iterletforEachI(map:('key,'value,_)t)~(f:key:'key->value:'value->unit):unit=Base.Map.iterimap~f:(fun~key~data->f~key~value:data)letfoldm~initial~f=Base.Map.foldm~init:initial~f:(fun~key~dataacc->facc~key~value:data)letkeys=Base.Map.keysletvalues=Base.Map.datalettoArraym=Base.Map.to_alistm|>Base.List.to_arraylettoListm=Base.Map.to_alistmmodulePoly=structtypeidentity=Base.Comparator.Poly.comparator_witnesstypenonrec('k,'v)t=('k,'v,identity)tletempty()=Base.Map.Poly.emptyletsingleton~key~value=Base.Map.Poly.singletonkeyvalueletofListl=Base.Map.Poly.of_alist_reducel~f:(fun_curr->curr)letofArraya=Base.Array.to_lista|>ofListendmoduleInt=structtypenonrec'vt=(Int.t,'v,Int.identity)tletempty=Base.Map.empty(moduleBase.Int)letsingleton~key~value=Base.Map.singleton(moduleBase.Int)keyvalueletofListl=Base.Map.of_alist_reduce(moduleBase.Int)l~f:(fun_curr->curr)letofArraya=Base.Array.to_lista|>ofListendmoduleString=structtypenonrec'vt=(String.t,'v,Base.String.comparator_witness)tletempty=Base.Map.empty(moduleBase.String)letsingleton~key~value=Base.Map.singleton(moduleBase.String)keyvalueletofListl=Base.Map.of_alist_reduce(moduleBase.String)l~f:(fun_curr->curr)letofArraya=Base.Array.to_lista|>ofListendendmoduleArray=structtype'at='aarrayletsingleton(a:'a)=([|a|]:'aarray)letclone=Base.Array.copyletinitialize(length:int)~(f:int->'a)=iflength<=0then[||]elseBase.Array.initlength~fletrepeatelement~length=initializelength~f:(Fun.constantelement)letrange?(from=0)(to_:int)=(Base.Array.init(max0(to_-from))~f:(funi->i+from):intarray)letofList=Base.List.to_arrayletlength(a:'aarray)=(Base.Array.lengtha:int)letisEmpty(a:'aarray)=(lengtha=0:bool)letfirstt=iflengtht<1thenNoneelseSomet.(0)letlastt=iflengtht<1thenNoneelseSomet.(lengtht-1)letget=Base.Array.getletgetAta~index=ifindex>=0&&index<lengthathenSome(Base.Array.getaindex)elseNonelet(.?())(array:'elementt)(index:int):'elementoption=getAtarray~indexletset=Base.Array.setletsetAtt~index~value=settindexvalueletfilter=Base.Array.filterletsum(typea)t(moduleM:Container.Sumwithtypet=a)=Base.Array.foldt~init:M.zero~f:M.addletfilterMap=Base.Array.filter_mapletflatMap=Base.Array.concat_mapletfolda~initial~f=Base.Array.folda~init:initial~fletfoldRighta~initial~f=Base.Array.fold_righta~init:initial~f:(Fun.flipf)letcountt~f=foldt~initial:0~f:(funtotalelement->total+matchfelementwithtrue->1|false->0)letswap=Base.Array.swapletfind=Base.Array.findletfindIndex=Base.Array.findiletmap=Base.Array.mapletmapI=Base.Array.mapiletmap2(a:'aarray)(b:'barray)~(f:'a->'b->'c)=(letminLength=min(lengtha)(lengthb)inBase.Array.initminLength~f:(funi->fa.(i)b.(i)):'carray)letzip=map2~f:Tuple.makeletmap3(arrayA:'aarray)(arrayB:'barray)(arrayC:'carray)~(f:'a->'b->'c->'d)=letminLength=Base.min(lengtharrayA)(Base.min(lengtharrayC)(lengtharrayB))inBase.Array.initminLength~f:(funi->farrayA.(i)arrayB.(i)arrayC.(i))letpartition=Base.Array.partition_tfletsplitAta~index=(Base.Array.initindex~f:(funi->a.(i)),Base.Array.init(lengtha-1)~f:(funi->a.(index+i)))letsplitWhena~f=matchfindIndexa~f:(fun_indexelement->felement)with|None->(a,[||])|Some(index,_)->splitAta~indexletunzip=Base.Array.unzipletappend(a:'aarray)(a':'aarray)=(Base.Array.appendaa':'aarray)letflatten(al:'aarrayarray)=(Base.Array.concat(Base.Array.to_listal):'aarray)letinterspersearray~sep=Base.Array.init(max0((Array.lengtharray*2)-1))~f:(funi->ifimod2<>0thensepelsearray.(i/2))letany=Base.Array.existsletall=Base.Array.for_allletincludes=Base.Array.memletvaluest=foldt~initial:[]~f:(funresultselement->matchelementwithNone->results|Somevalue->value::results)|>ofListletjoint~sep=Stdlib.String.concatsep(Array.to_listt)letslice?to_array~from=letdefaultTo=matchto_withNone->lengtharray|Somei->iinletsliceFrom=iffrom>=0thenmin(lengtharray)fromelsemax0(min(lengtharray)(lengtharray+from))inletsliceTo=ifdefaultTo>=0thenmin(lengtharray)defaultToelsemax0(min(lengtharray)(lengtharray+defaultTo))inifsliceFrom>=sliceTothen[||]elseBase.Array.init(sliceTo-sliceFrom)~f:(funi->array.(i+sliceFrom))letsliding?(step=1)a~size=letn=Array.lengthainifsize>nthen[||]elseinitialize(1+((n-size)/step))~f:(funi->initializesize~f:(funj->a.((i*step)+j)))letchunksOft~size=slidingt~step:size~sizeletmaximum=Base.Array.max_eltletminimum=Base.Array.min_eltletextentt~compare=foldt~initial:None~f:(funrangeelement->matchrangewith|None->Some(element,element)|Some(min,max)->Some((matchcompareelementmin<0with|true->element|false->min),matchcompareelementmax>0with|true->element|false->max))letsortt=Base.Array.sorttletreverse=Base.Array.rev_inplaceletforEacha~f=Base.Array.itera~fletforEachIa~f=Base.Array.iteria~fletgroupBytcomparator~f=foldt~initial:(Map.emptycomparator)~f:(funmapelement->(letkey=felementinMap.updatemap~key~f:(function|None->Some[element]|Someelements->Some(element::elements))))lettoList(a:'aarray)=(Base.Array.to_lista:'alist)lettoIndexedLista=Base.Array.fold_righta~init:(lengtha-1,[])~f:(funx(i,acc)->(i-1,(i,x)::acc))|>Base.sndletequalequalab=iflengtha<>lengthbthenfalseelseiflengtha=0thentrueelse(letrecloopindex=ifindex=lengthathentrueelseequala.(index)b.(index)&&loop(index+1)inloop0)letcomparecompareab=matchInt.compare(lengtha)(lengthb)with|0->iflengtha==0then0else(letrecloopindex=ifindex=lengthathen0else(matchcomparea.(index)b.(index)with|0->loop(index+1)|result->result)inloop0)|result->resultendmoduleList=structtype'at='alistletempty=[]letsingleton=Base.List.returnletrepeatelement~times=Base.List.inittimes~f:(fun_->element)letrecrange?(from=0)to_=iffrom>=to_then[]elsefrom::range~from:(from+1)to_letinitialize=Base.List.initletsum(typea)(a:at)(moduleM:Container.Sumwithtypet=a)=(Base.List.folda~init:M.zero~f:M.add:a)letofArray=Base.Array.to_listletisEmpty(l:'alist)=(l=[]:bool)lethead=Base.List.hdlettail=Base.List.tlletconslistelement=element::listlettaket~count=Base.List.taketcountlettakeWhile(l:'alist)~(f:'a->bool)=(letrectakeWhileHelperaccl'=matchl'with|[]->Base.List.revacc|x::rest->iffxthentakeWhileHelper(x::acc)restelseBase.List.revaccintakeWhileHelper[]l:'alist)letdropt~count=Base.List.droptcountletrecdropWhile(l:'alist)~(f:'a->bool)=(matchlwith|[]->[]|x::rest->iffxthendropWhile~frestelsel:'alist)letinitial(l:'alist)=(matchBase.List.revlwith|[]->None|_::rest->Some(Base.List.revrest):'alistoption)letreclast(l:'alist)=(matchlwith[]->None|[a]->Somea|_::tail->lasttail:'aoption)letappend(l1:'alist)(l2:'alist)=(Base.List.appendl1l2:'alist)letflatten=Base.List.concatletmap2=Base.List.map2_exnletmap3=Base.List.map3_exnletreverse(l:'alist)=(Base.List.revl:'alist)letmap=Base.List.mapletmapI=Base.List.mapiletflatMap=Base.List.concat_mapletincludes=Base.List.memletfind=Base.List.findletfindIndex=Base.List.findiletany=Base.List.existsletall=Base.List.for_allletgetAt(l:'alist)~(index:int)=(Base.List.nthlindex:'aoption)letfilterMap=Base.List.filter_mapletfiltert~f=Base.List.filtert~fletfilterIt~f=Base.List.filterit~fletpartition=Base.List.partition_tfletfoldt~initial~f=Base.List.foldt~init:initial~fletcount=Base.List.countletfoldRightt~initial~f=Base.List.fold_rightt~init:initial~f:(Fun.flipf)letsplitAt(l:'alist)~(index:int)=((take~count:indexl,drop~count:indexl):'alist*'alist)letsplitWhen(l:'alist)~(f:'a->bool)=(matchfindIndex~f:(fun_element->felement)lwith|Some(index,_)->splitAt~indexl|None->(l,[]):'alist*'alist)letupdateAt(l:'alist)~(index:int)~(f:'a->'a)=(ifindex<0thenlelse(let(front,back)=splitAt~indexlinmatchbackwith[]->l|x::rest->appendfront(fx::rest)):'alist)letlength(l:'alist)=(List.lengthl:int)letremoveAt(l:'alist)~(index:int)=(ifindex<0thenlelse(let(front,back)=splitAt~indexlinmatchtailbackwithNone->l|Somet->appendfrontt):'alist)letminimum=Base.List.min_eltletmaximum=Base.List.max_eltletextentt~compare=foldt~initial:None~f:(funcurrentelement->matchcurrentwith|None->Some(element,element)|Some(min,max)->Some((matchcompareelementmin<0with|true->element|false->min),matchcompareelementmax>0with|true->element|false->max))letinsertAt(t:'alist)~(index:int)~(value:'a)=(let(front,back)=splitAtt~indexinappendfront(value::back):'alist)letziplistAlistB=letrecloopresultxsys=match(xs,ys)with|[],_->result|_,[]->result|x::xs,y::ys->loop((x,y)::result)xsysinloop[]listAlistBletunzip=Base.List.unzipletsliding?(step=1)(t:'at)~(size:int)=(letrectakeAllOrEmptytn(current,count)=ifcount=nthenreversecurrentelse(matchtwith|[]->[]|x::xs->takeAllOrEmptyxsn(x::current,count+1))inletrecloopt=ifisEmptytthen[]else(letsample=takeAllOrEmptytsize([],0)inifisEmptysamplethen[]elsesample::loop(Base.List.droptstep))inloopt:'att)letchunksOft~size=slidingt~step:size~sizeletintersperse(l:'alist)~sep=(matchlwith|[]->[]|[x]->[x]|x::rest->x::foldRightrest~initial:[]~f:(funaccx->sep::x::acc):'alist)letforEachl~f=Base.List.iterl~fletforEachI=Base.List.iterilettoArray=Base.List.to_arrayletgroupWhilet~f=Base.List.groupt~break:fletsort=Base.List.sortletjoint~sep=Stdlib.String.concatseptletgroupBytcomparator~f=foldt~initial:(Map.emptycomparator)~f:(funmapelement->(letkey=felementinMap.updatemap~key~f:(function|None->Some[element]|Someelements->Some(element::elements))))letrecequalequalElementab=match(a,b)with|([],[])->true|(x::xs,y::ys)->equalElementxy&&equalequalElementxsys|_->falseletreccomparecompareElementab=match(a,b)with|([],[])->0|([],_)->-1|(_,[])->1|(x::xs,y::ys)->(matchcompareElementxywith|0->comparecompareElementxsys|result->result)endlet()=()