123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223(**************************************************************************)(* *)(* OCaml *)(* *)(* Pierre Chambart, OCamlPro *)(* Mark Shinwell and Leo White, Jane Street Europe *)(* *)(* Copyright 2013--2016 OCamlPro SAS *)(* Copyright 2014--2016 Jane Street Group LLC *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file ../LICENSE. *)(* *)(**************************************************************************)moduleStdlib_map=MapmoduleStdlib_set=SetmoduletypeThing=sigtypetincludeHashtbl.HashedTypewithtypet:=tincludeMap.OrderedTypewithtypet:=tvaloutput:out_channel->t->unitvalprint:Format.formatter->t->unitendmodulePair(A:Thing)(B:Thing):Thingwithtypet=A.t*B.t=structtypet=A.t*B.tletcompare(a1,b1)(a2,b2)=letc=A.comparea1a2inifc<>0thencelseB.compareb1b2letoutputoc(a,b)=Printf.fprintfoc" (%a, %a)"A.outputaB.outputblethash(a,b)=Hashtbl.hash(A.hasha,B.hashb)letequal(a1,b1)(a2,b2)=A.equala1a2&&B.equalb1b2letprintppf(a,b)=Format.fprintfppf" (%a, @ %a)"A.printaB.printbendmoduleMake_map(T:Thing)=structincludeMap.Make(T)letfilter_mapt~f=fold(funidvmap->matchfidvwith|None->map|Somer->addidrmap)temptyletof_listl=List.fold_left(funmap(id,v)->addidvmap)emptylletdisjoint_union?eqm1m2=merge(funidv1v2->matchv1,v2with|Somev1,Somev2->letok=matcheqwith|None->false|Someeq->eqv1v2inifnotokthenleterr=Format.asprintf"Map.disjoint_union %a"T.printidinMisc.fatal_errorerrelseSomev1|x,None|None,x->x)m1m2letunion_rightm1m2=merge(fun_xy->matchx,ywith|None,None->None|None,Somev|Somev,None|Some_,Somev->Somev)m1m2letunion_leftm1m2=union_rightm2m1letunion_mergefm1m2=letaux_m1m2=matchm1,m2with|None,m|m,None->m|Somem1,Somem2->Some(fm1m2)inmergeauxm1m2letrenamemv=tryfindvmwithNot_found->vletmap_keysfm=of_list(List.map(fun(k,v)->fk,v)(bindingsm))letprintfppfs=leteltsppfs=iter(funidv->Format.fprintfppf"@ (@[%a@ %a@])"T.printidfv)sinFormat.fprintfppf"@[<1>{@[%a@ @]}@]"eltssmoduleT_set=Set.Make(T)letkeysmap=fold(funk_set->T_set.addkset)mapT_set.emptyletof_setfset=T_set.fold(funemap->adde(fe)map)setemptylettranspose_keys_and_datamap=fold(funkvm->addvkm)mapemptyendmoduleMake_set(T:Thing)=structincludeSet.Make(T)letoutputocs=Printf.fprintfoc" ( ";iter(funv->Printf.fprintfoc"%a "T.outputv)s;Printf.fprintfoc")"letprintppfs=leteltsppfs=iter(fune->Format.fprintfppf"@ %a"T.printe)sinFormat.fprintfppf"@[<1>{@[%a@ @]}@]"eltssletto_strings=Format.asprintf"%a"printsletof_listl=matchlwith|[]->empty|[t]->singletont|t::q->List.fold_left(funacce->addeacc)(singletont)qletmapfs=of_list(List.mapf(elementss))endmoduleMake_tbl(T:Thing)=structincludeHashtbl.Make(T)moduleT_map=Make_map(T)letto_listt=fold(funkeydatumelts->(key,datum)::elts)t[]letof_listelts=lett=create42inList.iter(fun(key,datum)->addtkeydatum)elts;tletto_mapv=foldT_map.addvT_map.emptyletof_mapm=lett=create(T_map.cardinalm)inT_map.iter(funkv->addtkv)m;tletmemoizetf=funkey->tryfindtkeywith|Not_found->letr=fkeyinaddtkeyr;rletmaptf=of_map(T_map.mapf(to_mapt))endmoduletypeS=sigtypetmoduleT:Thingwithtypet=tincludeThingwithtypet:=T.tmoduleSet:sigincludeStdlib_set.Swithtypeelt=T.tandtypet=Make_set(T).tvaloutput:out_channel->t->unitvalprint:Format.formatter->t->unitvalto_string:t->stringvalof_list:eltlist->tvalmap:(elt->elt)->t->tendmoduleMap:sigincludeStdlib_map.Swithtypekey=T.tandtype'at='aMake_map(T).tvalfilter_map:'at->f:(key->'a->'boption)->'btvalof_list:(key*'a)list->'atvaldisjoint_union:?eq:('a->'a->bool)->'at->'at->'atvalunion_right:'at->'at->'atvalunion_left:'at->'at->'atvalunion_merge:('a->'a->'a)->'at->'at->'atvalrename:keyt->key->keyvalmap_keys:(key->key)->'at->'atvalkeys:'at->Make_set(T).tvalof_set:(key->'a)->Make_set(T).t->'atvaltranspose_keys_and_data:keyt->keytvalprint:(Format.formatter->'a->unit)->Format.formatter->'at->unitendmoduleTbl:sigincludeHashtbl.Swithtypekey=T.tandtype'at='aHashtbl.Make(T).tvalto_list:'at->(T.t*'a)listvalof_list:(T.t*'a)list->'atvalto_map:'at->'aMake_map(T).tvalof_map:'aMake_map(T).t->'atvalmemoize:'at->(key->'a)->key->'avalmap:'at->('a->'b)->'btendendmoduleMake(T:Thing)=structmoduleT=TincludeTmoduleSet=Make_set(T)moduleMap=Make_map(T)moduleTbl=Make_tbl(T)end