123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834(* camlp5r *)(* exparser.ml,v *)(* Copyright (c) INRIA 2007-2017 *)(* #load "q_MLast.cmo" *)typespat_comp=SpTrmofMLast.loc*MLast.patt*MLast.exproptionMLast.v|SpNtrofMLast.loc*MLast.patt*MLast.expr|SpLetofMLast.loc*MLast.patt*MLast.expr|SpLhdofMLast.loc*MLast.pattlistlist|SpStrofMLast.loc*MLast.patttypesexp_comp=SeTrmofMLast.loc*MLast.expr|SeNtrofMLast.loc*MLast.exprtypespat_comp_opt=SpoNoth|SpoBang|SpoQuesofMLast.exprtypespat_parser_ast=MLast.pattoption*((spat_comp*spat_comp_opt)list*MLast.pattoption*MLast.expr)listletstrm_n="strm__"letpeek_funloc=MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"peek"))letjunk_funloc=MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"junk"))(* Parsers *)letrecpattern_eq_expressionpe=matchp,ewithMLast.PaLid(_,Ploc.VaVala),MLast.ExLid(_,Ploc.VaValb)->a=b|MLast.PaLong(_,MLast.LiUid(_,Ploc.VaVala),Ploc.VaVal[]),MLast.ExLong(_,MLast.LiUid(_,Ploc.VaValb))->a=b|MLast.PaApp(_,p1,p2),MLast.ExApp(_,e1,e2)->pattern_eq_expressionp1e1&&pattern_eq_expressionp2e2|_->falseletis_raisee=matchewithMLast.ExApp(_,MLast.ExLid(_,Ploc.VaVal"raise"),_)->true|_->falseletis_raise_failuree=matchewithMLast.ExApp(_,MLast.ExLid(_,Ploc.VaVal"raise"),MLast.ExLong(_,MLast.LiAcc(_,MLast.LiUid(_,Ploc.VaVal"Stream"),Ploc.VaVal"Failure")))->true|_->falseletrechandle_failuree=matchewithMLast.ExTry(_,te,Ploc.VaVal[MLast.PaLong(_,MLast.LiAcc(_,MLast.LiUid(_,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"),Ploc.VaVal[]),Ploc.VaValNone,e])->handle_failuree|MLast.ExMat(_,me,Ploc.VaValpel)->handle_failureme&&List.for_all(function_,Ploc.VaValNone,e->handle_failuree|_->false)pel|MLast.ExLet(_,Ploc.VaValfalse,Ploc.VaValpel,e)->List.for_all(fun(p,e,_)->handle_failuree)pel&&handle_failuree|MLast.ExSeq(_,Ploc.VaValel)->List.for_allhandle_failureel|MLast.ExFle(_,MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_)),Ploc.VaVal(None,Ploc.VaVal_))|MLast.ExLid(_,Ploc.VaVal_)|MLast.ExInt(_,Ploc.VaVal_,"")|MLast.ExStr(_,Ploc.VaVal(_,Ploc.VaVal_))|MLast.ExChr(_,Ploc.VaVal_)|MLast.ExFun(_,Ploc.VaVal_)|MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))->true|MLast.ExApp(_,MLast.ExLid(_,Ploc.VaVal"raise"),e)->beginmatchewithMLast.ExLong(_,MLast.LiAcc(_,MLast.LiUid(_,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"))->false|_->trueend|MLast.ExApp(_,f,x)->no_raising_failure_funf&&handle_failuref&&handle_failurex|_->falseandno_raising_failure_fun=functionMLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))->true|MLast.ExLid(_,Ploc.VaVal_)->false|MLast.ExFle(_,MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"peek"))|MLast.ExFle(_,MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"junk"))->true|MLast.ExApp(_,x,y)->no_raising_failure_funx&&handle_failurey|_->falseletrecsubstve=matchewithMLast.ExLid(loc,Ploc.VaValx)->letx=ifx=vthenstrm_nelsexinMLast.ExLid(loc,Ploc.VaValx)|MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))->e|MLast.ExInt(_,Ploc.VaVal_,"")->e|MLast.ExChr(_,Ploc.VaVal_)->e|MLast.ExStr(_,Ploc.VaVal(_,Ploc.VaVal_))->e|MLast.ExFle(_,_,Ploc.VaVal(None,Ploc.VaVal_))->e|MLast.ExLet(loc,Ploc.VaValrf,Ploc.VaValpel,e)->MLast.ExLet(loc,Ploc.VaValrf,Ploc.VaVal(List.map(subst_pev)pel),substve)|MLast.ExApp(loc,e1,e2)->MLast.ExApp(loc,substve1,substve2)|MLast.ExTup(loc,Ploc.VaValel)->MLast.ExTup(loc,Ploc.VaVal(List.map(substv)el))|_->raiseNot_foundandsubst_pev(p,e,attrs)=matchpwithMLast.PaLid(_,Ploc.VaValv')whenv<>v'->p,substve,attrs|_->raiseNot_foundletoptim=reftruelet_=Pcaml.add_option"-no-pa-opt"(Arg.Clearoptim)"No parsers optimization."letrecperhaps_bounds=functionMLast.ExTup(_,Ploc.VaValel)->List.exists(perhaps_bounds)el|MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))|MLast.ExStr(_,Ploc.VaVal(_,Ploc.VaVal_))->false|_->trueletwildcard_if_not_boundpe=matchpwithMLast.PaLid(loc,Ploc.VaVals)->ifperhaps_boundsethenpelseMLast.PaAnyloc|_->pletstream_pattern_componentskontckont=functionSpTrm(loc,p,wo)->MLast.ExMat(loc,MLast.ExApp(loc,peek_funloc,MLast.ExLid(loc,Ploc.VaValstrm_n)),Ploc.VaVal[MLast.PaApp(loc,MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some"),Ploc.VaVal[]),p),wo,MLast.ExSeq(loc,Ploc.VaVal[MLast.ExApp(loc,junk_funloc,MLast.ExLid(loc,Ploc.VaValstrm_n));skont]);MLast.PaAnyloc,Ploc.VaValNone,ckont])|SpNtr(loc,p,e)->lete=matchewithMLast.ExFun(_,Ploc.VaVal[MLast.PaTyc(_,MLast.PaLid(_,Ploc.VaValv),MLast.TyApp(_,MLast.TyAcc(_,MLast.LiUid(_,Ploc.VaVal"Stream"),Ploc.VaVal"t"),MLast.TyAny_)),Ploc.VaValNone,e])whenv=strm_n->e|_->MLast.ExApp(loc,e,MLast.ExLid(loc,Ploc.VaValstrm_n))inif!optimthenifpattern_eq_expressionpskontthenifis_raise_failureckonttheneelseifhandle_failureetheneelseMLast.ExTry(loc,e,Ploc.VaVal[MLast.PaLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"),Ploc.VaVal[]),Ploc.VaValNone,ckont])elseifis_raise_failureckontthenletp=wildcard_if_not_boundpskontinMLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[p,e,Ploc.VaVal[]],skont)elseifis_raiseckontthenlettst=ifhandle_failureetheneelseMLast.ExTry(loc,e,Ploc.VaVal[MLast.PaLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"),Ploc.VaVal[]),Ploc.VaValNone,ckont])inletp=wildcard_if_not_boundpskontinMLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[p,tst,Ploc.VaVal[]],skont)elseifpattern_eq_expression(MLast.PaApp(loc,MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some"),Ploc.VaVal[]),p))skontthenMLast.ExTry(loc,MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some")),e),Ploc.VaVal[MLast.PaLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"),Ploc.VaVal[]),Ploc.VaValNone,ckont])elseMLast.ExMat(loc,MLast.ExTry(loc,MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some")),e),Ploc.VaVal[MLast.PaLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"),Ploc.VaVal[]),Ploc.VaValNone,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"None"))]),Ploc.VaVal[MLast.PaApp(loc,MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some"),Ploc.VaVal[]),p),Ploc.VaValNone,skont;MLast.PaAnyloc,Ploc.VaValNone,ckont])elseMLast.ExMat(loc,MLast.ExTry(loc,MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some")),e),Ploc.VaVal[MLast.PaLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure"),Ploc.VaVal[]),Ploc.VaValNone,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"None"))]),Ploc.VaVal[MLast.PaApp(loc,MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some"),Ploc.VaVal[]),p),Ploc.VaValNone,skont;MLast.PaAnyloc,Ploc.VaValNone,ckont])|SpLet(_,_,_)->assertfalse|SpLhd(loc,pl::pll)->letmklistpatlocpl=List.fold_right(funp1p2->MLast.PaApp(loc,MLast.PaApp(loc,MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"::"),Ploc.VaVal[]),p1),p2))pl(MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"[]"),Ploc.VaVal[]))inletlen=List.lengthplinifList.exists(funpl->List.lengthpl<>len)pllthenPloc.raiseloc(Stream.Error"lookahead patterns must be of the same lengths")elseletp=letp=mklistpatlocplinletpl=List.map(mklistpatloc)pllinList.fold_left(funp1p2->MLast.PaOrp(loc,p1,p2))pplinMLast.ExMat(loc,MLast.ExApp(loc,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"npeek")),MLast.ExInt(loc,Ploc.VaVal(string_of_intlen),"")),MLast.ExLid(loc,Ploc.VaVal"strm__")),Ploc.VaVal[p,Ploc.VaValNone,skont;MLast.PaAnyloc,Ploc.VaValNone,ckont])|SpLhd(loc,[])->ckont|SpStr(loc,p)->trymatchpwithMLast.PaLid(_,Ploc.VaValv)->MLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[MLast.PaLid(loc,Ploc.VaValv),MLast.ExLid(loc,Ploc.VaVal"strm__"),Ploc.VaVal[]],skont)|_->raiseNot_foundwithNot_found->MLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[p,MLast.ExLid(loc,Ploc.VaValstrm_n),Ploc.VaVal[]],skont)letrecstream_patternlocepoeekont=function[]->beginmatchepowithSomeep->MLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[ep,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"count")),MLast.ExLid(loc,Ploc.VaValstrm_n)),Ploc.VaVal[]],e)|_->eend|(SpLet(loc,p1,e1),_)::spcl->letskont=stream_patternlocepoeekontspclinMLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[p1,e1,Ploc.VaVal[]],skont)|(spc,err)::spcl->letskont=letekont=functionSpoQuesestr->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Error")),estr))|SpoBang->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure")))|SpoNoth->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Error")),MLast.ExStr(loc,Ploc.VaVal(loc,Ploc.VaVal""))))instream_patternlocepoeekontspclinletckont=ekonterrinstream_pattern_componentskontckontspcletstream_patterns_termlocekonttspel=letpel=List.map(fun(p,w,loc,spcl,epo,e)->letp=MLast.PaApp(loc,MLast.PaLong(loc,MLast.LiUid(loc,Ploc.VaVal"Some"),Ploc.VaVal[]),p)inlete=letekont=functionSpoQuesestr->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Error")),estr))|SpoBang->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure")))|SpoNoth->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExApp(loc,MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Error")),MLast.ExStr(loc,Ploc.VaVal(loc,Ploc.VaVal""))))inletskont=stream_patternlocepoeekontspclinMLast.ExSeq(loc,Ploc.VaVal[MLast.ExApp(loc,junk_funloc,MLast.ExLid(loc,Ploc.VaValstrm_n));skont])inp,w,e)tspelinletpel=pel@[MLast.PaAnyloc,Ploc.VaValNone,ekont()]inMLast.ExMat(loc,MLast.ExApp(loc,peek_funloc,MLast.ExLid(loc,Ploc.VaValstrm_n)),Ploc.VaValpel)letrecgroup_terms=function((SpTrm(loc,p,w),SpoNoth)::spcl,epo,e)::spel->let(tspel,spel)=group_termsspelin(p,w,loc,spcl,epo,e)::tspel,spel|spel->[],spelletrecparser_casesloc=function[]->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure")))|spel->if!optimthenmatchgroup_termsspelwith[],(spcl,epo,e)::spel->stream_patternlocepoe(fun_->parser_caseslocspel)spcl|tspel,spel->stream_patterns_termloc(fun_->parser_caseslocspel)tspelelsematchspelwith(spcl,epo,e)::spel->stream_patternlocepoe(fun_->parser_caseslocspel)spcl|[]->MLast.ExApp(loc,MLast.ExLid(loc,Ploc.VaVal"raise"),MLast.ExLong(loc,MLast.LiAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"Failure")))(* optim: left factorization of consecutive rules *)type('a,'b)tree_node=Nodeof'a*('a,'b)tree_nodelist|Leafof'bletrecmap_tree_nodef_nodef_leaf=functionNode(x,tl)->f_nodex(List.map(map_tree_nodef_nodef_leaf)tl)|Leafb->f_leafbletrecinsert_in_treeeq(l,a)tl=matchtlwithNode(n,tl1)::tl2->beginmatchlwithx::l->ifeqxnthenNode(n,insert_in_treeeq(l,a)tl1)::tl2elseList.fold_right(funxn->Node(x,[n]))(x::l)(Leafa)::tl|[]->letrecloop=functionNode(n,tl1)::tl->Node(n,tl1)::looptl|tl->Leafa::tlinlooptlend|_->List.fold_right(funxn->Node(x,[n]))l(Leafa)::tllettree_of_listeqll=List.fold_right(insert_in_treeeq)ll[]letreclist_of_treemk_nodemk_leaftl=List.map(map_tree_nodemk_nodemk_leaf)tlleteq_spat_compspc1spc2=matchspc1,spc2withSpTrm(_,p1,Ploc.VaValNone),SpTrm(_,p2,Ploc.VaValNone)->Reloc.eq_pattp1p2|SpNtr(_,p1,e1),SpNtr(_,p2,e2)->Reloc.eq_pattp1p2&&Reloc.eq_expre1e2|_->falseleteq_spospco1spco2=matchspco1,spco2withSpoQuese1,SpoQuese2->Reloc.eq_expre1e2|_->spco1=spco2leteq_spat_comp_opt(spc1,spco1)(spc2,spco2)=eq_spat_compspc1spc2&&eq_spospco1spco2letmk_emptyb=[],bletmk_rulex=function[]->failwith"mk_rule"|[rl,a]->x::rl,a|ll->letloc=Ploc.dummyinlete=letrl=List.map(fun(rl,(eo,a))->rl,eo,a)llinlete=parser_caseslocrlinletp=MLast.PaTyc(loc,MLast.PaLid(loc,Ploc.VaValstrm_n),MLast.TyApp(loc,MLast.TyAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"t"),MLast.TyAnyloc))inMLast.ExFun(loc,Ploc.VaVal[p,Ploc.VaValNone,e])inletspo=ifList.exists(fun(rl,_)->matchrlwith[]->true|(_,SpoBang)::_->true|_->false)llthenSpoBangelseSpoNothin[x;SpNtr(loc,MLast.PaLid(loc,Ploc.VaVal"a"),e),spo],(None,MLast.ExLid(loc,Ploc.VaVal"a"))letleft_factorizerl=letrl=List.map(fun(rl,eo,a)->rl,(eo,a))rlinlett=tree_of_listeq_spat_comp_optrlinletrl=list_of_treemk_rulemk_emptytinList.map(fun(rl,(eo,a))->rl,eo,a)rl(* Converting into AST *)letcparserloc(bpo,pc)=letpc=left_factorizepcinlete=parser_caseslocpcinlete=letloc=Ploc.with_commentloc""inmatchbpowithSomebp->MLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[bp,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"count")),MLast.ExLid(loc,Ploc.VaValstrm_n)),Ploc.VaVal[]],e)|None->einletp=letloc=Ploc.with_commentloc""inMLast.PaTyc(loc,MLast.PaLid(loc,Ploc.VaValstrm_n),MLast.TyApp(loc,MLast.TyAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"t"),MLast.TyAnyloc))inMLast.ExFun(loc,Ploc.VaVal[p,Ploc.VaValNone,e])letrecis_not_bounds=functionMLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))->true|MLast.ExApp(_,MLast.ExLid(_,Ploc.VaVal"raise"),MLast.ExLong(_,MLast.LiAcc(_,MLast.LiUid(_,Ploc.VaVal"Stream"),Ploc.VaVal"Failure")))->true|_->falseletcparser_matchlocme(bpo,pc)=letpc=left_factorizepcinletiloc=Ploc.with_commentloc""inletpc=parser_casesilocpcinlete=letloc=ilocinmatchbpowithSomebp->MLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[bp,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"count")),MLast.ExLid(loc,Ploc.VaValstrm_n)),Ploc.VaVal[]],pc)|None->pcinmatchmewithMLast.ExLid(_,Ploc.VaValx)whenx=strm_n->e|_->letp=letloc=ilocinifis_not_boundstrm_nethenMLast.PaAnylocelseMLast.PaLid(loc,Ploc.VaValstrm_n)inMLast.ExLet(loc,Ploc.VaValfalse,Ploc.VaVal[MLast.PaTyc(loc,p,MLast.TyApp(loc,MLast.TyAcc(loc,MLast.LiUid(loc,Ploc.VaVal"Stream"),Ploc.VaVal"t"),MLast.TyAnyloc)),me,Ploc.VaVal[]],e)(* Streams *)letrecnot_computing=functionMLast.ExLid(_,Ploc.VaVal_)|MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))|MLast.ExInt(_,Ploc.VaVal_,"")|MLast.ExFlo(_,Ploc.VaVal_)|MLast.ExChr(_,Ploc.VaVal_)|MLast.ExStr(_,Ploc.VaVal(_,Ploc.VaVal_))->true|MLast.ExApp(_,x,y)->is_cons_apply_not_computingx&¬_computingy|_->falseandis_cons_apply_not_computing=functionMLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal_))->true|MLast.ExLid(_,Ploc.VaVal_)->false|MLast.ExApp(_,x,y)->is_cons_apply_not_computingx&¬_computingy|_->falseletslazyloce=matchewithMLast.ExApp(_,f,MLast.ExLong(_,MLast.LiUid(_,Ploc.VaVal"()")))->beginmatchfwithMLast.ExLid(_,Ploc.VaVal_)->f|_->MLast.ExFun(loc,Ploc.VaVal[MLast.PaAnyloc,Ploc.VaValNone,e])end|_->MLast.ExFun(loc,Ploc.VaVal[MLast.PaAnyloc,Ploc.VaValNone,e])letreccstreamgloc=function[]->letloc=glocinMLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"sempty"))|[SeTrm(loc,e)]->ifnot_computingethenMLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"ising")),e)elseMLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"lsing")),slazyloce)|SeTrm(loc,e)::secl->ifnot_computingethenMLast.ExApp(loc,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"icons")),e),cstreamglocsecl)elseMLast.ExApp(loc,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"lcons")),slazyloce),cstreamglocsecl)|[SeNtr(loc,e)]->ifnot_computingetheneelseMLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"slazy")),slazyloce)|SeNtr(loc,e)::secl->ifnot_computingethenMLast.ExApp(loc,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"iapp")),e),cstreamglocsecl)elseMLast.ExApp(loc,MLast.ExApp(loc,MLast.ExFle(loc,MLast.ExLong(loc,MLast.LiUid(loc,Ploc.VaVal"Stream")),Ploc.VaVal(None,Ploc.VaVal"lapp")),slazyloce),cstreamglocsecl)