123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372(* The MIT License
Copyright (c) 2008--2024 Jane Street Group, LLC
<opensource-contacts@janestreet.com>
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. *)moduleList=ListLabels(* The module [T] serves to enforce the invariant that all Blang.t values are in
a normal form whereby boolean constants True and False only appear as the
topmost constructor -- in any other position they are simplified away using
laws of boolean algebra.
We also enforce that nested [And]s and [Or]s each lean to the right so that
[eval] doesn't need so much stack space as it would if they leaned to the
left. Thought experiment: compare how [eval] works on right-leaning
[And (a, And (b, And (c, d)))] versus left-leaning
[And (And (And (a, b), c), d)]. The former is the best case and is enforced.
Note: this file deviates from the usual pattern of modules with Stable
interfaces in that the Stable sub-module is not the first thing to be defined
in the module. The reason for this deviation is so that one can convince
oneself of the aforementioned invariant after reading only this small amount
of code. After defining T we then immediately define its Stable interface. *)moduleT:sigtype+'at=private|True|False|Andof'at*'at|Orof'at*'at|Notof'at|Ifof'at*'at*'at|Baseof'avalcompare:('a->'a->int)->'at->'at->intvalequal:('a->'a->bool)->'at->'at->boolvaltrue_:'atvalfalse_:'atvalnot_:'at->'atvalandalso:'at->'at->'atvalorelse:'at->'at->'atvalif_:'at->'at->'at->'atvalbase:'a->'atend=structtype+'at=|True|False|Andof'at*'at|Orof'at*'at|Notof'at|Ifof'at*'at*'at|Baseof'aletreccompare:'a.('a->'a->int)->'at->'at->int=fun_cmp__a->funa__001_->funb__002_->ifStdlib.(==)a__001_b__002_then0else(matcha__001_,b__002_with|True,True->0|True,_->-1|_,True->1|False,False->0|False,_->-1|_,False->1|And(_a__003_,_a__005_),And(_b__004_,_b__006_)->(matchcompare_cmp__a_a__003__b__004_with|0->compare_cmp__a_a__005__b__006_|n->n)|And_,_->-1|_,And_->1|Or(_a__011_,_a__013_),Or(_b__012_,_b__014_)->(matchcompare_cmp__a_a__011__b__012_with|0->compare_cmp__a_a__013__b__014_|n->n)|Or_,_->-1|_,Or_->1|Not_a__019_,Not_b__020_->compare_cmp__a_a__019__b__020_|Not_,_->-1|_,Not_->1|If(_a__023_,_a__025_,_a__027_),If(_b__024_,_b__026_,_b__028_)->(matchcompare_cmp__a_a__023__b__024_with|0->(matchcompare_cmp__a_a__025__b__026_with|0->compare_cmp__a_a__027__b__028_|n->n)|n->n)|If_,_->-1|_,If_->1|Base_a__035_,Base_b__036_->_cmp__a_a__035__b__036_);;letrecequal:'a.('a->'a->bool)->'at->'at->bool=fun_cmp__a->funa__037_->funb__038_->ifStdlib.(==)a__037_b__038_thentrueelse(matcha__037_,b__038_with|True,True->true|True,_->false|_,True->false|False,False->true|False,_->false|_,False->false|And(_a__039_,_a__041_),And(_b__040_,_b__042_)->Stdlib.(&&)(equal_cmp__a_a__039__b__040_)(equal_cmp__a_a__041__b__042_)|And_,_->false|_,And_->false|Or(_a__047_,_a__049_),Or(_b__048_,_b__050_)->Stdlib.(&&)(equal_cmp__a_a__047__b__048_)(equal_cmp__a_a__049__b__050_)|Or_,_->false|_,Or_->false|Not_a__055_,Not_b__056_->equal_cmp__a_a__055__b__056_|Not_,_->false|_,Not_->false|If(_a__059_,_a__061_,_a__063_),If(_b__060_,_b__062_,_b__064_)->Stdlib.(&&)(equal_cmp__a_a__059__b__060_)(Stdlib.(&&)(equal_cmp__a_a__061__b__062_)(equal_cmp__a_a__063__b__064_))|If_,_->false|_,If_->false|Base_a__071_,Base_b__072_->_cmp__a_a__071__b__072_);;lettrue_=Trueletfalse_=Falseletbasev=Basevletnot_=function|True->False|False->True|Nott->t|t->Nott;;letrecandalsot1t2=matcht1,t2with|_,False|False,_->False|other,True|True,other->other|And(t1a,t1b),_->(* nested [And]s lean right -- see comment above *)And(t1a,andalsot1bt2)|_->And(t1,t2);;letrecorelset1t2=matcht1,t2with|_,True|True,_->True|other,False|False,other->other|Or(t1a,t1b),_->(* nested [Or]s lean right -- see comment above *)Or(t1a,orelset1bt2)|_->Or(t1,t2);;letif_abc=matchawith|True->b|False->c|_->(matchb,cwith|True,_->orelseac|_,False->andalsoab|_,True->orelse(not_a)b|False,_->andalso(not_a)c|_->If(a,b,c));;endincludeTmoduleStable=structmoduleV1:sig(* THIS TYPE AND ITS SERIALIZATIONS SHOULD NEVER BE CHANGED - PLEASE SPEAK
WITH ANOTHER DEVELOPER IF YOU NEED MORE DETAIL *)type'at='aT.t=private|True|False|Andof'at*'at|Orof'at*'at|Notof'at|Ifof'at*'at*'at|Baseof'avalcompare:('a->'a->int)->'at->'at->intvalequal:('a->'a->bool)->'at->'at->boolincludeSexpable.S1withtype'at:='at(* the remainder of this signature consists of functions used in the
definitions of sexp conversions that are also useful more generally *)valand_:'atlist->'atvalor_:'atlist->'atvalgather_conjuncts:'at->'atlistvalgather_disjuncts:'at->'atlistend=structtype'at='aT.t=private|True|False|Andof'at*'at|Orof'at*'at|Notof'at|Ifof'at*'at*'at|Baseof'ainclude(T:sigtype'atvalcompare:('a->'a->int)->'at->'at->intvalequal:('a->'a->bool)->'at->'at->boolendwithtype'at:='at)typesexp=Sexp.t=|Atomofstring|Listofsexplist(* cheap import *)(* flatten out nested and's *)letgather_conjunctst=letrecloopacc=function|True::ts->loopaccts|And(t1,t2)::ts->loopacc(t1::t2::ts)|t::ts->loop(t::acc)ts|[]->List.revaccinloop[][t];;(* flatten out nested or's *)letgather_disjunctst=letrecloopacc=function|False::ts->loopaccts|Or(t1,t2)::ts->loopacc(t1::t2::ts)|t::ts->loop(t::acc)ts|[]->List.revaccinloop[][t];;(* [and_] and [or_] use [fold_right] instead of [fold_left] to avoid
quadratic behavior with [andalso] or [orelse], respectively. *)letand_ts=List.fold_rightts~init:true_~f:andalsoletor_ts=List.fold_rightts~init:false_~f:orelseletof_sexp_errorstrsexp=raise(Sexp.Of_sexp_error(Failurestr,sexp))letunarynameargssexp=matchargswith|[x]->x|_->letn=List.lengthargsinof_sexp_error(Printf.sprintf"%s expects one argument, %d found"namen)sexp;;letternarynameargssexp=matchargswith|[x;y;z]->x,y,z|_->letn=List.lengthargsinof_sexp_error(Printf.sprintf"%s expects three arguments, %d found"namen)sexp;;letsexp_of_tsexp_of_valuet=letrecauxt=matchtwith|Basex->sexp_of_valuex|True->Atom"true"|False->Atom"false"|Nott->List[Atom"not";auxt]|If(t1,t2,t3)->List[Atom"if";auxt1;auxt2;auxt3]|And_ast->letts=gather_conjunctstinList(Atom"and"::List.map~f:auxts)|Or_ast->letts=gather_disjunctstinList(Atom"or"::List.map~f:auxts)inauxt;;lett_of_sexpbase_of_sexpsexp=letbasesexp=base(base_of_sexpsexp)inletrecauxsexp=matchsexpwith|Atomkw->(matchString.lowercase_asciikwwith|"true"->true_|"false"->false_|_->basesexp)|List(Atomkw::args)->(matchString.lowercase_asciikwwith|"and"->and_(List.map~f:auxargs)|"or"->or_(List.map~f:auxargs)|"not"->not_(aux(unary"not"argssexp))|"if"->letx,y,z=ternary"if"argssexpinif_(auxx)(auxy)(auxz)|_->basesexp)|_->basesexpinauxsexp;;endendinclude(Stable.V1:moduletypeofStable.V1withtype'at:='at)letconstantb=ifbthentrue_elsefalse_moduletypeConstructors=sigvalbase:'a->'atvaltrue_:_tvalfalse_:_tvalconstant:bool->_tvalnot_:'at->'atvaland_:'atlist->'atvalor_:'atlist->'atvalif_:'at->'at->'at->'atendmoduleO=structincludeTletnot=not_letand_=and_letor_=or_letconstant=constantlet(&&)=andalsolet(||)=orelselet(==>)ab=(nota)||bend(* semantics *)letrecevaltbase_eval=matchtwith|True->true|False->false|And(t1,t2)->evalt1base_eval&&evalt2base_eval|Or(t1,t2)->evalt1base_eval||evalt2base_eval|Nott->not(evaltbase_eval)|If(t1,t2,t3)->ifevalt1base_evalthenevalt2base_evalelseevalt3base_eval|Basex->base_evalx;;