The Scala Universe
Looks up your index, retrieves you from the index.
From "Countable set" - Wikipedia:
In mathematics, a countable set is a set with the same cardinality (number of elements) as some subset of the set of natural numbers. A countable set is either a finite set or a countably infinite set. Whether finite or infinite, the elements of a countable set can always be counted one at a time and—although the counting may never finish—every element of the set is associated with a unique natural number.
This library is for countable types in which the value can represent as a natural number.
For example, each of BigInt
values can associate to a natural number by a function x => if (x < 0) -x * 2 + 1 else x * 2
.
A product of two countable types are also countable.
In fact, we can order (BigInt, BigInt)
values like (0, 0), (-1, 0), (0, -1), (1, 0), ...
.
Of course, all finite types (a number of its values is finite) are countable.
Many types in programming are countable, Int
, Option[A]
for countable A
, Seq[A]
for countable A
, Set[A]
for finite A
,
Map[A, B]
for finite A
and countable B
, to take one example of many.
If a type is countable, we can look up a value's index, and retrieve this value from the index after, and vice versa.
In other words, we can transfer a value via natural number, and can generate a value from randomly generated natural number.
scala> import codes.quine.labo.uchu._
import codes.quine.labo.uchu._
scala> // Looks up an index of `Seq(1, 2, 3)`.
scala> Universe[Seq[Int]].indexOf(Seq(1, 2, 3))
res0: Nat = 71251
scala> // Then, retrieves this from the index.
scala> Universe[Seq[Int]].get(Nat(71251))
res1: Option[Seq[Int]] = Some(List(1, 2, 3))
scala> // Of course we can look up larger value's index.
scala> Universe[Seq[Int]].indexOf(Seq.range(0, 100))
res2: Nat = 489085920819330747617706992050462432524787020462903639367826683892161327203514936193162126531119341779718808675526363930290074638096645538541969230537502743645520989037375527170708920387525963301786542993275032490560112462126267436612936294394948015312351631364130261800769828235969082934496872004225053345301699587962335114787840523089807145741337733384987983239368139280596373057267621098685216046812719787585952451755939941766612930677379483455639616357691737000752434743658221410887759427773155330110878554090939586963803658796097212287471007063413324925107541638106533282276803862996170782821180214406815249610439553724213698148412251312179771242469821542752887828950604561469555956821158446503853913982924884405548189267855545506574188458019253011448393812511667887318497018155287572481304028767630333553227764223158764451374412132910867159512381845945097869902330205630669656904688061680986006682836902223516612620554600095799351794380463669248
scala> // `Map` and `Set` are supported.
scala> Universe[(Map[Int, Int], Set[Int])].indexOf((Map(85 -> 55), Set(10, 26)))
res3: Nat = 23216106441179410470647658464159656414610792054985115537463961128927973858247010658756861297390089084245344093063722974317357738997144399448420626705509328620316428167377930957644275525348984657560340096481318035037782637005455454622212158341851359224446313758084814525985568244829029187128546650416033413369922492404772799818707480773445191857938951176151282634100597566877666688094452493896676653633864715850651452132834082761212165203142043187093209152491983419508103802102751709382237624427023456447381707317656230633949093075170119411756445929906832298861342778781340069717009832274683748265905671492947239120974458728858836038341438310445763508698145517072032809593233577552970532890813019854785927380183242067614292291811348455915260875714051476208179337386262837600079452741945820083631827912983896006406117829257516390126228183239298244901346537275670393423819368695304398233471016927476165185964746292422368367578940737945792691856824480469516786812939080714939631071685590383220697910715366974310078214489846324101819372259930157604757546544410323897891954645214864402909866515717510440712575344994133703929723019075439683178784504467929033320474463615907797827645888812508470749626936043595142691504434028109490541552615280032409736337390297910708922017265418464287819683078563833594001495035180876052272084283829148930871497015006375217528496165509675540585806278707476317083385369482773887423082076818083129236216461490088340686929710493271963475162859378789029531384408330637720270226855254457246037913518535962106119525322489575955656559134353234604860700761539733959695162092566649674939478122886660551517666213966905276094834042557837845234636003890085768902825054336932618172557335880573062330483144295206453946279111524860556973849058785120239336463897411479050159225200491762813401415645978774617938070681480806103481981271412412647467659210739079643937840931709250310003092473563003797679766928943659592790725468835698770621783509490550497442298524810410738774352385612125929554766843591876476237542164682540384346060246003157802599199897106912482325248403182171042112356106222785759436965369890259945274082172272048676323049004299292288380496437088689868907847298973636981479230639065124924502722255796388325324111393031038171273932678009977236153986550772064705746371155907385809557350543037715369414915717709520494193471294899626538395227472779358791610290610001235984138978498999145277830783119074737010603302210946922206326901378315849907282044590715629395942075231620574026772424898890256905636044191628646596002281276972943974804178271003973958958756034371142229491756379030808129048236227177969177042969799951702575928339209190876640120557578904350804331003579485368694986505668955734105256131338557977737162239199722164617713058203966675157656401479550957003980745663405733550226028521492623172426141167467059642166144036552901795472485508653495625511143952434139412808421256356529043724895480252845466758513826994427388820868235567603980058312300352004949463307750851772347027492136437198705352973512007767468366237564166061217480201597963249451303204830580696914394430669235151719480868026083493118377088917998320951207536972644649300249124983835990088
scala> // Also, supports enumerating all possible value of a type.
scala> Universe[Set[Boolean]].enumerate.toList
res4: List[Set[Boolean]] = List(Set(), Set(false), Set(true), Set(false, true))
MIT
2021 (C) TSUYUSATO "MakeNowJust" Kitsune