123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947(**************************************************************************
* Copyright (C) 2012-2022
* Dmitri Boulytchev (dboulytchev@math.spbu.ru), St.Petersburg State University
* Universitetskii pr., 28, St.Petersburg, 198504, RUSSIA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* See the GNU Lesser General Public License version 2.1 for more details
* (enclosed in the file COPYING).
**************************************************************************)(** Implementation of transformation for standard types *)openPrintfmoduleFormat=structincludeFormatletpp_print_unitppf()=pp_print_stringppf"()"letpp_print_int32ppfn=fprintfppf"%ld" nletpp_print_int64ppfn=fprintfppf"%Ld" nletpp_print_nativeintppfn=fprintfppf"%nd" nletpp_print_stringfmts=fprintffmt"%S" sendtype('a,'b,'c)t={gcata:'a;plugins:'b;fix:'c}lettransform_gcgcatamake_objinhsubj =letrecobj=lazy(make_objfself)andfselfinhx=gcata(Lazy.forceobj)inhxinfselfinhsubjlet transformbundle=transform_gcbundle.gcataletliftf_=ftypecomparison=LT|EQ|GTlet chain_comparexf=matchxwith|EQ->f()|_->xletcompare_primitivexy=ifx<ythenLTelseifx>ythenGTelseEQletcmp_to_intx=matchxwith|LT->(-1)|GT->1|EQ->0letpoly_tagx=letx=Obj.magicxin(Obj.magic(if Obj.is_blockxthenObj.fieldx0elsex):int)letvari_tagx=ifObj.is_blockxthenObj.tagxelseObj.magicxletcompare_polyxy=compare_primitive(poly_tagx)(poly_tagy)letcompare_varixy=letx,y=Obj.reprx,Obj.repryinletb=Obj.is_blockxin(* TODO: rewrite with built-in structural equality *)matchcompare_primitiveb(Obj.is_blocky)with|EQ->compare_primitive(vari_tagx)(vari_tagy)|_whenb->GT(* block is greater then non-block *)|_->LTletstring_of_strings="\""^String.escapeds^"\""letstring_of_unit_="()"letstring_of_charc=String.make1cletstring_of_int32=Int32.to_stringletstring_of_int64=Int64.to_stringletstring_of_nativeint=Nativeint.to_stringGENERIFY(bool)GENERIFY(int)GENERIFY(string)GENERIFY(float)GENERIFY(char)GENERIFY(unit)GENERIFY(int32)GENERIFY(int64)GENERIFY(nativeint)(* Fixpoint combinator to define recursive transformation without extra
* object allocations *)letfix0ft=letknot=ref(fun_->assertfalse)inletrecurset=f!knottinknot:=recurse;recurse t(** {1 List } *)(* ************************************************************************* *)(** Standart types go there *)type'aplist='alisttype'alist='aplistclassvirtual['ia,'a,'sa,'inh,'self,'syn]list_t=objectmethodvirtualc_Nil:'inh->'self->'synmethodvirtualc_Cons:'inh->'self->'a->'alist->'synendletgcata_listtrinhs=match swith|[]->tr#c_Nilinhs|x::xs->tr#c_Consinhsxxsclass['a,'self]html_list_tfafself=objectinherit[unit,'a,HTML.viewer,unit,'self,HTML.viewer]@listmethodc_Nil__=View.string"[]"methodc_Cons__xxs=HTML.seq([HTML.string"list";HTML.ul@@HTML.seq(List.map(funx->HTML.li@@fa()x)(x::xs))])(* View.concat (fa x) (match xs with [] -> View.empty |xs -> HTML.li (fself () xs)) *)endclass['a,'self]show_list_tfafself=objectinherit[unit,'a,string,unit,'self,string]list_tmethodc_Nil__=""methodc_Cons__xxs=(fa()x)^(matchxswith[]->""|_->"; "^(fself()xs))endclass['a,'self]enum_list_tfafself=objectinherit[unit,'a,int,unit,'self,int]list_tmethodc_Nil__=0methodc_Cons___x _xs=1endclass['a,'self]fmt_list_tfafself=objectinherit['inh,'a,unit,'inh,'self,unit]list_tconstraint'inh=Format.formattermethodc_Nilfmt_=Format.fprintffmt"[]"methodc_Consfmtxs__=Format.fprintf fmt"@[@,[";(* Extra break here to prevent clashing with m4 macro begin *)let ()=match xswith|[]-> ()|x::xs->Format.fprintffmt"@[ %a@]"fax;List.iter(Format.fprintffmt"@[; %a@]"fa)xs;inFormat.fprintffmt"]@]"endclass ['a,'sa,'self,'syn]gmap_list_tfafself=objectconstraint'syn='salistinherit[unit,'a,'sa,unit,'self,'syn]list_tmethodc_Nil__=[]methodc_Cons __xxs=(fa()x)::(fself()xs)endclass['a,'sa,'self,'syn,'env]eval_list_tfafself=objectinherit['env,'a,'sa,'env,'self,'salist]list_tmethodc_Nil__=[]methodc_Consenv_xxs=(faenvx)::(fselfenvxs)endclass['a,'sa,'self,'syn,'env]stateful_list_tfafself=objectinherit['env,'a,'env*'sa,'env,'self,'env*'salist]list_tmethodc_Nilenv_=(env,[])methodc_Consenv0_xxs:'env*'salist=letenv1,h=faenv0xinletenv2,tl=fselfenv1xsinenv2,(h::tl)endclass['a,'syn,'self]foldl_list_tfafself=objectinherit['syn,'a,'syn,'syn,'self,'syn]list_tmethodc_Nils_=smethodc_Conss_xxs=fself(fasx)xsendclass['a,'syn,'self]foldr_list_tfafself=objectinherit['a,'syn,'self]foldl_list_tfafselfmethod!c_Conss_xxs=fa(fselfsxs)xendclass['a,'self]eq_list_tfafself=objectinherit['a,'a,bool,'alist,'self,bool]list_tmethodc_Nilinh_=(inh=[])method c_Consinh_xxs=matchinhwith|y::ys->fayx&&fselfysxs|_->falseendclass['a,'self]compare_list_tfafself=objectinherit['a,'a,comparison,'alist,'self,comparison]list_tmethodc_Nilinh_=matchinhwith|[]->EQ|_->GTmethodc_Consinh_xxs=matchinhwith|[]->LT|(y::ys)->(matchfayxwith|EQ->fselfysxs|c->c)endletlist:(('ia,'a,'sa,'inh,_,'syn)#list_t->'inh->'alist->'syn,<show:('a->string)->'alist->string;html:('a->HTML.viewer)->'alist->HTML.viewer;gmap:('a->'b)->'alist->'blist;enum:('a->int)->'alist->int;fmt:(Format.formatter->'a->unit)->Format.formatter->'alist->unit;eval:('env->'a->'b)->'env->'alist->'blist;stateful:('env->'a->'env *'b)->'env->'alist->'env*'blist;foldl :('c->'a->'c)->'c->'alist->'c;foldr:('c->'a->'c)->'c->'alist->'c;eq:('a->'a->bool)->'alist->'alist->bool;compare:('a->'a->comparison)->'alist->'alist->comparison;>,(('inh->'alist->'syn)->('ia,'a,'sa,'inh,'alist,'syn)list_t)->'inh->'alist->'syn)t={gcata=gcata_list;fix=(fun c->transform_gcgcata_listc);plugins=lettrobjsubj=transform_gcgcata_listobj()subjinlettr1objinhsubj=transform_gc gcata_list objinhsubjinobjectmethodshowfal=sprintf"[%a]" (transform_gcgcata_list(new show_list_t(liftfa)))lmethodhtmlfa=tr(newhtml_list_t(liftfa))methodgmapfa=tr(newgmap_list_t(liftfa))methodenumfa=tr(newenum_list_t(liftfa))methodfmtfainhl=(transform_gcgcata_list (newfmt_list_tfa))inhlmethodstatefulfa=tr1(newstateful_list_tfa)methodevalfa=tr1(neweval_list_tfa)methodeqfa=tr1(neweq_list_tfa)methodcomparefa=tr1(newcompare_list_tfa)methodfoldlfa=tr1(newfoldl_list_tfa)methodfoldrfa=tr1(newfoldr_list_tfa)end}(** {1 Lazy values } *)moduleLazy=structtype('a,'b,'c)t'=('a,'b,'c)tincludeLazyclassvirtual['ia,'a,'sa,'inh,'self,'syn]t_t=objectmethodvirtualt_t:'inh->'at->'synendletgcata_ttrinhsubj=tr#t_tinhsubjletgcata_lazy=gcata_tclass['a,'self]show_t_tfa_fself=objectinherit[unit,'a,string,unit,'self,string]t_tmethodt_tinhsubj=fa()@@Lazy.forcesubjendclass ['a,'self]enum_t_tfa_fself=objectinherit[unit,'a,int,unit,'self,int]t_tmethod t_t__=0endclass['a,'self]html_t_tfa_fself =objectinherit[unit,'a,HTML.viewer,unit,'self,HTML.viewer]t_tmethodt_tinhsubj=fa()@@Lazy.forcesubjendclass ['a,'sa,'self,'syn]gmap_t_tfa_fself=objectconstraint'syn='satinherit[unit,'a,'sa,unit,'self,'syn]t_tmethodt_tinhsubj=lazy(fa()@@Lazy.force subj)endclass['a,'sa,'self,'syn,'env]eval_t_tfa_fself=objectconstraint'syn='satinherit['env,'a,'sa,'env,'self,'syn]t_tmethodt_tenvsubj =lazy(faenv@@Lazy.forcesubj)endclass['a,'sa,'self,'syn,'env]stateful_t_tfa_fself=objectconstraint'syn='satinherit['env,'a,'sa,'env,'self,'env*'syn]t_tmethodt_tenvsubj=let(env1,r)=faenv @@Lazy.forcesubjinenv1,Lazy.from_fun(fun()->r)(* THE SAME AS eval *)endclass['a,'syn,'self]foldl_t_tfa_fself=objectinherit['syn,'a,'syn,'syn,'self,'syn ]t_tmethodt_tinhsubj=fainh @@Lazy.forcesubjendclass ['a,'syn,'self]foldr_t_tfafself=objectinherit['a,'syn,'self]foldl_t_tfselffaendclass ['a,'self]eq_t_tfa_fself=objectinherit['a,'a,bool,'at,'self,bool]t_tmethodt_tinhsubj=fa(Lazy.forceinh)(Lazy.forcesubj)endclass['a,'self]compare_t_tfa_fself=objectinherit ['a,'a,comparison,'at,'self,comparison]t_tmethodt_tinhsubj=fa(Lazy.forceinh)(Lazy.forcesubj)endlett:(('ia,'a,'sa,'inh,_,'syn)#t_t->'inh->'at->'syn,<show:('a->string)->'at->string;enum:('a->int)->'at->int;html:('a->HTML.viewer)->'at->HTML.viewer;gmap:('a->'b)->'at->'bt;eval:('env ->'a->'b)->'env->'at->'bt;stateful:('env->'a->'env*'b)->'env->'at->'env*'bt;foldl :('c->'a->'c)->'c->'at->'c;foldr:('c->'a->'c)->'c->'at->'c;eq:('a->'a->bool)->'at->'at->bool;compare:('a->'a->comparison)->'at->'at->comparison;>,_)t'=letfself__=assertfalsein{gcata=gcata_lazy;fix=(func->transform_gcgcata_lazyc);plugins=objectmethodshowfa=gcata_lazy(newshow_t_tfself(liftfa))()methodenumfa=gcata_lazy(newenum_t_tfself(liftfa))()methodhtmlfa=gcata_lazy(newhtml_t_tfself(liftfa))()methodgmapfa=gcata_lazy(newgmap_t_tfself(liftfa))()methodevalfa=gcata_lazy(neweval_t_tfselffa)methodstatefulfa=gcata_lazy(newstateful_t_tfselffa)methodeqfa=gcata_lazy(neweq_t_tfselffa)methodcomparefa=gcata_lazy(newcompare_t_tfselffa)methodfoldlfa=gcata_lazy(newfoldl_t_tfselffa)methodfoldrfa=gcata_lazy(newfoldr_t_tfselffa)end}end(** {1 Option } *)(* ************************************************************************* *)type'apoption='aoptiontype'aoption='apoptionclassvirtual['ia,'a,'sa,'inh,'self,'syn]option_t=objectmethodvirtualc_None:'inh->'aoption->'synmethod virtualc_Some:'inh->'aoption->'a->'synendletgcata_optiontrinhsubj=matchsubjwith|None->tr#c_Noneinhsubj|Somex->tr#c_Some inhsubjxclass['a,'self]show_option_tfa_fself =objectinherit[unit,'a,string,unit,'self,string]option_tmethodc_None()_="None"methodc_Some()_x=Printf.sprintf"Some (%a)"faxendclass['a,'self]html_option_t fa_fself=objectinherit[unit,'a,HTML.viewer,unit,'self,HTML.viewer]option_tmethodc_None()_=HTML.string"None"methodc_Some()_x=View.concat(HTML.string"Some")(HTML.ul(fa()x))endclass ['a,'self]fmt_option_t fa_fself=objectinherit [Format.formatter,'a,unit,Format.formatter,'self,unit]option_tmethod c_Nonefmt_=Format.fprintf fmt"None"methodc_Somefmt_x=Format.fprintffmt"Some (%a)"faxendclass['a,'sa,'self,'syn]gmap_option_tfa_fself=objectconstraint'syn='saoptioninherit[unit,'a,'sa,unit,'self,'syn]option_tmethodc_None()_=Nonemethodc_Some()_x=Some(fa()x)endclass['a,'sa,'self,'syn,'env]eval_option_tfa_fself=objectinherit['env,'a,'env*'sa,'env,'self,'saoption]option_tmethodc_None__=Nonemethodc_Someenv_x=Some(faenvx)endclass ['a,'sa,'self,'syn,'env]stateful_option_tfa_fself=objectconstraint'syn='saoptioninherit['env,'a,'sa,'env,'self,'env*'syn]option_tmethod c_Noneenv _=(env,None)methodc_Someenv_x=letenv1,r=faenvxin(env1,Somer)endclass ['a,'syn,'self]foldl_option_tfa_fself=objectinherit['syn,'a,'syn,'syn,'self,'syn]option_tmethod c_Nones_=smethodc_Somes_x=fasxendclass['a,'syn,'self]foldr_option_tfa_fself=objectinherit['a,'syn,'self]foldl_option_tfa_fselfendclass['a,'self]eq_option_tfa_fself=objectinherit['a,'a,bool,'aoption,'self,bool]option_tmethodc_Noneinh_=(inh=None)methodc_Someinh_x=matchinhwith|Somey->fayx|_->falseendclass['a,'self]compare_option_tfa_fself=objectinherit['a,'a,comparison,'aoption,'self,comparison]option_tmethodc_Noneinh_=matchinhwith|None->EQ|_->GTmethodc_Someinh_x=matchinhwith|None->LT|Somey->fayxendletoption:(('ia,'a,'sa,'inh,_,'syn)#option_t->'inh->'aoption->'syn,<show:('a->string)->'aoption->string;html:('a->HTML.viewer)->'aoption->HTML.viewer;gmap:('a->'b)->'aoption->'boption;fmt:(Format.formatter->'a->unit)->Format.formatter->'aoption->unit;stateful:('env->'a->'env*'b)->'env ->'aoption->'env*'boption;eval:('env->'a->'b)->'env->'aoption->'boption;foldl:('c->'a->'c)->'c->'aoption ->'c;foldr:('c->'a->'c)->'c->'aoption->'c;eq:('a->'a->bool)->'aoption->'aoption->bool;compare:('a->'a->comparison)->'aoption->'aoption->comparison;>,_)t={gcata=gcata_option;fix=(func->transform_gcgcata_optionc);plugins=objectmethodshowfa=transform_gcgcata_option(newshow_option_t(liftfa))()methodhtmlfa =transform_gcgcata_option(newhtml_option_t(liftfa))()methodgmapfa=transform_gcgcata_option(newgmap_option_t(liftfa))()methodfmtfa=transform_gcgcata_option(newfmt_option_tfa)methodstatefulfa=transform_gc gcata_option(newstateful_option_tfa)methodevalfa=transform_gc gcata_option(neweval_option_tfa)methodeqfa=transform_gcgcata_option(neweq_option_tfa)methodcompare fa=transform_gcgcata_option(newcompare_option_tfa)methodfoldlfa =transform_gcgcata_option(newfoldl_option_tfa)methodfoldrfa=transform_gcgcata_option(newfoldr_option_tfa)end}(* Pairs and other stuff without explicitstructure *)(*******************************************************************************)(** Arrow *)type('a,'b)arrow='a->'bletgcata_arrowtrinharr=tr#c_Arrowinharrclassvirtual['ia,'a,'sa,'ib,'b,'sb,'inh,'self,'syn]arrow_t =objectmethodvirtualc_Arrow:'inh->('a,'b)arrow->'synendclass['a,'b,'self]show_arrow_t fafb_=objectinherit [unit,'a,string,unit,'b,string,unit,'self,string]arrow_tmethodc_Arrow()_=Printf.sprintf"<function>"endclass['a,'b,'self]enum_arrow_tfafb_=objectinherit [unit,'a,int,unit,'b,int,unit,'self,int]arrow_tmethodc_Arrow()_=failwith"enumerating of arrows is not supported"endclass['a,'b,'self]fmt_arrow_tfafb_=objectinherit['inh,'a,unit,'inh,'b,unit,'inh,'self,unit]arrow_tconstraint'inh=Format.formattermethodc_Arrowfmt_=Format.fprintffmt"<function>"endclass['a,'b,'self]html_arrow_tfafb_=objectinherit[unit,'a,'syn,unit,'b,'syn,unit,'self,'syn]arrow_tconstraint'syn=HTML.viewermethodc_Arrow()_=HTML.string"<arrow>"endclass['a,'sa,'b,'sb,'self]gmap_arrow_t(fa:unit->'a->'sa)fb_=objectinherit[unit,'a,'sa,unit,'b,'sb,unit,'self,('sa,'sb)arrow]arrow_tmethodc_Arrow()_=failwith"gmap for arrows is not implemented"endclass['a,'sa,'b,'sb,'env,'self]eval_arrow_tfafb_=objectinherit['env,'a,'sa,'env,'b,'sb,'env,'self,('sa,'sb)arrow]arrow_tmethodc_Arrow__=failwith"eval for arrows is not implemented"endclass['a,'sa,'b,'sb,'self,'syn,'env]stateful_arrow_tfafb_=objectinherit['env,'a,'env*'sa,'env,'b,'sb,'env,'self,'env*('sa,'sb)arrow]arrow_tmethodc_Arrow__=failwith"stateful for arrows is not implemented"endclass['a,'b,'syn,'self]foldl_arrow_tfafb_=objectinherit['syn,'a,'syn,'syn,'b,'syn,'syn,'self,'syn]arrow_tmethodc_Arrow__=failwith"foldl for arrows is not implemented"endclass['a,'b,'syn,'self]foldr_arrow_tfafb_=objectinherit['syn,'a,'syn,'syn,'b,'syn,'syn,'self,'syn]arrow_tmethodc_Arrow__=failwith"foldr for arrows is not implemented"endclass['a,'b,'self]eq_arrow_tfafb_=objectinherit['a,'a,bool,'b,'b,bool,('a,'b)arrow,'self,bool]arrow_tmethodc_Arrow__=failwith "eq for arrows is not implemented"endclass['a,'b,'self]compare_arrow_tfafb_=objectinherit['a,'a,'syn,'b,'b,'syn,('a,'b)arrow,'self,'syn]arrow_tconstraint'syn=comparisonmethodc_Arrow__=failwith"compare for arrows is not implemented"endletarrow:(('ia,'a,'sa,'ib,'b,'sb,'inh,_,'syn)#arrow_t->'inh ->('a,'b)arrow->'syn,<show:('a->string)->('b->string)->('a,'b)arrow->string;html:('a->HTML.viewer)->('b-> HTML.viewer)->('a,'b)arrow ->HTML.viewer;gmap:('a->'c)->('b->'d)->('a,'b)arrow->('c,'d)arrow;enum:('a->int)->('b->int)->('a,'b)arrow->int;fmt:(Format.formatter->'a->unit)->(Format.formatter->'b->unit)->Format.formatter->('a,'b)arrow->unit;stateful:('env->'a->'env*'c)->('env->'b->'env*'d)->'env->('a,'b)arrow->'env*('c,'d)arrow;eval:('env->'a->'c)->('env->'b->'d)->'env->('a,'b)arrow->('c,'d)arrow;foldl:('c->'a->'c)->('c->'b->'c)->'c->('a,'b)arrow ->'c;foldr:('c->'a->'c)->('c->'b->'c)->'c->('a,'b)arrow ->'c;eq:('a->'a->bool)->('b->'b->bool)->('a,'b)arrow->('a,'b)arrow-> bool;compare:('a->'a->comparison)->('b->'b->comparison)->('a,'b)arrow->('a,'b)arrow->comparison;>,_)t={gcata=gcata_arrow;fix=(func->transform_gcgcata_arrowc);plugins=lettrobjsubj=transform_gcgcata_arrowobj()subjinlettr1objinhsubj=transform_gcgcata_arrowobjinhsubjinobjectmethodshowfafb=tr(newshow_arrow_t(liftfa)(liftfb))methodhtmlfafb=tr(newhtml_arrow_t(liftfa)(liftfb))methodgmapfafb=tr(newgmap_arrow_t(liftfa)(liftfb))methodenumfafb=tr(newenum_arrow_t(liftfa)(liftfb))methodfmtfafb=tr1(newfmt_arrow_tfafb)methodevalfafb=tr1(neweval_arrow_tfafb)methodstatefulfafb=tr1(newstateful_arrow_tfafb)methodeqfafb=tr1(neweq_arrow_tfafb)methodcomparefafb=tr1(newcompare_arrow_tfafb)methodfoldlfafb=tr1 (newfoldl_arrow_t fafb)methodfoldrfafb=tr1(newfoldr_arrow_tfafb)end}(*******************************************************************************)(****************************************************************************)(* {1 Mutable references}*)type'aref2='areftype 'aref='aref2classvirtual['ia,'a,'sa,'inh,'e,'syn]ref_t=objectmethodvirtualc_ref:'inh->'a->'synendletgcata_reftrinhr=tr#c_ref inh!rclass['a,'self]fmt_ref_tfa_=objectinherit['inh,'a,unit,'inh,'self,unit]ref_tconstraint'inh=Format.formattermethodc_reffmta=Format.fprintffmt"!(%a)"faaendclass['a,'self]html_ref_tfa_=objectinherit['inh,'a,'syn,'inh,'self,'syn]ref_tconstraint'syn=HTML.viewerconstraint'inh=unitmethod c_ref()a=fa()aendclass['a,'self]show_ref_tfa_=objectinherit['inh,'a,'syn,'inh,'self,'syn]ref_tconstraint'syn=stringconstraint'inh=unitmethodc_ref()a=fa()aendletref:(('ia,'a,'sa,'inh,_,'syn)#ref_t ->'inh->'aref->'syn,<html:('a-> HTML.er)->'aref->HTML.er;show:('a-> string)->'aref->string;fmt:(Format.formatter->'a->unit)->Format.formatter->'aref->unit;>,_)t={gcata=gcata_ref;fix=(func->transform_gcgcata_refc);plugins=objectmethod showfa=transform_gcgcata_ref(newshow_ref_t(liftfa))()methodhtmlfa=transform_gcgcata_ref(newhtml_ref_t(liftfa))()methodfmtfa=transform_gcgcata_ref(newfmt_ref_tfa)end}(*** arrays *****************************************************************)(* TODO: array are not really implemented *)(* {1 Arrays (N.B. WIP) } *)type 'aparray='aarraytype'aarray ='aparrayclassvirtual['ia,'a,'sa,'inh,'self,'syn]array_t=objectmethodvirtualdo_array:'inh->'aarray->'synendletgcata_arraytrinhsubj=tr#do_array inhsubjclass['a,'self]show_array_tfafself=objectinherit[unit,'a,string,unit,'self,string]array_tmethod do_array()arr="[|"^(Array.fold_right(fun xs->Printf.sprintf"%a; %s"faxs)arr" |]")endclass['a,'self]enum_array_tfafself=objectinherit[unit,'a,int,unit,'self,int]array_tmethoddo_array()_=0endclass['a,'sa,'self,'syn]gmap_array_tfafself=objectinherit[unit,'a,'sa,unit,'self,'syn]array_tconstraint'syn='saarraymethoddo_array()arr=Array.map(fa())arrendclass['a,'self]html_array_tfafself=objectinherit[unit,'a,HTML.viewer,unit,'self,HTML.viewer]array_tmethoddo_array()arr=HTML.ul@@HTML.seq([HTML.string"array"]@List.map(funx->HTML.li@@fa()x)@@Array.to_listarr)endclass['a,'self]fmt_array_tfafself=objectinherit[Format.formatter,'a,unit,Format.formatter,'self,unit]array_tmethoddo_arrayfmtarr=Format.fprintffmt"[| ";Array.iter(funx->Format.fprintffmt"%a; "fax)arr;Format.fprintffmt" |]"endclass['a,'sa,'self,'syn,'env]eval_array_tfafself=objectinherit['env,'a,'sa,'env,'self,'syn]array_tconstraint'syn='saarraymethoddo_arrayenvarr=Array.map(faenv)arrendclass['a,'sa,'self,'syn,'env]stateful_array_tfafself=objectinherit['env,'a,'env*'sa,'env,'self,'env*'saarray]array_tmethod do_arrayenv0arr=letn=Array.lengtharrinifn=0then([||],env0)elselet(x1,env1)=faenv0(Array.getarr0)inletenv=Stdlib.ref env1inletans=Array.makenx1infori=1ton-1dolet(x,env2)=fa!env(Array.getarri)inenv:=env2;Array.setansixdone;(!env,ans)endclass['a,'syn,'self]foldl_array_tfafself=objectinherit['syn,'a,'syn,'syn,'self,'syn]array_tmethoddo_arrayenvarr=Array.fold_leftfaenvarrendclass['a,'syn,'self]foldr_array_tfafself=objectinherit['syn,'a,'syn,'syn,'self,'syn]array_tmethoddo_arrayenvarr=Array.fold_right(funxacc->faaccx)arrenvendclass['a,'self]eq_array_tfafself=objectinherit['a,'a,bool,'aarray,'self,bool]array_tmethoddo_arrayenvarr=letn=Array.lengtharrin(Array.lengthenv=n)&&(letans=Stdlib.reftrueinfori=0tondoans:=!ans&&(fa(Array.getenvi)(Array.getarri))done;!ans)endclass['a,'self]compare_array_tfafself=objectinherit['a,'a,comparison,'aarray,'self,comparison]array_tmethoddo_arrayenvarr=letn=Array.lengtharrinifArray.lengthenv<nthenLTelse(letans=Stdlib.refEQinfori=0tondoans:= chain_compare!ans(fun()->fa(Array.getenvi)(Array.getarri))done;!ans)endletarray={gcata=gcata_array;fix=(fun c->transform_gcgcata_arrayc);plugins=let trobjfas=transform_gcgcata_array(objfa)()sinlettr1objfais=transform_gc gcata_array(objfa)isinobjectmethodshowfa=tr(newshow_array_t)(liftfa)methodgmapfa=tr(newgmap_array_t)(liftfa)method htmlfa=tr(newhtml_array_t)(lift fa)methodfmtfa=tr1(newfmt_array_t)famethodevalfa=tr1(neweval_array_t)famethodstatefulfa=tr1(newstateful_array_t)famethodcomparefa=tr1(newcompare_array_t)famethodeqfa=tr1(neweq_array_t)famethodfoldlfa=tr1(newfoldl_array_t)famethodfoldrfa=tr1(newfoldr_array_t)famethodenumfa=tr1(newenum_array_t)faend}(*** bytes *****************************************************************)(* {1 Bytes (mutable string) } *)typebytes=Bytes.tclassvirtual['inh,'self,'syn]bytes_t=objectmethodvirtualdo_bytes:'inh->bytes->'synendletgcata_bytestrinhsubj=tr#do_bytesinhsubjclass['self]html_bytes_tfself=objectinherit[unit,'self,HTML.viewer]bytes_tmethoddo_bytes()arr=HTML.string@@Bytes.to_stringarrendclass['self]show_bytes_tfself=objectinherit[unit,'self,string]bytes_tmethoddo_bytes()=Bytes.to_stringendclass['self,'syn]gmap_bytes_tfself=objectinherit[unit,'self,'syn]bytes_tconstraint'syn=bytesmethoddo_bytes()arr=arrendclass['self]fmt_bytes_tfself=objectinherit[Format.formatter,'self,unit]bytes_tmethod do_bytesfmtarr=Format.fprintffmt"%S"(Bytes.to_stringarr)endclass['self,'syn,'env]eval_bytes_tfself=objectinherit['env,'self,'syn]bytes_tconstraint'syn=bytesmethoddo_bytesenvarr=arrendclass['self,'syn,'env]stateful_bytes_tfself=objectinherit['env,'self,'syn]bytes_tconstraint'syn='env*bytesmethoddo_bytes env0arr=(env0,arr)endclass ['syn,'self]foldl_bytes_tfself=objectinherit['syn,'self,'syn]bytes_tmethoddo_bytesenv_=envendclass['syn,'self]foldr_bytes_tfself=objectinherit ['syn,'self,'syn]bytes_tmethoddo_bytesenv_=envendclass['self]eq_bytes_t fself=objectinherit[bytes,'self,bool]bytes_tmethoddo_bytesenvarr=(Bytes.compareenvarr=0)endclass['self]compare_bytes_tfself=objectinherit[bytes,'self,comparison]bytes_tmethoddo_bytesenvarr=letc=Bytes.compareenvarrinifc<0thenLTelseifc=0thenEQelseGTendletbytes={gcata=gcata_bytes;fix=(func->transform_gcgcata_bytesc);plugins =lettrobjs=gcata_bytes(obj(fun__->assertfalse))()sinlettr1objis=gcata_bytes(obj(fun__->assertfalse))isinobjectmethodshow=tr(newshow_bytes_t)methodgmap=tr(newgmap_bytes_t)methodhtml=tr(newhtml_bytes_t)methodfmt=tr1(newfmt_bytes_t)methodeval=tr1(neweval_bytes_t)methodstateful=tr1(newstateful_bytes_t)methodcompare=tr1(newcompare_bytes_t)methodeq=tr1(neweq_bytes_t)methodfoldl =tr1(new foldl_bytes_t)methodfoldr=tr1(newfoldr_bytes_t)end}(****************************************************************************)letshowt=t.plugins#showlethtmlt=t.plugins#htmlletgmapt=t.plugins#gmapletfmtt=t.plugins#fmtletevalt=t.plugins#evalletfoldlt=t.plugins#foldlletfoldrt=t.plugins#foldrleteqt=t.plugins#eqlet comparet=t.plugins#compareletstatefult=t.plugins#statefulletevalt=t.plugins#evalletenumt=t.plugins#enum